Set MsgBox Button Text? a133 Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Larkal
Posts: 21
Joined: 08 Mar 2019, 00:36

Set MsgBox Button Text? a133

Post by Larkal » 30 Apr 2021, 17:09

Can someone tell me what I'm doing wrong and how to get a message box with customized button text?

Code: Select all

OnMessage(0x44, OnMsgBox)

msgboxResult := MsgBox("Hello, world!", "This is AutoHotkey", 0x4243)

OnMsgBox(wParam, lParam, msg, hwnd) {
    wintitle := "ahk_id " hwnd
    ControlSetText "This", "Button1", wintitle
    ControlSetText "is", "Button2", wintitle
    ControlSetText "a", "Button3", wintitle
    ControlSetText "test", "Button4", wintitle
}
I'm getting
Error: Target window not found.
I think I'm misunderstanding what the hwnd id is for.
User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: Set MsgBox Button Text? a133

Post by kczx3 » 30 Apr 2021, 20:47

What if you just use the hwnd and don’t prepend ahk_id?
Larkal
Posts: 21
Joined: 08 Mar 2019, 00:36

Re: Set MsgBox Button Text? a133

Post by Larkal » 30 Apr 2021, 22:51

If I try just using hwnd it complains
Error: Target control not found.
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: Set MsgBox Button Text? a133  Topic is solved

Post by lexikos » 01 May 2021, 02:47

That's because there are no buttons on the window identified by hwnd, which is probably the script's main window, not the MsgBox. You need to be more selective with your OnMessage. You also might need to find the MsgBox by using WinExist (with DetectHiddenWindows true).

The error is different if you use ahk_id because then it is restricted by DetectHiddenWindows, and the window is hidden.
The names of the buttons can be customized by following this example.
Source: MsgBox - Syntax & Usage | AutoHotkey v2
(But if used correctly, OnMessage is probably better than the timer used in the example, as it can act earlier.)
Larkal
Posts: 21
Joined: 08 Mar 2019, 00:36

Re: Set MsgBox Button Text? a133

Post by Larkal » 04 May 2021, 04:30

Thanks @lexikos, that was exactly right.

I was able to get it working using DetectHiddenWindows true and WinExist.
Here's what I finally got working

Code: Select all

OnMessage(0x44, OnMsgBox)

gMsgBoxTitle := "Hello Autohotkey"
MsgBox("Hello, world!", gMsgBoxTitle, 0x4243)

OnMsgBox(wParam, lParam, msg, hwnd) {
    global gMsgBoxTitle
    DetectHiddenWindows true
    msgBoxHwnd := WinExist(gMsgBoxTitle)
    if (msgBoxHwnd) {
      ControlSetText "First", "Button1", msgBoxHwnd
      ControlSetText "Second", "Button2", msgBoxHwnd
      ControlSetText "Third", "Button3", msgBoxHwnd
      ControlSetText "Fourth", "Button4", msgBoxHwnd
    }
    DetectHiddenWindows false
}
Post Reply

Return to “Ask for Help (v2)”