Jump to content

Can I get hold of MsgBox button caption? - SOLVED


CYCho
 Share

Recommended Posts

Suppose I have the following MsgBox:

#include <MsgBoxConstants.au3>

MsgBox($MB_OKCANCEL, "MUI Question", "Click 'OK' to continue, 'Cancel' to quit")

The captions of the buttons will dynamically change depending on the default language of the Windows. How can I get hold of these captions and replace 'OK' and 'Cancel' with them in the text of the above MsgBox?

Edited by CYCho
Link to comment
Share on other sites

50 minutes ago, CYCho said:

How can I get hold of these captions and replace 'OK' and 'Cancel' with them in the text of the above MsgBox?

Have a look at @funkey 's solution : english-msgbox-button-texts-for-everyone

Code :

#include <WinAPI.au3>
Opt("MustDeclareVars", 1)
Global $hHookMsgBox

_MsgBoxEnglish(0, "Title", "Text")
_MsgBoxEnglish(1, "Title", "Text")
_MsgBoxEnglish(2, "Title", "Text")
_MsgBoxEnglish(3, "Title", "Text")
_MsgBoxEnglish(4, "Title", "Text")
_MsgBoxEnglish(5, "Title", "Text")
_MsgBoxEnglish(6, "Title", "Text")
_MsgBoxEnglish(7, "Title", "Text")

#Region English Button text for MsgBox!!
;##########################################################
Func _MsgBoxEnglish($flag, $title, $text, $timeout = 0, $hwnd = 0)
    Local $hProcMsgBox = DllCallbackRegister("CbtHookProcMsgBox", "int", "int;int;int")
    Local $TIDMsgBox = _WinAPI_GetCurrentThreadId()
    $hHookMsgBox = _WinAPI_SetWindowsHookEx($WH_CBT, DllCallbackGetPtr($hProcMsgBox), 0, $TIDMsgBox)
    Local $iRet = MsgBox($flag, $title, $text, $timeout, $hwnd)
    _WinAPI_UnhookWindowsHookEx($hHookMsgBox)
    DllCallbackFree($hProcMsgBox)
    Return $iRet
EndFunc   ;==>_MsgBoxEnglish

Func CbtHookProcMsgBox($nCode, $wParam, $lParam, $hHookMsgBox)
    Local $RET = 0, $hBitmap = 0, $xWnd = 0
    Local $sButtonText
    If $nCode < 0 Then
        $RET = _WinAPI_CallNextHookEx($hHookMsgBox, $nCode, $wParam, $lParam)
        Return $RET
    EndIf
    Switch $nCode
        Case 5 ;5=HCBT_ACTIVATE
            _WinAPI_SetDlgItemText($wParam, 1, "OK")
            _WinAPI_SetDlgItemText($wParam, 2, "Cancel")
            _WinAPI_SetDlgItemText($wParam, 3, "&Abort")
            _WinAPI_SetDlgItemText($wParam, 4, "&Retry")
            _WinAPI_SetDlgItemText($wParam, 5, "&Ignore")
            _WinAPI_SetDlgItemText($wParam, 6, "&Yes")
            _WinAPI_SetDlgItemText($wParam, 7, "&No")
            _WinAPI_SetDlgItemText($wParam, 8, "Help")
            _WinAPI_SetDlgItemText($wParam, 10, "&Try Again")
            _WinAPI_SetDlgItemText($wParam, 11, "&Continue")
    EndSwitch
    Return
EndFunc   ;==>CbtHookProcMsgBox
Func _WinAPI_SetDlgItemText($hDlg, $nIDDlgItem, $lpString)
    Local $aRet = DllCall('user32.dll', "int", "SetDlgItemText", _
            "hwnd", $hDlg, _
            "int", $nIDDlgItem, _
            "str", $lpString)
    Return $aRet[0]
EndFunc   ;==>_WinAPI_SetDlgItemText
;##########################################################
#EndRegion English Button text for MsgBox!!

 

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

you could check the OS language and use Switch...Case...EndSwitch to display your text accordingly.

this by itself would require you to use a translator before hand and manually write every language case into your code,
or you could try to make the code translate on the fly by using the Google Translate API but that would require a LOT of effort.

If @error Then
    MsgBox(262192, "", @ComputerName & " slaps " & @UserName & " around a bit with a large trout!")
EndIf

"Yeah yeah yeah patience, how long will that take?"  -Ed Gruberman

REAL search results  |  SciTE4AutoIt3 Editor Full Version

Link to comment
Share on other sites

@alienclone, Thanks for your comment. I was wondering if there was a way to intercept the internal process of MsgBox function and get the translated captions of the buttons. As I cannot predefine OS languages, Switch...EndSwitch would not be an option.

PS: I understand that Windows provides Multilanguage User Interface function, but using it would be too large a task for my small application, and I don't know how.

Edited by CYCho
Link to comment
Share on other sites

came up with a messy but easy way...

have the beginning of your script create a stand alone msgbox so that your running script does not pause,
then get the text displayed on the buttons stored in variables, then close the test msgbox.

Run(@AutoItExe & ' /AutoIt3ExecuteLine "MsgBox(4097, ''test msgbox title'', ''test msgbox text'')"')
WinWait("test msgbox title")
$okButtonText = ControlGetText("test msgbox title","test msgbox text",1)
$cancelButtonText = ControlGetText("test msgbox title","test msgbox text",2)
WinClose("test msgbox title")

then later on in msgbox replace 'ok' and 'cancel' with your variables and they should be in whatever language the OS made the buttons

MsgBox($MB_OKCANCEL, "MUI Question", "Click '"& $okButtonText &"' to continue, '"& $cancelButtonText &"' to quit")

you could also just run it all back to back so that the test msgbox flashes for just a second and then is immediately replaced with the final msgbox.

Edited by alienclone
If @error Then
    MsgBox(262192, "", @ComputerName & " slaps " & @UserName & " around a bit with a large trout!")
EndIf

"Yeah yeah yeah patience, how long will that take?"  -Ed Gruberman

REAL search results  |  SciTE4AutoIt3 Editor Full Version

Link to comment
Share on other sites

@alienclone Nice job! You have come up with a very clever solution. One drawback is that this code, even when compiled, will not run in a PC where there is no AutoIt installed. If we have to make another exe and fileinstall it, it would be too much cumbersome.

Link to comment
Share on other sites

With the help of some friends in the Korean AutoIt community, I found a solution.

#include <MsgBoxConstants.au3>
#include <WinAPIRes.au3>

$hInstance = _WinAPI_LoadLibrary("USER32.DLL")
$text = "Click '" & _WinAPI_LoadString($hInstance, 800) & "' to continue, '" & _WinAPI_LoadString($hInstance, 801) & "' to quit"
_WinAPI_FreeLibrary($hInstance)
MsgBox($MB_OKCANCEL, "MUI Question", $text)

#cs
String ID's in User32.dll
OK          800
Cancel      801
Abort       802
Retry       803
Ignore      804
Yes         805
No          806
Close       807
Help        808
TryAgain    809
#ce

 

Edited by CYCho
Link to comment
Share on other sites

nice, so the OS will just automatically show the desired string based on ID in its default language? that is cool.

If @error Then
    MsgBox(262192, "", @ComputerName & " slaps " & @UserName & " around a bit with a large trout!")
EndIf

"Yeah yeah yeah patience, how long will that take?"  -Ed Gruberman

REAL search results  |  SciTE4AutoIt3 Editor Full Version

Link to comment
Share on other sites

Hi CYCho,
Great find in your preceding post ! Here is another way... never ask the user to press on this or that :D

1316354116_dontaskdonttell.png.a1ea0726e7595b5cf62fdad61567527f.png

That one should show "Ok", "Cancel" with an english OS... and in case user press Enter too quickly / mistakenly, "Cancel" got the focus...

Edited by pixelsearch
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...