Windows MessageBox_() with custom buttons text

Share your advanced PureBasic knowledge/code with the community.
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Windows MessageBox_() with custom buttons text

Post by Keya »

This is for the Windows MessageBox_() API, not MessageRequester.

Instead of "Yes", "No" etc default MessageBox buttons we can set a simple windows hook and change their text

Code: Select all

Global hHook.l

Procedure.l HookProc(lMsg.l, wParam.l, lParam.l)
  If lMsg = #HCBT_ACTIVATE
    SetWindowText_ (GetDlgItem_(wParam, #IDYES), "Male")
    SetWindowText_ (GetDlgItem_(wParam, #IDNO),  "Female")
    UnhookWindowsHookEx_ (hHook)
  EndIf
EndProcedure

;Have to call this is we havent already created a window
InitCommonControls_()    

hHook = SetWindowsHookEx_(#WH_CBT, @HookProc(), GetModuleHandle_(0), GetCurrentThreadId_())
MsgReply.l = MessageBox_(#HWND_DESKTOP, "What is your gender?", "Custom Msgbox", #MB_YESNO + #MB_ICONINFORMATION) 

Select MsgReply
  Case #IDYES
    MessageRequester("Result", "You clicked Male")
  Case #IDNO
    MessageRequester("Result", "You clicked Female")
EndSelect

Another example using ABORTRETRYIGNORE for 3 buttons (you can also use YESNOCANCEL but caveat of that is the "X" Close button is enabled and clicking that is the same as clicking Cancel)

Code: Select all

Global hHook.l

Procedure.l HookProc(lMsg.l, wParam.l, lParam.l)
  If lMsg = #HCBT_ACTIVATE
    SetWindowText_ (GetDlgItem_(wParam, #IDABORT),  "Male")
    SetWindowText_ (GetDlgItem_(wParam, #IDRETRY),  "Female")
    SetWindowText_ (GetDlgItem_(wParam, #IDIGNORE), "Other")
    UnhookWindowsHookEx_ (hHook)
  EndIf
EndProcedure

;Have to call this is we havent already created a window
InitCommonControls_()    

hHook = SetWindowsHookEx_(#WH_CBT, @HookProc(), GetModuleHandle_(0), GetCurrentThreadId_())
MsgReply.l = MessageBox_(#HWND_DESKTOP, "What is your gender?", "Custom Msgbox", #MB_ABORTRETRYIGNORE + #MB_ICONINFORMATION) 

Select MsgReply
  Case #IDABORT
    MessageRequester("Result", "You clicked Male")
  Case #IDRETRY
    MessageRequester("Result", "You clicked Female")
  Case #IDIGNORE
    MessageRequester("Result", "You clicked Other")
EndSelect
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5357
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Windows MessageBox_() with custom buttons text

Post by Kwai chang caine »

Very interesting
Thanks for sharing 8)
ImageThe happiness is a road...
Not a destination
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Windows MessageBox_() with custom buttons text

Post by Dude »

Thanks for your code, Keya! :) I needed the #MB_YESNO version today.
Post Reply