Moving MSGBOX to different monitor.

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
eblanc
Posts: 71
Joined: 08 May 2019, 14:41

Moving MSGBOX to different monitor.

Post by eblanc » 18 Sep 2019, 17:25

I have a couple of scripts. Evertime there is an Input box, it always pops up on my main monitor (center) I would like to move it to my Right side monitor.

Any ideas?

thanks in advance, you all sexy AHK coders.

User avatar
boiler
Posts: 16774
Joined: 21 Dec 2014, 02:44

Re: Moving MSGBOX to different monitor.

Post by boiler » 18 Sep 2019, 17:31

Your title says "MSGBOX" but your post says "Input box". If it's an InputBox, it's very easy to have it appear exactly where you want by specifying the x and y coordinates in the InputBox command:
InputBox, OutputVar , Title, Prompt, HIDE, Width, Height, X, Y, Font, Timeout, Default

A MsgBox can also be moved, although not as straightforward, but I can show you if you need to move a MsgBox as well.

User avatar
boiler
Posts: 16774
Joined: 21 Dec 2014, 02:44

Re: Moving MSGBOX to different monitor.

Post by boiler » 18 Sep 2019, 17:41

Here is a method for moving a MsgBox:

Code: Select all

SetTimer, MoveMsgBox, -50
MsgBox, I have been moved to 0,0
return

MoveMsgBox:
	WinWait, ahk_class #32770 ahk_exe AutoHotkey.exe
	WinMove, ahk_class #32770 ahk_exe AutoHotkey.exe,, 0, 0
return
It creates a second thread via SetTimer that will act after MsgBox appears. You can't simply put the WinMove immediately after the MsgBox command because it won't get to that line until the MsgBox is dismissed.

EnjoyRC
Posts: 2
Joined: 23 Mar 2017, 09:54

Re: Moving MSGBOX to different monitor.

Post by EnjoyRC » 24 May 2021, 08:33

I'm interested in any responses to the OP.
Can I display the InputBox on on my second monitor?
It defaults to the main monitor.

Maybe a way to have it displayed on the monitor where the mouse is currently?

User avatar
boiler
Posts: 16774
Joined: 21 Dec 2014, 02:44

Re: Moving MSGBOX to different monitor.

Post by boiler » 24 May 2021, 09:39

@EnjoyRC - Try the script below. I was only able to test it on a single-monitor setup, but I believe it should work.

Code: Select all

Name := InputBoxOnMouseMonitor("Welcome", "Please enter your name:")
MsgBox, % "Hello, " Name
return

InputBoxOnMouseMonitor(Title:="", Prompt:="", Hide:="", Width:="", Height:="", Font:="", Locale:="", Timeout:="", Default:="") {
	; centers InputBox on monitor where mouse is located
	; returns value entered by user
	CM := A_CoordModeMouse
	CoordMode, Mouse, Screen
	MouseGetPos, MX, MY
	CoordMode, Mouse, % CM
	SysGet, MonCount, MonitorCount
	loop, % MonCount {
		SysGet, Mon, Monitor, % A_Index
		if (MX >= MonLeft) && (MX <= MonRight) && (MY >= MonTop) && (MY <= MonBottom) {
			MonNum := A_Index
			break
		}
	}
	X := (MonRight - MonLeft) / 2 - (Width = "" ? 375 : Width) / 2
	Y := (MonBottom - MonTop) / 2 - (Height = "" ? 189 : Height) / 2
	InputBox, Output, % Title, % Prompt, % Hide, Width, Height, X, Y, % Locale, Timeout, % Default
	return Output
}

EnjoyRC
Posts: 2
Joined: 23 Mar 2017, 09:54

Re: Moving MSGBOX to different monitor.

Post by EnjoyRC » 24 May 2021, 14:06

I can see what you're trying to do here. But, it pops up on the main monitor regardless of where the mouse is. What a great start/idea you're engineering here. I'm gonna see if I can make it work too.

User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Moving MSGBOX to different monitor.

Post by FanaticGuru » 24 May 2021, 16:42

boiler wrote:
18 Sep 2019, 17:41
Here is a method for moving a MsgBox:

Code: Select all

SetTimer, MoveMsgBox, -50
MsgBox, I have been moved to 0,0
return

MoveMsgBox:
	WinWait, ahk_class #32770 ahk_exe AutoHotkey.exe
	WinMove, ahk_class #32770 ahk_exe AutoHotkey.exe,, 0, 0
return
It creates a second thread via SetTimer that will act after MsgBox appears. You can't simply put the WinMove immediately after the MsgBox command because it won't get to that line until the MsgBox is dismissed.

Just because I already have it.

Here is a cool (cool meaning complex and hard to understand) method of putting a MsgBox where you want it. It is like magic: most people don't understand how the trick is done, it just seems to happen.

Code: Select all

MsgBox, This MessageBox will appear in the centre of screen, as usual
MsgBox,, |x50 y70, This MessageBox will appear in coords x = 50 y = 70 without a title.
MsgBox,, Hello!|x500 y0, This MessageBox will appear in coords x = 500 y = 0 with the title "Hello!"
Return

CBTProc(nCode, wp, lp)  {
   static WH_CBT := 5, HCBT_CREATEWND := 3
        , dummy := OnExit( Func("CBTProc").Bind("OnExit") )
        , hHook := DllCall("SetWindowsHookEx", Int, WH_CBT
                                             , Ptr, RegisterCallback("CBTProc", "Fast")
                                             , Ptr, 0
                                             , UInt, DllCall("GetCurrentThreadId") , Ptr)
   if (nCode = "OnExit")  {
      DllCall("UnhookWindowsHookEx", Ptr, hHook)
      return
   }
   if (nCode = HCBT_CREATEWND)  {
      VarSetCapacity(WinClass, 256)
      DllCall("GetClassName", Ptr, hwnd := wp, Str, WinClass, Int, 256)
      if (WinClass = "#32770")  {
         pCREATESTRUCT := NumGet(lp+0)
         sTitle := StrGet( pTitle := NumGet(pCREATESTRUCT + A_PtrSize * 5 + 4 * 4), "UTF-16" )
         RegExMatch(sTitle, "^(.*)\|(?:x(\d+)\s?)?(?:y(\d+))?$", match)
         ( !(match2 = "" && match3 = "") && StrPut(match1, pTitle, "UTF-16") )
         ( match2 != "" && NumPut(match2, pCREATESTRUCT + A_PtrSize * 4 + 4 * 3, "Int") )
         ( match3 != "" && NumPut(match3, pCREATESTRUCT + A_PtrSize * 4 + 4 * 2, "Int") )
      }
   }
   Return DllCall("CallNextHookEx", Ptr, 0, Int, nCode, Ptr, wp, Ptr, lp)
}
You put this function anywhere in the script and suddenly MsgBox has a new parameter syntax. And you don't even have to ever call the function.

FG

Edit: Cool Magic MsgBox by @teadrinker here viewtopic.php?p=172566#p172566
Last edited by FanaticGuru on 12 Aug 2022, 17:11, edited 1 time in total.
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks

User avatar
boiler
Posts: 16774
Joined: 21 Dec 2014, 02:44

Re: Moving MSGBOX to different monitor.

Post by boiler » 24 May 2021, 16:58

FanaticGuru wrote: Here is a cool (cool meaning complex and hard to understand) method of putting a MsgBox where you want it. It is like magic: most people don't understand how the trick is done, it just seems to happen.
...
You put this function anywhere in the script and suddenly MsgBox has a new parameter syntax. And you don't even have to ever call the function.
That is cool. Thanks!

b0dhimind
Posts: 18
Joined: 20 May 2015, 11:41

Re: Moving MSGBOX to different monitor.

Post by b0dhimind » 11 Aug 2022, 23:06

I suggest using the self-deleting timer object method Lexikos described here:
viewtopic.php?t=13010#p66731

It's v2 friendly and useful with many other scenarios.
I used this to move MsgBox around as in:

Code: Select all

; MASTER OBJECT https://www.autohotkey.com/boards/viewtopic.php?t=13010#p66731
class SelfDeletingTimer {
    __New(period, fn, prms*) {
        this.fn := IsObject(fn) ? fn : Func(fn)
        this.prms := prms
        SetTimer % this, % period
    }
    Call() {
        this.fn.Call(this.prms*)
        SetTimer % this, Delete
    }
}

;WINDOW MOVE FUNCTION
windowMoveFunction(windowname,windowx,windowy,windowl,windowh){
	WinMove, %windowname%,,%windowx%,%windowy%,%windowl%,%windowh%
}

F10::
new SelfDeletingTimer(-100, "windowMoveFunction", "A","100","300","","")
MsgBox

User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Moving MSGBOX to different monitor.

Post by FanaticGuru » 12 Aug 2022, 17:05

b0dhimind wrote:
11 Aug 2022, 23:06
I suggest using the self-deleting timer object method Lexikos described here:
viewtopic.php?t=13010#p66731

It's v2 friendly and useful with many other scenarios.
I used this to move MsgBox around as in:

Code: Select all

; MASTER OBJECT https://www.autohotkey.com/boards/viewtopic.php?t=13010#p66731
class SelfDeletingTimer {
    __New(period, fn, prms*) {
        this.fn := IsObject(fn) ? fn : Func(fn)
        this.prms := prms
        SetTimer % this, % period
    }
    Call() {
        this.fn.Call(this.prms*)
        SetTimer % this, Delete
    }
}

;WINDOW MOVE FUNCTION
windowMoveFunction(windowname,windowx,windowy,windowl,windowh){
	WinMove, %windowname%,,%windowx%,%windowy%,%windowl%,%windowh%
}

F10::
new SelfDeletingTimer(-100, "windowMoveFunction", "A","100","300","","")
MsgBox

A fancier SetTimer and Move that is an interesting technique but far from cool magic.

Cool magic is mind controlling AutoHotkey so that it puts the MsgBox where you want in the first place. No moving, just creating a MsgBox at a location without any visual ugliness.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks

Post Reply

Return to “Ask for Help (v1)”