[SOLVED] Code to determine what monitor active gui is in?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
jsmain
Posts: 62
Joined: 07 Feb 2014, 08:21

[SOLVED] Code to determine what monitor active gui is in?

Post by jsmain » 25 Nov 2014, 15:06

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!
Last edited by jsmain on 26 Nov 2014, 11:55, edited 1 time in total.

Bkid
Posts: 31
Joined: 13 Jun 2014, 12:19

Re: Code to determine what monitor the active gui is within?

Post by Bkid » 25 Nov 2014, 15:20

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.

just me
Posts: 9574
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Code to determine what monitor the active gui is within?

Post by just me » 25 Nov 2014, 15:29

You might try MDMF.

jsmain
Posts: 62
Joined: 07 Feb 2014, 08:21

Re: Code to determine what monitor the active gui is within?

Post by jsmain » 25 Nov 2014, 16:24

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.

Bkid
Posts: 31
Joined: 13 Jun 2014, 12:19

Re: Code to determine what monitor the active gui is within?

Post by Bkid » 25 Nov 2014, 17:52

Here's my working example, just because I was bored.
Maybe you can pick useful pieces from it. :P

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

jsmain
Posts: 62
Joined: 07 Feb 2014, 08:21

Re: Code to determine what monitor the active gui is within?

Post by jsmain » 26 Nov 2014, 11:53

:superhappy: 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

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

Post Reply

Return to “Ask for Help (v1)”