Reposition a MsgBox? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
shipaddicted
Posts: 108
Joined: 15 Jul 2016, 19:57

Reposition a MsgBox?

Post by shipaddicted » 21 Apr 2024, 21:44

I have created a script that shows each option in a MsgBox as it scrolls through the page, asking me if I want to select that option (which then opens that option in a new tab and then goes back to the original page). So far, I love it except for one thing -- the MsgBox pops up right over the "meat" of the webpage that it's asking me about.

Is there any way to reposition the MsgBox so that it pops up in a different section of the window? I know I can position a GUI wherever I want it, but I don't see any way to do the same with a MsgBox.

This is what I have so far, that doesn't work (maybe because I positioned the winmove stuff in the wrong place??)

Code: Select all

!q::
{
	cUIA:=UIA_Browser("ahk_exe chrome.exe")
	wdwHdl := WinGetTitle("A")
	tabName := RegExReplace(wdwHdl, " - Google Chrome")
	x := cUIA.GetCurrentDocumentElement() 
	y := cUIA	.GetAllLinks() 
	Loop y.Length 
	{
		If (A_Index > 9)
		{
			If (A_Index = 50)
			{
				Break 
			}
			val := Mod(A_Index, 5)
			If (val = "0")
			{
				y[A_Index].ScrollIntoView()
				Result := MsgBox(y[A_Index].Name "`n `n Navigate to this page? (press Yes or No)","Want this one?", "YesNo Icon? Default2")
				WinWaitActive("Want this one?")
				WinMove(1090, 507,,,"Want this one?")
				If Result = "Yes"
				{
					cUIA.NewTab()
					cUIA.WaitPageLoad("New Tab")
					cUIA.Navigate(y[A_Index].Value)
					cUIA.SelectTab(tabName, 2, 0)
				}
				If Result = "No"
					Continue 			
			}
		}
	}
}

Draken
Posts: 58
Joined: 08 Dec 2022, 01:19

Re: Reposition a MsgBox?

Post by Draken » 22 Apr 2024, 00:48

Hello, the MsgBox script waits for you to close it and you can't move the window using winmove, but you can work around it.

Code: Select all

#Requires AutoHotkey 2.0

SetTimer MoveMsgBox, 20

MsgBox("I'm not in the usual place!", "Moved Box")
sleep 500
MsgBox("I'm not in the usual place!", "Moved Box")
return

MoveMsgBox(){
    if WinExist("Moved Box")
        WinMove(0, 0, , , "Moved Box")
}
original script for V1
viewtopic.php?style=1&t=111035

shipaddicted
Posts: 108
Joined: 15 Jul 2016, 19:57

Re: Reposition a MsgBox?

Post by shipaddicted » 23 Apr 2024, 18:54

Draken wrote:
22 Apr 2024, 00:48
Hello, the MsgBox script waits for you to close it and you can't move the window using winmove, but you can work around it.

Interesting! Thank you!

william_ahk
Posts: 501
Joined: 03 Dec 2018, 20:02

Re: Reposition a MsgBox?  Topic is solved

Post by william_ahk » 24 Apr 2024, 21:30

This method is more reliable

Code: Select all

OnMessage(0x44, CB := (*)=>(DetectHiddenWindows(True), WinExist("ahk_class #32770 ahk_pid " ProcessExist()) && WinMove(A_ScreenWidth-300)))
MsgBox("Right")
OnMessage(0x44, CB, 0)
MsgBox("Normal")

Draken
Posts: 58
Joined: 08 Dec 2022, 01:19

Re: Reposition a MsgBox?

Post by Draken » 25 Apr 2024, 01:10

@william_ahk
Hello, this is a very elegant solution, I would have a question how to find out the message number to track and why it is before WinMove && (AND?)? If I replace the && with a comma it also works, but I assume it has some meaning. Thank you.

william_ahk
Posts: 501
Joined: 03 Dec 2018, 20:02

Re: Reposition a MsgBox?

Post by william_ahk » 25 Apr 2024, 03:14

@Draken I didn't discover this message code. It was well established in v1 in this thread, I just translated it into v2. It appears to be WM_COMMNOTIFY according to this table https://www.autoitscript.com/autoit3/docs/appendix/WinMsgCodes.htm but I have no idea what that means. :D

The AND operator was to ensure WinExist is true. It's just a one-liner expressionist way to write If WinExist Then WinMove.
Here's the same callback function written in declaration syntax:

Code: Select all

OnMessage(0x44, CB)
MsgBox("Right")
OnMessage(0x44, CB, 0)
MsgBox("Normal")

CB(*) {
	DetectHiddenWindows(True)
	If WinExist("ahk_class #32770 ahk_pid " ProcessExist())
		WinMove(A_ScreenWidth-300)
}
Here's an encapsulated function for easier use:

Code: Select all

MsgBoxSetPos(300)
MsgBox("Left")
MsgBoxSetPos(A_ScreenWidth-300)
MsgBox("Right")
MsgBoxSetPos()
MsgBox("Normal")

MsgBoxSetPos(X?, Y?) {
	Static CB
	If IsSet(CB)
		OnMessage(0x44, CB, 0)
	If !(IsSet(X) || IsSet(Y))
		Return
	OnMessage(0x44, CB:=WM_COMMNOTIFY)
	WM_COMMNOTIFY(*) {
		DetectHiddenWindows(True)
		If WinExist("ahk_class #32770 ahk_pid " ProcessExist())
			WinMove(X?, Y?)
	}
}

Post Reply

Return to “Ask for Help (v2)”