Moving a Msgbox (revisited)

Get help with using AutoHotkey and its commands and hotkeys
ctfscripter
Posts: 4
Joined: 15 Feb 2016, 15:15

Moving a Msgbox (revisited)

23 Feb 2016, 13:33

So I found a post about how to move a msgbox and it apparently solved someone's problem.
But I can't get it to work. FanaticGuru has a lot of code in his function and I really don't understand why it is all there. If I call a simple function:

MoveMsgBox(WinName)
{
Sleep 1000
IfWinExist, %WinName%
{
WinMove, %WinName%,,%SetHi%, %SetWd%
}
}

IfWinExist is never validated and the WinMove code never executes. Is this something about the function I don't understand?
User avatar
Exaskryz
Posts: 2876
Joined: 17 Oct 2015, 20:28

Re: Moving a Msgbox (revisited)

23 Feb 2016, 14:18

Is this an AHK MsgBox?

How are you calling the function?

Seeing the code you execute would be a good idea. I'd also like to see what the original code was, because I don't know where the SetHi and SetWd values are coming from? I assume they're set somewhere outside the function as [super-global](https://autohotkey.com/docs/Functions.htm#Locals) variables?

Regardless, here's one way to move the MsgBox:

Code: Select all

^6::
SetTimer, MoveBox, -50
MsgBox,,MyTitle, Hello
return

MoveBox:
WinMove, MyTitle,, 400, 400, 500, 500
return
ctfscripter
Posts: 4
Joined: 15 Feb 2016, 15:15

Re: Moving a Msgbox (revisited)

23 Feb 2016, 19:37

So there is a fair amount of code in this whole process, but my problem is since this is being deployed on some tablets, the users are changing the screen % and although I can set the input popups to always be visible, if there is an error message displayed with Msgbox, it can end up off the screen or under the onscreen keyboard. I really wish you could control it's location when you call it. So first I declare these values (used for all input boxes)
......
SetTitleMatchMode, 2 ;Match any portion of the Window title string
SetWorkingDir, C:\DBA Help ;Used for ini file
SetHi=15
SetWd=200

Then when I check an Input box value one of the things I check and want to respond to:
..........................
If (Empnum = "")
{
WinName = Employee Number Missing
MssText = Please start again`nEnter an Employee Number
OnMessage(0x44, "MoveMsgBox")
MsgBox, 0, %WinName%,%MssText%
Exit
}


So OnMessage (which gets called with no problem) using my function
MoveMsgBox is:
.................................................
MoveMsgBox(WinName)
{
Sleep 1000
IfWinExist, %WinName%
{
WinMove, %WinName%,,%SetHi%, %SetWd%
}
}


This, I thought, is following the format of the SOLVED issue I found on this board about moving a Msgbox. But as I said, it doesn't work.
Does all that help?

Thanks for responding.
wizardzedd
Posts: 319
Joined: 23 Jan 2016, 23:03

Re: Moving a Msgbox (revisited)

23 Feb 2016, 19:42

SetHi and SetWd are being treated as local variables

Code: Select all

SetTitleMatchMode, 2 ;Match any portion of the Window title string
SetWorkingDir, C:\DBA Help ;Used for ini file
SetHi=15
SetWd=200

Then when I check an Input box value one of the things I check and want to respond to:
..........................
If (Empnum = "")
{
WinName = Employee Number Missing
MssText = Please start again`nEnter an Employee Number
OnMessage(0x44, "MoveMsgBox")
MsgBox, 0, %WinName%,%MssText%
Exit
}


So OnMessage (which gets called with no problem) using my function
MoveMsgBox is:
.................................................
MoveMsgBox(WinName)
{
global     ; ASSUME GLOBAL VARIABLES
Sleep 1000
IfWinExist, %WinName%
{
WinMove, %WinName%,,%SetHi%, %SetWd%
}
}
ctfscripter
Posts: 4
Joined: 15 Feb 2016, 15:15

Re: Moving a Msgbox (revisited)

24 Feb 2016, 16:02

wizardzedd,
I added the 'global' reference as you suggested. I didn't make any difference, the WinMove action still does not get executed.
wizardzedd
Posts: 319
Joined: 23 Jan 2016, 23:03

Re: Moving a Msgbox (revisited)

24 Feb 2016, 18:35

We fixed one problem with adding global, but the problem now seems to be with the message. I'm still not sure what the 0x44 message is... but it is called before the window exists. Here is a way that works by using a timer instead.

Code: Select all

SetHi=15
SetWd=200
 
WinName = Employee Number Missing
MssText = Please start again`nEnter an Employee Number
setTimer, MoveMsgBox, -50
MsgBox, 0, %WinName%,%MssText%
Exit
 
 
MoveMsgBox()
{
	global
	IfWinExist, %WinName%
		WinMove, %WinName%,,%SetHi%, %SetWd%
}
ctfscripter
Posts: 4
Joined: 15 Feb 2016, 15:15

Re: Moving a Msgbox (revisited)

24 Feb 2016, 20:56

What I understood is that OnMessage was a standard way to execute code when a message, like a Msgbox, popped up on the screen. Apparently 0x44 references the Msgbox type of popup. But since no one has come forward to comment on that I'll try the setTimer method. It will require dozens of setTimer calls because I need to setTimer off after every Msgbox appears. Thanks for your help.
FanaticGuru
Posts: 1356
Joined: 30 Sep 2013, 22:25

Re: Moving a Msgbox (revisited)

24 Feb 2016, 20:59

If you just want a function that puts msgbox dialogs at an X, Y when created using OnMessage:

Code: Select all

MsgBox_XY("Hello", 30,50)

MsgBox_XY(Message, X, Y)
{
	global MsgBox_X := X, MsgBox_Y := Y	; Make global for other function
	OnMessage(0x44, "Move_MsgBox")	; When a dialog appears run Move_MsgBox
	MsgBox, % Message
	OnMessage(0x44, "")				; When a dialog appears do nothing special
}

Move_MsgBox(P)
{
	global MsgBox_X, MsgBox_Y 		; Have to be global to get value set in other function
	if (P = 1027) 					; Make sure it is a AHK dialog
	{
		Process, Exist	; Get the Process PID into ErrorLevel
		DetectHiddenWindows, % (Setting_A_DetectHiddenWindows := A_DetectHiddenWindows) ? "On" :	; Round about way of changing DetectHiddenWindows settting and saving the setting all in one line
		if WinExist("ahk_class #32770 ahk_pid " ErrorLevel)	; Make sure dialog still exist
			WinMove, MsgBox_X, MsgBox_Y	; Move the default window from above WinExist
		DetectHiddenWindows, %Setting_A_DetectHiddenWindows%	; Set the DetectHiddenWindows setting back to what it was before function changed it
	}
}
With lots of comments.

This is kind of overkill for a simple move of one dialog but the power of the 0x44 method is that you can create a function that will figure out where to put an msgbox dialog when it is created.

It is like creating a special rule for where to put dialogs. In my example I turn on the special rule OnMessage(0x44, "Move_MsgBox") right before creating a msgbox and then turn it back off. But you could turn on the rule at the top of a script and never turn it off and then all dialogs could be moved based on code in the function called. The function code could be very sophisticated and always put the dialogs in the perfect place. Which sounds like what you need to do if you are trying to get dialogs to appear properly in different circumstances.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts

AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon

[Function] Timer - Create and Manage Timers
FanaticGuru
Posts: 1356
Joined: 30 Sep 2013, 22:25

Re: Moving a Msgbox (revisited)

24 Feb 2016, 21:15

For example you could put the below code in the top of everyone of your scripts and it would have very little effect on any MsgBox commands throughout the script UNLESS a Outlook email was open then it would center any MsgBox on the Outlook email.

Code: Select all

OnMessage(0x44, "Center_MsgBox_Email")
Center_MsgBox_Email(P)
{
	if (P = 1027) 
	{
		if WinExist("ahk_class rctrl_renwnd32")
		{
			WinGet, State, MinMax
			if !(State = -1)
			{
				WinGetPos, eX, eY, eW, eH
				Process, Exist
				DetectHiddenWindows, % (Setting_A_DetectHiddenWindows := A_DetectHiddenWindows) ? "On" :
				if WinExist("ahk_class #32770 ahk_pid " ErrorLevel)
				{
					WinGetPos,,,mW,mH
					WinMove, (eW-mW)/2 + eX, (eH-mH)/2 + eY
				}
				DetectHiddenWindows, %Setting_A_DetectHiddenWindows%
			}
		}
	}
}
Maybe you don't like the middle of the screen as the default.

Code: Select all

OnMessage(0x44, "Move_MsgBox")
Move_MsgBox(P)
{
	if (P = 1027)
	{
		Process, Exist
		DetectHiddenWindows, % (Setting_A_DetectHiddenWindows := A_DetectHiddenWindows) ? "On" :
		if WinExist("ahk_class #32770 ahk_pid " ErrorLevel)
			WinMove, 25, 25
		DetectHiddenWindows, %Setting_A_DetectHiddenWindows%
	}
}

MsgBox Put All MsgBox at 25,25
MsgBox Yea, All of them
All the MsgBox in this script will be at 25,25.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts

AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon

[Function] Timer - Create and Manage Timers

Return to “Ask For Help”

Who is online

Users browsing this forum: devnullsp, Dysphoria, sikongshan and 154 guests