MsgBox with custom icon (from resource)

Post your working scripts, libraries and tools
just me
Posts: 6464
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: MsgBox with custom icon (from resource)

05 Apr 2017, 03:38

From the AHK source code:

Code: Select all

// Since WM_COMMNOTIFY is never generated by the Win32 API, and since we want AHK_USER_MENU to be
// an ID less than WM_HOTKEY so that it doesn't get filtered out when the script is uninterruptible,
// the following trick is used to map our user-defined messages onto WM_COMMNOTIFY by sacrificing the
// wParam part of the message (using it as an indicator of what the message really is).
...
// POST_AHK_DIALOG:
// Post a special msg that will attempt to force it to the foreground after it has been displayed,
// since the dialog often will flash in the task bar instead of becoming foreground.
// It's enough just to queue up a single message that dialog's message pump will forward to our
// main window proc once the dialog window has been displayed.  This avoids the overhead of creating
// and destroying the timer (although the timer may be needed anyway if any timed subroutines are
// enabled).  My only concern about this is that on some OS's, or on slower CPUs, the message may be
// received too soon (before the dialog window actually exists) resulting in our window proc not
// being able to ensure that it's the foreground window.  That seems unlikely, however, since
// MessageBox() and the other dialog invocating API calls (for FileSelectFile/Folder) likely
// ensures its window really exists before dispatching messages.
#define POST_AHK_DIALOG(timeout) PostMessage(g_hWnd, WM_COMMNOTIFY, AHK_DIALOG, (LPARAM)timeout);
Helgef
Posts: 3892
Joined: 17 Jul 2016, 01:02
Contact:

Re: MsgBox with custom icon (from resource)

05 Apr 2017, 03:59

just me wrote:From the AHK source code:
Interesting, so the 0x44 msg really comes from ahk it self, to it self, when creating a msgbox. :crazy:
lexikos
Posts: 6488
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: MsgBox with custom icon (from resource)

05 Apr 2017, 04:03

If you don't want to rely on this undocumented detail (or you are calling MessageBox() directly), you can just use the same technique: post yourself a message. I think that it tends to be processed before the window becomes visible, unlike a timer.
User avatar
jNizM
Posts: 2451
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: MsgBox with custom icon (from resource)

05 Apr 2017, 04:50

0x44 does work with AHK-MsgBox but not with MessageBox() function

Code: Select all

; GLOBAL SETTINGS ===============================================================================================================

#NoEnv
#SingleInstance Force
SetBatchLines -1

; SCRIPT ========================================================================================================================

OnMessage(0x0044, "OnMsgBox")
MsgBox % "text"
OnMessage(0x0044, "")


OnMessage(0x0044, "OnMsgBox")
DllCall("user32\MessageBox", "ptr", 0, "str", "text", "str", "title", "uint", 0)
OnMessage(0x0044, "")

; FUNCTIONS =====================================================================================================================

OnMsgBox(param1, param2, param3, param4)
{
    MsgBox % param1 " | " param2 " | " param3 " | " param4
}
[AHK] 1.1.30.03 x64 Unicode | [WIN] 10 Pro (Version 1903) x64 | [GitHub] Profile
Donations are appreciated if I could help you
lexikos
Posts: 6488
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: MsgBox with custom icon (from resource)

05 Apr 2017, 05:02

jNizM, didn't you read the previous posts?

The message is sent by the MsgBox command. Of course it won't work with MessageBox(), unless you post the message yourself.
User avatar
jNizM
Posts: 2451
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: MsgBox with custom icon (from resource)

05 Apr 2017, 05:42

Nope, because I did not reload the tab before I hit send ^^
[AHK] 1.1.30.03 x64 Unicode | [WIN] 10 Pro (Version 1903) x64 | [GitHub] Profile
Donations are appreciated if I could help you
Helgef
Posts: 3892
Joined: 17 Jul 2016, 01:02
Contact:

Re: MsgBox with custom icon (from resource)

05 Apr 2017, 06:18

I would never have tried this without the preceding conversation, :)

Code: Select all

DetectHiddenWindows, On
OnMessage(0x5555, "OnMsgBox")
timeout:=2000
PostMessage, 0x5555 , timeout,,, % "ahk_id " A_ScriptHwnd
DllCall("user32\MessageBox", "ptr", 0, "str", "text", "str", "title", "uint", 0x80)
OnMessage(0x5555, "")

OnMsgBox(timeout){
    DetectHiddenWindows, On
    Process, Exist
    If (lf:=WinExist("ahk_class #32770 ahk_pid " . ErrorLevel)) {
        hIcon := LoadPicture("explorer.exe", "w32 Icon22", _)
        SendMessage 0x172, 1, %hIcon% , Static1 ; STM_SETIMAGE
    }
	if (timeout) {
		fn:=Func("WinClose").Bind(lf)
		SetTimer, % fn, % -timeout
	}
}
WinClose(hWnd){
	WinClose, % "ahk_id " hWnd
	return
}
User avatar
jNizM
Posts: 2451
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: MsgBox with custom icon (from resource)

05 Apr 2017, 06:24

Thx helgef
My (rewritten) example

Code: Select all

; GLOBAL SETTINGS ===============================================================================================================

#NoEnv
#SingleInstance Force
SetBatchLines -1

; SCRIPT ========================================================================================================================

OnMessage(0x0044, "OnMsgBox")
MsgBox % "text"
OnMessage(0x0044, "")


OnMessage(0x4444, "OnMessageBox")
DllCall("user32\PostMessage", "ptr", A_ScriptHwnd, "uint", 0x4444, "ptr", 1337, "ptr", 0)
DllCall("user32\MessageBox", "ptr", 0, "str", "text", "str", "title", "uint", 0)
OnMessage(0x4444, "")

; FUNCTIONS =====================================================================================================================

OnMsgBox(wParam, lParam, msg, hWnd)
{
    MsgBox % wParam " | " lParam " | " msg " | " hWnd
}

OnMessageBox(wParam, lParam, msg, hWnd)
{
    MsgBox % wParam " | " lParam " | " msg " | " hWnd
}
To get the MessageBox handle you can also use this (SetWindowsHookEx / CallWndRetProc) instant of match 1 of many #32770 windows
(Disable Moving a MsgBox)
Example:

Code: Select all

; GLOBAL SETTINGS ===============================================================================================================

#NoEnv
#SingleInstance Force
SetBatchLines -1

; GUI ===========================================================================================================================

Gui, +hWndhMyGUI
Gui, Margin, 10, 10
Gui, Add, Button, xm-1 ym w402 h200 gBUTTON_MSGBOX, % "Click me!"
Gui, Add, Edit, xm y+9 w400 0x801 vMyEdit
Gui, Show, AutoSize
GuiDisableMove(hMyGUI)
GuiDisableCloseButton(hMyGUI)
return

; SCRIPT ========================================================================================================================

BUTTON_MSGBOX:
    ret := MessageBox(hMyGUI, "Random Title", "YES or NO?", 0x24)
    GuiControl,, MyEdit, % (ret = 7) ? "NO!" : "YES!"
return

; FUNCTIONS =====================================================================================================================

MessageBox(handle, title, text, options)
{
    WndHook := DllCall("SetWindowsHookEx", "int", 12, "ptr", RegisterCallback("CallWndRetProc", ""), "ptr", 0, "uint", DllCall("GetCurrentThreadId"), "ptr") ; WH_CALLWNDPROCRET := 12
    ret := DllCall("user32\MessageBox", "ptr", handle, "str", text, "str", title, "uint", options)
    if (WndHook)
        DllCall("UnhookWindowsHookEx", "ptr", WndHook)
    return ret
}

GuiDisableMove(handle)
{
    hMenu := DllCall("user32\GetSystemMenu", "ptr", handle, "int", false, "ptr")
    DllCall("user32\RemoveMenu", "ptr", hMenu, "uint", 0xf010, "uint", 0)
    return DllCall("user32\DrawMenuBar", "ptr", handle)
}

GuiDisableCloseButton(handle)
{
    hMenu := DllCall("user32\GetSystemMenu", "ptr", handle, "int", false, "ptr")
    DllCall("user32\EnableMenuItem", "ptr", hMenu, "uint", 0xf060, "uint", 0x3)
    return DllCall("user32\DrawMenuBar", "ptr", handle)
}

; CALLBACKS =====================================================================================================================

CallWndRetProc(code, wParam, lParam)
{
    ;critical 1000
    if (code >= 0) { ; code >= HC_ACTION := 0
        if (NumGet(lParam+0, A_PtrSize * 3, "uint") = 0x0110) { ; cwpmessage = WM_INITDIALOG := 0x0110
            cwplparam := NumGet(lParam+0, A_PtrSize, "ptr"), cwphwnd := NumGet(lParam+0, A_PtrSize * 4, "ptr")
            if (cwplparam && DllCall("GetWindowLong" (A_PtrSize = 8 ? "Ptr" : ""), "ptr", cwphwnd, "int", -21, "ptr") = cwplparam) ; GWLP_USERDATA := -21
                if (NumGet(cwplparam+0, "uint") = A_PtrSize * 10) ; MSGBOXPARAMS := A_PtrSize * 10
                    GuiDisableMove(cwphwnd)
        }
    }
    return DllCall("CallNextHookEx", "ptr", 0, "int", code, "ptr", wParam, "ptr", lParam, "ptr")
}

; EXIT ==========================================================================================================================

GuiEscape:
GuiClose:
ExitApp

; ===============================================================================================================================
[AHK] 1.1.30.03 x64 Unicode | [WIN] 10 Pro (Version 1903) x64 | [GitHub] Profile
Donations are appreciated if I could help you

Return to “Scripts and Functions”

Who is online

Users browsing this forum: Dysphoria and 41 guests