Page 1 of 2

Must be easy to set window on top.

Posted: 21 Oct 2021, 05:01
by ozgurerdogan
All I want, when I run calc.exe set its windows on top of all windows. But I could not do it. Can you please send me a sample?

Re: Must be easy to set window on top.

Posted: 21 Oct 2021, 05:30
by mikeyww

Code: Select all

#SingleInstance Force
Try Menu, Tray, Icon, %A_WinDir%\System32\calc.exe
SoundBeep, 1700
Loop {
 WinWait, Calculator ahk_exe ApplicationFrameHost.exe
 WinSet, AlwaysOnTop, On
 SoundBeep, 1500
 WinWaitClose
 SoundBeep, 1000
}

Re: Must be easy to set window on top.

Posted: 21 Oct 2021, 05:37
by ozgurerdogan
Sorry did not help.

Re: Must be easy to set window on top.

Posted: 21 Oct 2021, 06:20
by mikeyww
Exactly what happened when you ran the script, and then ran calc.exe?

What version of Windows do you have?

If you run Window Spy and then activate Calc, what is the complete WinTitle shown there? You can post a screenshot of Window Spy.

Re: Must be easy to set window on top.

Posted: 21 Oct 2021, 06:46
by ozgurerdogan
Nothing happened.

My calc path is: C:\Windows\System32\calc1.exe
ahk_class CalcFrame
ahk_exe calc1.exe
Version 1.1.33.02

Actually what I need is simple, no tray so sound is needed. When I manually run calc.exe with mouse click, then it should automaticly stay on top.

Re: Must be easy to set window on top.  Topic is solved

Posted: 21 Oct 2021, 07:17
by mikeyww

Code: Select all

#SingleInstance Force
Try Menu, Tray, Icon, %A_WinDir%\System32\calc1.exe
SoundBeep, 1700
Loop {
 WinWait, ahk_exe calc1.exe
 WinSet, AlwaysOnTop, On
 SoundBeep, 1500
 WinWaitClose
 SoundBeep, 1000
}
If you click on the commands in the posted script, you can learn more about how they work. After you get the script working, you can delete the sound commands.

Re: Must be easy to set window on top.

Posted: 21 Oct 2021, 07:30
by ozgurerdogan
Thank you man.

Re: Must be easy to set window on top.

Posted: 21 Oct 2021, 07:35
by ozgurerdogan
One more thing, I also have some other command in script file. If I put this in middle of script, it does not work, but when at top of script file, It runs fine. So what must I consider of when managing multible commands?

Re: Must be easy to set window on top.

Posted: 21 Oct 2021, 07:44
by ozgurerdogan
When I put this code at top of script file that also has some other command then it work but when at bottom or middle of file it does not work. So what must I watch for when using many commands at single script file?

Re: Must be easy to set window on top.

Posted: 21 Oct 2021, 07:45
by ozgurerdogan
Also how can I add second app to this loop?

Re: Must be easy to set window on top.

Posted: 21 Oct 2021, 08:17
by RussF
How do you have two or more apps always on top? Which one wins?

Re: Must be easy to set window on top.

Posted: 21 Oct 2021, 08:22
by ozgurerdogan
RussF wrote:
21 Oct 2021, 08:17
How do you have two or more apps always on top? Which one wins?
Not at same time. Might be notepad.exe, Calc.exe whichever runs.

Re: Must be easy to set window on top.

Posted: 21 Oct 2021, 08:32
by fenchai
RussF wrote:
21 Oct 2021, 08:17
How do you have two or more apps always on top? Which one wins?
they get a special layer, separated from the other windows which are not on "always on top", they fight for whichever has the current focus from your mouse.

Re: Must be easy to set window on top.

Posted: 21 Oct 2021, 08:38
by mikeyww
This section goes at the top.

Explained: Auto-execute

Re: Must be easy to set window on top.

Posted: 21 Oct 2021, 09:09
by ozgurerdogan
I think which one runs at last will be on top. But my request it not about it. I need to add more app to loop and if it is running, should be on top.

Re: Must be easy to set window on top.

Posted: 21 Oct 2021, 09:14
by mikeyww
You may want to use a shell hook to determine when a specific window appears. https://autohotkey.com/board/topic/32628-tool-shellhook-messages/

Re: Must be easy to set window on top.

Posted: 21 Oct 2021, 09:19
by ozgurerdogan
Can not I simply add more loop like:

Code: Select all

Loop {
WinWait, ahk_exe calc1.exe
WinSet, AlwaysOnTop, On
}

Loop {
WinWait, ahk_exe notepad.exe
WinSet, AlwaysOnTop, On
}

Re: Must be easy to set window on top.

Posted: 21 Oct 2021, 10:18
by mikeyww
What happened when you tried it?

Re: Must be easy to set window on top.

Posted: 21 Oct 2021, 10:23
by ozgurerdogan
notepad.exe does not come to on top but calc1.exe

Re: Must be easy to set window on top.

Posted: 21 Oct 2021, 10:31
by mikeyww
The first loop is infinite, so the second loop is never reached. To understand it, follow the script line by line, one line at a time. When the first closing brace is reached, the first loop repeats itself instead of proceeding to the next line.

Code: Select all

DllCall("RegisterShellHookWindow", "UInt", A_ScriptHwnd)
OnMessage(DllCall("RegisterWindowMessage", "Str", "SHELLHOOK"), "onTop")
Return

onTop(wParam, lParam) {
 If (wParam > 2)
  Return
 WinGet, pname, ProcessName, % wTitle := "ahk_id " lParam
 SplitPath, pname,,,, fnBare
 If fnBare not in calc1,calc,notepad,Calculator
  Return
 If (wParam = 1 && fnBare = "Calculator")
  Return
 SoundBeep, 1700
 WinSet, AlwaysOnTop, On, % fnBare = "Calculator" ? "Calc ahk_exe ApplicationFrameHost.exe" : wTitle
}