Window always on top

General Macro Scheduler discussion

Moderators: Dorian (MJT support), JRL

Post Reply
Dimmak
Junior Coder
Posts: 25
Joined: Tue Jan 24, 2012 4:57 pm

Window always on top

Post by Dimmak » Tue Jan 24, 2012 5:07 pm

Please Help!
How to enable always on top(topmost) for any window and disable that??

User avatar
Rain
Automation Wizard
Posts: 550
Joined: Tue Aug 09, 2005 5:02 pm
Contact:

Post by Rain » Tue Jan 24, 2012 8:29 pm

I started this script a while ago and never got a round to finishing it. It does what you are looking for. Simply follow the instructions in the dialog.

[code]
//Set IGNORESPACES to 1 to force script interpreter to ignore spaces.
//If using IGNORESPACES quote strings in {" ... "}
//Let>IGNORESPACES=1


Dialog>Dialog1
object Dialog1: TForm
Left = 570
Top = 165
HelpContext = 5000
AutoSize = True
BorderIcons = [biSystemMenu, biMinimize, biHelp]
BorderStyle = bsSingle
BorderWidth = 5
Caption = 'Always On Top App'
ClientHeight = 212
ClientWidth = 391
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = [fsBold]
OldCreateOrder = True
Position = poDesktopCenter
ShowHint = True
OnTaskBar = True
PixelsPerInch = 96
TextHeight = 13
object Label1: TLabel
Left = 0
Top = 160
Width = 377
Height = 52
Alignment = taCenter
Caption =
'To make a window always stay on top, select the window from the ' +
'list above, check Always On Top and click the Set button. To res' +
'tore the window to its original state, select the window from th' +
'e list, uncheck Always Stay On Top and click Set.'
WordWrap = True
end
object MSListBox1: tMSListBox
Left = 0
Top = 0
Width = 391
Height = 97
ParentCustomHint = False
Align = alTop
BevelInner = bvNone
BevelOuter = bvNone
BevelWidth = 2
BiDiMode = bdLeftToRight
BorderStyle = bsNone
Color = clWhite
Ctl3D = True
DoubleBuffered = False
Font.Charset = ANSI_CHARSET
Font.Color = clWindowText
Font.Height = -13
Font.Name = 'Arial'
Font.Style = [fsBold]
ItemHeight = 16
ParentBiDiMode = False
ParentCtl3D = False
ParentDoubleBuffered = False
ParentFont = False
ParentShowHint = False
ScrollWidth = 2500
ShowHint = True
Sorted = True
TabOrder = 0
TabWidth = 100
SelectedIndex = -1
end
object CheckBox1: TCheckBox
Left = 0
Top = 104
Width = 105
Height = 17
Caption = 'Always On Top'
TabOrder = 1
end
object MSButton1: tMSButton
Left = 0
Top = 128
Width = 75
Height = 25
Caption = 'Apply'
DoubleBuffered = True
ParentDoubleBuffered = False
TabOrder = 2
DoBrowse = False
BrowseStyle = fbOpen
end
object MSButton2: tMSButton
Left = 80
Top = 128
Width = 75
Height = 25
Caption = 'Refresh'
DoubleBuffered = True
ParentDoubleBuffered = False
TabOrder = 3
DoBrowse = False
BrowseStyle = fbOpen
end
end
EndDialog>Dialog1


AddDialogHandler>Dialog1,MSButton1,OnClick,SetWindowPosition
AddDialogHandler>Dialog1,MSButton2,OnClick,GetWindowsList
AddDialogHandler>Dialog1,,OnClose,Exit
SetDialogProperty>Dialog1,,FormStyle,fsStayOnTop
Show>Dialog1
SetDialogProperty>Dialog1,,AutoSize,False

Gosub>GetWindowsList
Label>Loop
Wait>.1
Goto>Loop



SRT>GetWindowsList
Let>WIN_USEHANDLE=1
GetWindowList>allWindows
Separate>allWindows,CRLF,wins
If>wins_countk=0
Label>GetFirstWindowInfo
Add>k,1
Let>this_window=wins_%k%
GetWindowNames>this_window,strTitle,strClass
If>%strClass%=,GetFirstWindowInfo
Let>Result=%strTitle%
Repeat>k
Add>k,1
Let>this_window=wins_%k%
GetWindowNames>this_window,strTitle,strClass
If>%strTitle%=Program Manager,EndOfRepeat
If>%strClass%=
Else
Let>Result=%Result%%CRLF%%strTitle%
Endif
Label>EndOfRepeat
Until>k=wins_count

Let>WIN_USEHANDLE=0

Label>EndGetWindowsList
SetDialogProperty>Dialog1,MSListBox1,Text,%Result%
END>GetWindowsList


SRT>SetWindowPosition
Let>WIN_USEHANDLE=1
GetDialogProperty>Dialog1,MSListBox1,SelectedItems,TheSelectedWindow
GetDialogProperty>Dialog1,CheckBox1,Checked,AlwaysOnTop
GetDialogProperty>Dialog1,MSComboBox1,SelectedItems,OpacityPercent
GetWindowList>allWindows
Separate>allWindows,CRLF,wins
Let>k=0
Repeat>k
Add>k,1
Let>this_window=wins_%k%
GetWindowNames>this_window,strTitle,strClass
If>%strTitle%=%TheSelectedWindow%
If>AlwaysOnTop=True
LibFunc>user32,FindWindowA,dhwnd,strClass,strTitle
LibFunc>User32,SetWindowPos,swpr,dhwnd,-1,0,0,0,0,83
Else
LibFunc>user32,FindWindowA,dhwnd,strClass,strTitle
LibFunc>User32,SetWindowPos,swpr,dhwnd,1,0,0,0,0,83
Endif

Endif
Until>k=wins_count

Let>WIN_USEHANDLE=0

Wait>.5
END>SetWindowPosition




SRT>Exit
Exit>1
END>Exit

[/code]

Dimmak
Junior Coder
Posts: 25
Joined: Tue Jan 24, 2012 4:57 pm

Post by Dimmak » Tue Jan 24, 2012 8:52 pm

GREAT RAIN!!!
THANX!!! :)

garycee
Newbie
Posts: 17
Joined: Mon Jul 20, 2015 7:17 pm

Re: Window always on top

Post by garycee » Fri Jul 07, 2017 1:18 am

This is awesome!

One question if I may, how to hard code the Window into it when one already knows the name of the window, please?

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

Re: Window always on top

Post by JRL » Fri Jul 07, 2017 4:00 pm

Here's an example using mspaint.

Code: Select all

OnEvent>key_down,VK27,0,Quit

SRT>Quit
  Exit>0
END>Quit

RunProgram>mspaint.exe
WaitWindowOpen>paint*

Wait>1

GetWindowHandle>paint*,hwnd

Label>Loop
  Wait>0.01
  GoSub>StayOnTop,hwnd
Goto>Loop

//Usage:
//GoSub>StayOnTop,WindowHandle
SRT>StayOnTop
  Let>HWND_TOPMOST=-1
  Let>SWP_SHOWWINDOW=64
  LibFunc>User32,SetWindowPos,SWPres,%StayOnTop_var_1%,HWND_TOPMOST,100,100,500,300,SWP_SHOWWINDOW
END>StayOnTop

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