[SOLVED] MsgBox in center of active window?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Iomega0318
Posts: 83
Joined: 06 Apr 2015, 14:56
Location: Wolfforth, TX
Contact:

[SOLVED] MsgBox in center of active window?

10 Apr 2015, 14:31

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**
Last edited by Iomega0318 on 13 Apr 2015, 12:50, edited 2 times in total.
ahkcoder
Posts: 2
Joined: 08 Apr 2015, 17:45

Re: MsgBox in center of active window?

10 Apr 2015, 15:03

The answer to this is easily found by reading the Help Files:

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


Hint: System Modal
User avatar
Iomega0318
Posts: 83
Joined: 06 Apr 2015, 14:56
Location: Wolfforth, TX
Contact:

Re: MsgBox in center of active window?

10 Apr 2015, 15:17

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 :)
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: MsgBox in center of active window?

10 Apr 2015, 16:56

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
Last edited by FanaticGuru on 10 Apr 2015, 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
fischgeek
Posts: 435
Joined: 29 Jan 2014, 21:39

Re: MsgBox in center of active window?

10 Apr 2015, 16:58

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
}
ameyrick
Posts: 122
Joined: 20 Apr 2014, 18:12

Re: MsgBox in center of active window?

10 Apr 2015, 17:05

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 
User avatar
Iomega0318
Posts: 83
Joined: 06 Apr 2015, 14:56
Location: Wolfforth, TX
Contact:

Re: MsgBox in center of active window?

13 Apr 2015, 10:03

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

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 109 guests