Is it possible to determine which monitor a scripts GUI is open in within ahk?
I have a gui I am putting together that is movable, and opens another gui that will anchor itself to the first, but I need to determine the monitor it is within, and anchor it so that this second window remains within the borders of that monitor, and also changes the anchor position to be above or below the first gui, again by placement within that monitor. If there is room beneath the first, then place it beneath, otherwise place it above.
To do this however, I need to figure out how to determine what monitor the first gui is active within.
Can anyone prove sample code if this is possible?
Thanks Much!
[SOLVED] Code to determine what monitor active gui is in?
[SOLVED] Code to determine what monitor active gui is in?
Last edited by jsmain on 26 Nov 2014, 11:55, edited 1 time in total.
Re: Code to determine what monitor the active gui is within?
Edit:
SysGet, MonitorCount to get the number of monitors. If the GUI is between x/y and that x/y it's on monitor z. Then you can use WorkArea to determine where the other GUI appears.
SysGet, MonitorCount to get the number of monitors. If the GUI is between x/y and that x/y it's on monitor z. Then you can use WorkArea to determine where the other GUI appears.
Re: Code to determine what monitor the active gui is within?
You might try MDMF.
Re: Code to determine what monitor the active gui is within?
Thanks Bkid. I already knew how to get the display count. Was hoping there was something a variable I was overlooking that would tell me which monitor the script was operating in. I don't see one. Too bad it wasn't that simple.
just me, thanks to you too! I'll check it out further, but again, I was hoping for a much simpler solution.
just me, thanks to you too! I'll check it out further, but again, I was hoping for a much simpler solution.
Re: Code to determine what monitor the active gui is within?
Here's my working example, just because I was bored.
Maybe you can pick useful pieces from it.
Maybe you can pick useful pieces from it.
Code: Select all
PID := DllCall("GetCurrentProcessId")
SysGet, NumMons, MonitorCount
Gui, Add, Button, gSecondGui, Display 2nd GUI
Gui, Show, h300 w300, Second Gui
Return
SecondGui:
Loop, % NumMons {
SysGet, Mon%a_index%, MonitorWorkArea, %a_index%
WinGetPos, x, y, w, h, ahk_pid %PID%
If (x > Mon%a_index%Left && x < Mon%a_index%Right && y > Mon%a_index%Top && y < Mon%a_index%Bottom)
M := A_Index
}
;This example assumes the 2nd GUI is also h100 w300
If(y+h+100 > Mon%M%Bottom) {
y2 := y-140
Gui, 2:Show, h100 w300 x%x% y%y2%, AboveWindow
} Else {
y2 := y+340
Gui, 2:Show, h100 w300 x%x% y%y2%, BelowWindow
}
Return
GuiClose:
ExitApp
Re: Code to determine what monitor the active gui is within?
Thanks again Bkid!
A little more than what I was hoping for, but end result is acceptable.
Here is my modification/solution to the problem
A little more than what I was hoping for, but end result is acceptable.
Here is my modification/solution to the problem
Code: Select all
#SingleInstance,force
PID := DllCall("GetCurrentProcessId")
SysGet, NumMons, MonitorCount
Gui, Main: Margin, -5,-5
Gui, Main: +Toolwindow +AlwaysOnTop
Gui, Main: Add, Button, h40 w200 gGUI2, Display 2nd GUI
Gui, Main: Show, , Main Gui
Return
GUI2:
H2 := 200
W2 := 400
Loop, % NumMons {
SysGet, Mon%a_index%, MonitorWorkArea, %a_index%
WinGetPos, mainX, mainY, mainW, mainH, ahk_pid %PID%
If (mainX > Mon%a_index%Left && mainX < Mon%a_index%Right && mainY > Mon%a_index%Top && mainY < Mon%a_index%Bottom)
Mon := A_Index
}
Gui, 2: Destroy
Gui, 2: +Toolwindow +AlwaysOnTop -caption border
Gui, 2: Color, silver, white
Gui, 2: Add, text,, 2nd GUI
x2 := mainX-5 ;default offset
If(mainY+mainH+H2 > Mon%Mon%Bottom)
y2 := mainY-205
Else
y2 := mainY+58
; Check that GUI2 will show entirely on the chosen display, offsetting if not
If(mainX+W2 > Mon%Mon%Right)
x2 := Mon%Mon%Right - W2
Gui, 2:Show, h%H2% w%W2% x%x2% y%y2%, 2nd GUI
Return
MainGuiClose:
ExitApp