Page 1 of 1

[SOLVED] MsgBox in center of active window?

Posted: 10 Apr 2015, 14:31
by Iomega0318
So I have a script to create an email in Outlook, then ask the user if they'd like to add an attachment.
How can I get the message box to open on top of the email itself wherever it is on the screen, as in my case my emails are loaded on my second monitor and the message box always opens on the default monitor which is my left screen. Likewise some people may have it create the email on their left monitor so I don't want it to just default to the right monitor, instead I would like a way to have it always open on top of the email. Is this even possible?

**EDITED FOR CLARITY**

Re: MsgBox in center of active window?

Posted: 10 Apr 2015, 15:03
by ahkcoder
The answer to this is easily found by reading the Help Files:

http://ahkscript.org/docs/commands/MsgBox.htm


Hint: System Modal

Re: MsgBox in center of active window?

Posted: 10 Apr 2015, 15:17
by Iomega0318
ahkcoder wrote:The answer to this is easily found by reading the Help Files:

http://ahkscript.org/docs/commands/MsgBox.htm


Hint: System Modal
Thank you, I know about that. However it is not what I wanted. I edited my post for clarity that was my mistake :)

Re: MsgBox in center of active window?

Posted: 10 Apr 2015, 16:56
by FanaticGuru
Iomega0318 wrote:So I have a script to create an email in Outlook, then ask the user if they'd like to add an attachment.
How can I get the message box to open on top of the email itself wherever it is on the screen, as in my case my emails are loaded on my second monitor and the message box always opens on the default monitor which is my left screen. Likewise some people may have it create the email on their left monitor so I don't want it to just default to the right monitor, instead I would like a way to have it always open on top of the email. Is this even possible?

**EDITED FOR CLARITY**
MsgBox is not really designed for positioning. Creating your own GUI allows for a lot more control.

But out of amusement here is a script to change the position where MsgBox will appear.

Code: Select all

F12::
	OnMessage(0x44, "Center_MsgBox_Email")
	MsgBox, 4096, Email Centering Script, Center of Email if exist and not minimized
	OnMessage(0x44,"")
	MsgBox, 4096, Email Centering Script, Back to default of center of screen
return

Center_MsgBox_Email(P)
{
	if (P = 1027) 
	{
		if WinExist("ahk_class rctrl_renwnd32") ; Window to Center In
		{
			WinGet, State, MinMax
			if !(State = -1)
			{
				WinGetPos, eX, eY, eW, eH
				Process, Exist
				DetectHiddenWindows, On
				if WinExist("ahk_class #32770 ahk_pid " ErrorLevel)
				{
					WinGetPos,,,mW,mH
					WinMove, (eW-mW)/2 + eX, (eH-mH)/2 + eY
				}
			}
		}
	}
}

Esc::ExitApp
This works for me on Windows 7 and Office 2010.

It should work on multiply monitors but I cannot test it.

It can easily be changed to center in any Window if you know the WinTitle to locate it.

FG

Re: MsgBox in center of active window?

Posted: 10 Apr 2015, 16:58
by fischgeek
Haha. You beat me to it Guru. Anyway, here's my implementation.

Code: Select all

Gui, +AlwaysOnTop +ToolWindow -Caption +Border
Gui, Color, White
Gui, Font, s11, Segoe UI
Gui, Add, Text,, % "Do you want to add an attachment?"
Gui, Add, Text ; spacer
Gui, Add, Button, Section w50 gYes, Yes
Gui, Add, Button, ys w50 gNo, No
return

F1:: 
{
	WinGetPos, mX, mY, mW, mH, Untitled - Message (HTML)
	Gui, Show, % "x" mX+(mW/2)-100 " y" mY+(mH/2)-50 " w300 h125"
	return
}

Yes:
{
	; attachment business here
	return
}

No:
{
	Gui, Hide
	; return
	ExitApp ; uncomment line above and comment this out to keep this active
}

Re: MsgBox in center of active window?

Posted: 10 Apr 2015, 17:05
by ameyrick

Code: Select all

attachment:
WinGetPos, outlookX, outlookY, outlookW, outlookH, Inbox
WinActivate, Inbox
SetTimer, CenterMsgBox, 10
Msgbox, 4, Add an attachment?, would you like to add an attachment?
IfMsgBox No
{
	SetTimer, CenterMsgBox, Off
    return
}
;...
return


CenterMsgBox:
IfWinExist, Add an attachment?
{
	WinGetPos, , , msg_W, msg_H, Add an attachment?
	SetTimer, CenterMsgBox, Off
	WinMove, Add an attachment?, , (outlookW/2+outlookX-(msg_W/2)), (outlookH/2+outlookY-(msg_H/2))
}
return 

Re: MsgBox in center of active window?

Posted: 13 Apr 2015, 10:03
by Iomega0318
You guys are awesome! and fast :)

I ended up going with FanaticGuru's example as I liked the way it still used the standard windows type message box, but fischgeek yours was nice as well! Other than that it works great and does exactly what I need. I think I am just about done with this script! Now I just have to decide which one of these to use lol

Code: Select all

    SendInput, {Alt}n
    SendInput, AF
    Sleep, 1000
    SendInput, M:\Folder To Open
    SendInput, {Enter}
-or-

Code: Select all

    Run, M:\Folder To Open