Make a specified color in a window transparent

Example scripts and tips (replaces Old Scripts & Tips archive)

Moderators: Dorian (MJT support), JRL, Phil Pendlebury

Post Reply
User avatar
JRL
Automation Wizard
Posts: 3397
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Make a specified color in a window transparent

Post by JRL » Fri Sep 05, 2008 5:17 am

Browsing the Microsoft website to understand more about window opacity, I stumbled across information several forum users have requested. How to make a portion of a window transparent. This uses the same function calls that can change window opacity but uses different parameters. Basically, you get to select one color in the window that will be made transparent. The color number that is entered as a parameter, is of the same color numbering scheme used in Macro Scheduler. This makes the color number determination easy.

Here are a couple of examples. The first one requires version 10 or greater because the color that is made transparent is colored text. The second one opens notepad then makes the notepad typing field transparent. This will require version 7 or greater since Libfunc> was introduced in version 7.

This first example will turn a green box transparent. Drag the window around a bit to prove that the area is truly transparent.

Code: Select all

Dialog>Dialog1
   Caption=Transparent Color Demo
   Width=570
   Height=165
   Top=0
   Left=0
   Label=g,4,4
   Label=Press the button%CRLF%to make the green box transparent.,16,48,true
   Button=Make%CRLF%Transparent,320,4,137,80,3
EndDialog>Dialog1

//Set Text Color (color number 234567 is a shade of green)
//The letter "g" in webdings is a filled box.  Nice for doing
//background colors in dialogs.
  SetDialogObjectFont>Dialog1,msLabel1,WebDings,100,1,234567
//Set Text Color (color number 0 is black)
  SetDialogObjectFont>Dialog1,msLabel2,Ariel,24,1,0


//Open the dialog (normal appearance)
  Show>Dialog1,res1
//If the "Make Transparent" button is pressed... Do It!
If>res1=3,Process

SRT>Process
//constants
  Let>GWL_EXSTYLE=-20
  Let>WS_EX_LAYERED=524288
  Let>LWA_COLORKEY=1

//get style attributes of dialog1 window
  LibFunc>user32,GetWindowLongA,attribs,dialog1.handle,GWL_EXSTYLE
  Let>attribs={%attribs% OR %WS_EX_LAYERED%}
//make selected color transparent (color number 234567)
  LibFunc>user32,SetWindowLongA,swl,dialog1.handle,GWL_EXSTYLE,attribs
  LibFunc>user32,SetLayeredWindowAttributes,res,dialog1.handle,234567,0,LWA_COLORKEY
//Make the button disappear
  LibFunc>User32,ShowWindow,SWres,dialog1.msbutton1.handle,0
//Show the dialog without text for a couple of seconds
  Let>dialog1.mslabel2=%TAB% Look!!!
  ResetDialogAction>dialog1
  Show>dialog1
  Wait>2
  Closedialog>dialog1
//Put text on the dialog and open it one last time
  Let>dialog1.mslabel2=There's a hole in this window%CRLF%%TAB%where a color used to be!
  ResetDialogAction>dialog1
  Show>dialog1,res1
END>Process
This one Opens Notepad, makes the typing field transparent then enters a Bill Gates quote. Notice you can pick through or type on the transparent field.

Code: Select all

Run>Notepad.Exe
WaitWindowOpen>Notepad*
Wait>0.2
GoSub>Process
SetFocus>Notepad*
Send>You've got to be willing to read other people's code, and then write your own, then have other people review your code. You've got to want to be in this incredible feedback loop where you get the world-class people to tell you what you're doing wrong...%CRLF%%TAB%-- Bill Gates

SRT>Process
//constants
  Let>GWL_EXSTYLE=-20
  Let>WS_EX_LAYERED=524288
  Let>LWA_COLORKEY=1
//get style attributes of Notepad
  GetWindowHandle>Notepad*,NPhdl
  LibFunc>user32,GetWindowLongA,attribs,NPhdl,GWL_EXSTYLE
  Let>attribs={%attribs% OR %WS_EX_LAYERED%}
//make selected color transparent (color number 16777215)
  LibFunc>user32,SetWindowLongA,swl,NPhdl,GWL_EXSTYLE,attribs
  LibFunc>user32,SetLayeredWindowAttributes,res,NPhdl,16777215,0,LWA_COLORKEY
END>Process

User avatar
JRL
Automation Wizard
Posts: 3397
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Fri Sep 05, 2008 10:04 pm

Here's another example. this will float the text of the dialog on the screen while the rest of the dialog is invisible. This requires Macro Scheduler Version 10 or greater.

Code: Select all

Dialog>Dialog1
   Caption=Transparent Color Demo
   Width=630
   Height=160
   Top=-10000
   Left=CENTER
   Label=g,0,0
   Label=  Float Text on an invisible dialog.%CRLF%This will close in about,16,32,True
   Label=10,390,70,True
EndDialog>Dialog1

//Borderless Dialog
  LibFunc>user32,SetWindowLongA,sres,dialog1.handle,-16,1073741824

Show>Dialog1

//Set Text Color
  SetDialogObjectFont>Dialog1,msLabel1,WebDings,1000,1,234567
  SetDialogObjectFont>Dialog1,msLabel2,Arial,24,1,0
  SetDialogObjectFont>Dialog1,msLabel3,Arial,24,1,0

//constants
  Let>GWL_EXSTYLE=-20
  Let>WS_EX_LAYERED=524288
  Let>LWA_COLORKEY=1

//get style attributes of dialog1 window
  LibFunc>user32,GetWindowLongA,attribs,dialog1.handle,GWL_EXSTYLE
  Let>attribs={%attribs% OR %WS_EX_LAYERED%}
//make selected color transparent
  LibFunc>user32,SetWindowLongA,swl,dialog1.handle,GWL_EXSTYLE,attribs
  LibFunc>user32,SetLayeredWindowAttributes,res,dialog1.handle,234567,0,LWA_COLORKEY
//Eliminate the scrollbars that occur because I'm too lazy to make the area covered
//by the green text exactly the same size as the dialog.
  GetWindowPos>Transparent Color Demo,WinX,WinY
  MoveWindow>Transparent Color Demo,WinX,300
  LibFunc>user32,ShowScrollBar,sbres,dialog1.handle,3,0
  LibFunc>user32,ShowScrollBar,sbres,dialog1.handle,3,0
//Wait>a few seconds then close
Let>kk=0
Label>Loop
  GetCursorPos>CurX,CurY
  Wait>0.005
  Add>kk,1
  If>%kk%>800,EOF
  Let>dialog1.mslabel3={10-(round(%kk%/80))}
  Concat>dialog1.mslabel3, seconds.
  ResetDialogAction>dialog1
  //Unremark the next line the have the text follow the mouse cursor 
  //MoveWindow>Transparent Color Demo,CurX,CurY
  LibFunc>user32,ShowScrollBar,sbres,dialog1.handle,3,0
  LibFunc>user32,ShowScrollBar,sbres,dialog1.handle,3,0
Goto>Loop

Label>EOF

One more example that floats MJTNET text then creates a "cloudlike" background and finally plops the mjtnet tagline on the "cloud". Sort of a splashscreen.

Code: Select all

GetScreenRes>ScreenX,ScreenY
Let>ScreenX=%ScreenX%/2
sub>screenX,190
Let>screenX3=%screenX-20
Let>ScreenY=%ScreenY%/2

Dialog>Dialog1
   Caption=mjtnet logo
   Width=731
   Height=731
   Top=-10000
   Left=%ScreenX%
   Label=g,0,0
   Label=mjtnet ,135,5,True
   Label=mjtnet ,130,0,True
   Label= ,0,210,True
EndDialog>Dialog1
Show>dialog1

Dialog>Dialog3
   Caption=mjtnet logo background
   Width=731
   Height=731
   Top=-10000
   Left=%ScreenX3%
   Label=g,0,0
EndDialog>Dialog3
Show>Dialog1
Show>Dialog3

CloseDialog>dialog1
GoSub>BorderlessDialog,Dialog1
CloseDialog>dialog1
Show>dialog1
CloseDialog>dialog3
GoSub>BorderlessDialog,Dialog3
CloseDialog>dialog3
Show>dialog3

SetDialogObjectFont>Dialog1,mslabel1,webdings,548,0,234567
SetDialogObjectFont>Dialog1,mslabel3,Helvetica,1,2,6488835
SetDialogObjectFont>Dialog1,mslabel4,Arial,20,1,3289650
SetDialogObjectFont>Dialog3,mslabel1,webdings,548,0,234567

GoSub>TransparentDialog,Dialog1,234567
GoSub>TransparentDialog,Dialog3,234567
GetWindowPos>mjtnet logo,WinX,WinY

Let>kk=0
Let>kkk=20
Sub>ScreenY,160
Label>Loop
  GetDialogAction>dialog1,res1
  If>res1=2,EOF
  If>res1=3,EOF
  If>{(%kkk%<%ScreenY%)and(%kkk%<270)}
    Add>kk,10
    Add>kkk,20
    sub>winx,15
    SetDialogObjectFont>Dialog1,mslabel3,Helvetica,kk,2,6488835
    SetDialogObjectFont>Dialog1,mslabel2,Helvetica,kk,2,13290186
    MoveWindow>mjtnet logo,Winx,kkk
    LibFunc>user32,ShowScrollBar,sbres,dialog1.handle,3,0
    LibFunc>user32,ShowScrollBar,sbres,dialog1.handle,3,0
  Else
    GoSub>EOF
  EndIf
Goto>Loop

SRT>EOF
  MoveWindow>mjtnet logo background,Winx,kkk
  LibFunc>user32,ShowScrollBar,sbres,dialog3.handle,3,0
  LibFunc>user32,ShowScrollBar,sbres,dialog3.handle,3,0
  SetFocus>mjtnet logo
  //Include>%script_dir%\Draw Line Include.scp
  Let>kk=0
  Repeat>kk
    add>kk,1
    Random>690,Xpos
    Add>Xpos,20
    Random>220,Ypos
    Add>Ypos,20
  GoSub>DrawLine,dialog3.handle,40,16777215,xpos,ypos,xpos,ypos

  Until>kk=500
    Let>dialog1.mslabel4= Providing Automation and Scripting Tools Since 1997
    GetDialogAction>dialog1,res1
  Wait>6
  Exit>0
END>EOF

//Borderless Dialog usage
//GoSub>BorderlessDialog,DialogName
SRT>BorderlessDialog
  Let>dlg_hndl=%BorderlessDialog_var_1%.handle
  LibFunc>user32,SetWindowLongA,sres,dlg_hndl,-16,1073741824
END>BorderlessDialog

//DrawLine Usage:
//GoSub>DrawLine,WindowHandle,PenSize,PenColor,XStart,YStart,XEnd,YEnd

SRT>DrawLine
  LibFunc>user32,GetDC,HDC,%DrawLine_var_1%
  LibFunc>gdi32,CreatePen,Penres,0,%DrawLine_var_2%,%DrawLine_var_3%
  LibFunc>gdi32,SelectObject,SOPres,hdc,Penres
  Libfunc>gdi32,MoveToEx,mtres,HDC,%DrawLine_var_4%,%DrawLine_var_5%,0
  LibFunc>gdi32,LineTo,ltres,hdc,%DrawLine_var_6%,%DrawLine_var_7%
  LibFunc>gdi32,DeleteObject,DOres,Penres
END>DrawLine

//TransparentDialog usage
//GoSub>TransparentDialog,DialogName,TransparentColorNumber
SRT>TransparentDialog
Let>dlg_hndl=%TransparentDialog_var_1%.handle
//constants
  Let>GWL_EXSTYLE=-20
  Let>WS_EX_LAYERED=524288
  Let>LWA_COLORKEY=1
//get style attributes of dialog1 window
  LibFunc>user32,GetWindowLongA,attribs,dlg_hndl,GWL_EXSTYLE
  Let>attribs={%attribs% OR %WS_EX_LAYERED%}
//make selected color transparent
  LibFunc>user32,SetWindowLongA,swl,dlg_hndl,GWL_EXSTYLE,attribs
  LibFunc>user32,SetLayeredWindowAttributes,res,dlg_hndl,%TransparentDialog_var_2%,0,LWA_COLORKEY
END>TransparentDialog

User avatar
JRL
Automation Wizard
Posts: 3397
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Wed Jun 22, 2011 3:29 pm

Here's the original sample converted to post version 11 dialogs. No library functions required.

Code: Select all

Dialog>Dialog1
object Dialog1: TForm
  Left = 386
  Top = 201
  Caption = 'Transparent Color Demo'
  ClientHeight = 138
  ClientWidth = 562
  Position = poScreenCenter
  object Label1: TLabel
    Left = 0
    Top = 0
    Width = 137
    Height = 137
    AutoSize = False
    Caption = ''
  end
  object Label2: TLabel
    Left = 16
    Top = 48
    Width = 165
    Height = 26
    Caption = 'Press the button'#10#13'to make the green box transparent.'
    Transparent = True
  end
  object msButton1: tMSButton
    Left = 320
    Top = 4
    Width = 137
    Height = 80
    Caption = 'Make'#10#13'Transparent'
    DoubleBuffered = True
    ParentDoubleBuffered = False
    TabOrder = 0
    DoBrowse = False
    BrowseStyle = fbOpen
  end
end
EndDialog>Dialog1

//Set Label Color (color number 234567 is a shade of green)
//Be sure AutoSize is set to False and the Caption is blank
//In the dialog block
  SetDialogProperty>Dialog1,Label1,Color,234567
//Set Text Color (color number 0 is black)
  SetDialogObjectFont>Dialog1,Label2,Ariel,24,1,0
//If the "Make Transparent" button is pressed... Do It!
  AddDialogHandler>Dialog1,msButton1,OnClick,Process
//Open the dialog (normal appearance)
  Show>Dialog1,res1


SRT>Process
//make selected color transparent (color number 234567)
SetDialogProperty>Dialog1,,TransparentColor,True
SetDialogProperty>Dialog1,,TransparentColorValue,234567
//Make the button disappear
  SetDialogProperty>Dialog1,msButton1,Visible,False
//Show the dialog without text for a couple of seconds
  SetDialogProperty>Dialog1,Label2,Caption,
  Wait>2
//Put text back on the dialog.
  SetDialogProperty>Dialog1,Label2,Caption,There's a hole in this window%CRLF%%TAB%where a color used to be!
END>Process

User avatar
Grovkillen
Automation Wizard
Posts: 681
Joined: Fri Aug 10, 2012 2:38 pm
Location: Hudiksvall, Sweden
Contact:

Re: Make a specified color in a window transparent

Post by Grovkillen » Fri Dec 04, 2020 7:58 am

A note to self, here's how to hide everything except the actual HTML:

Code: Select all

Dialog>Dialog1
object Dialog1: TForm
  BorderStyle = bsNone
  ClientHeight = 150
  ClientWidth = 400
  Color = clBtnFace
  TransparentColor = True
  TransparentColorValue = clLime
  object MSHTMLViewer1: tMSHTMLViewer
    Left = 0
    Top = 0
    Height = 150
    Width = 400
    DefBackground = clLime
  end
end
EndDialog>Dialog1
LabelToVar>HTML_CODE,TEST,0,0,*/
SetDialogProperty>Dialog1,MSHTMLViewer1,HTML,TEST

Show>Dialog1,r

/*
HTML_CODE:
<img src="https://www.w3schools.com/html/pic_trulli.jpg" style="width:100px;height:120px;">
*/
Let>ME=%Script%

Running: 15.0.19b
version history

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts