MessageRequester Flags, overview

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

MessageRequester Flags, overview

Post by BackupUser »

Code updated For 5.20+

Restored from previous forum. Originally posted by Danilo.

Code: Select all

; 
; 14.11.2002 - by Danilo for german forum
; 12.01.2003 - by Danilo, translated for english forum
; 
; 
; Flags for MessageRequester(Title$, Text$, Flags) 
; ------------------------------------------------- 
; 
; - only available on PureBasic /Windows 
; - API: MessageBox_() & MessageBoxEx_() 
; 
; 
; [ Button Types ] 
; 
;                       #MB_OK = [OK]              ( --> 0 = default ) 
;                 #MB_OKCANCEL = [OK] [Cancel] 
; 
;                    #MB_YESNO = [Yes] [No] 
;              #MB_YESNOCANCEL = [Yes] [No] [Cancel]
; 
;              #MB_RETRYCANCEL = [Retry] [Cancel] 
; 
;         #MB_ABORTRETRYIGNORE = [Abort] [Retry] [Ignore] 
;        #MB_CANCELTRYCONTINUE = [Cancel] [Retry] [Continue]  -  (Windows 98/2000+) 
; 
;                     #MB_HELP = [Help] --> Help-Button (Windows NT4/95+) 
;                                Pressing this Button sends a '#WM_HELP'
;                                message to your program event handler (callback).
; 
; [ Icons (Images) ] 
; 
;                 #MB_ICONSTOP = Icon (X) : Stop 
;                #MB_ICONERROR = Icon (X) : Stop (NT4/95+) 
;                 #MB_ICONHAND = Icon (X) : Stop 
; 
;             #MB_ICONQUESTION = Icon (?) : Question
; 
;             #MB_ICONASTERISK = Icon (i) : Information 
;          #MB_ICONINFORMATION = Icon (i) : Information 
; 
;              #MB_ICONWARNING = Icon (!) : Warning (NT4/95+) 
;          #MB_ICONEXCLAMATION = Icon (!) : Warning 
; 
; [ Default Button ] 
; 
;               #MB_DEFBUTTON1 = 1. Button is 'default' (selected) 
;               #MB_DEFBUTTON2 = 2. Button is 'default' (selected) 
;               #MB_DEFBUTTON3 = 3. Button is 'default' (selected) 
;               #MB_DEFBUTTON4 = 4. Button is 'default' (selected)  -  (NT4/95+) 
; 
; [ Priority ] 
; 
;                #MB_APPLMODAL = User must click the messagebox before 
;                                he can continue using the program. (default) 
;              #MB_SYSTEMMODAL = User must click the messagebox before 
;                                he can continue using the program.
;                                This should only be used if a big general
;                                error occured, like 'out of memory' for example.
;                                
;                #MB_TASKMODAL = Same as #MB_APPLMODAL (in general), but other
;                                Windows of the same Task get disabled too. 
; 
; [ Miscellaneous ] 
; 
;                    #MB_RIGHT = All Text right aligned  -  (Windows NT4/95+) 
;            #MB_SETFOREGROUND = The MsgBox comes to foreground 
;                                (internal SetForegroundWindow_() gets called) 
;                  #MB_TOPMOST = MsgBox is the topmost window ('Stay-on-Top')  -  (Windows NT4/95+) 
;     #MB_SERVICE_NOTIFICATION = The Program is a Windows-Service and the MsgBox 
;                                is also shown if no user is logged in.  -  (Windows NT 4.0+ only) 
;               #MB_RTLREADING = Window is mirrored on its Y axis. 
;                                (for Hebrew and Arabic systems)  -  (Windows NT4/95+) 
; 
; 
; 
; [ Return values ] 
; 
;                 #IDYES       = Yes 
;                 #IDNO        = No 
;                 #IDOK        = OK 
;                 #IDABORT     = Abort 
;                 #IDCANCEL    = Cancel 
;                 #IDCONTINUE  = Continue     ; (Windows 98/2000+) 
;                 #IDIGNORE    = Ignore 
;                 #IDRETRY     = Retry 
;                 #IDTRYAGAIN  = Try again    ; (Windows 98/2000+) 
;                            0 = Error,
;                                cant display MessageBox
; 
; 
#MB_CANCELTRYCONTINUE    = $00000006 ; (Windows 2000+) 
#MB_HELP                 = $00004000 ; (Windows NT4/95+) 
#MB_DEFBUTTON4           = $00000300 ; (Windows NT4/95+) 
#MB_RIGHT                = $00080000 ; (Windows NT4/95+) 
#MB_TOPMOST              = $00040000 ; (Windows NT4/95+) 
#MB_SERVICE_NOTIFICATION = $00040000 ; (Windows NT 4.0+) 
#MB_RTLREADING           = $00100000 ; (Windows NT4/95+) 
#IDCONTINUE              = $0000000B ; (Windows 98/2000+) 
#IDTRYAGAIN              = $0000000A ; (Windows 98/2000+) 

Select MessageRequester("ERROR", "Cant read file 'xyz' !", #MB_ABORTRETRYIGNORE | #MB_ICONSTOP | #MB_DEFBUTTON2) 
   Case #IDYES      : Result$ = "Yes" 
   Case #IDNO       : Result$ = "No" 
   Case #IDOK       : Result$ = "OK" 
   Case #IDABORT    : Result$ = "Abort" 
   Case #IDCANCEL   : Result$ = "Cancel" 
   Case #IDCONTINUE : Result$ = "Continue"     ; (Windows 98/2000+) 
   Case #IDIGNORE   : Result$ = "Ignore" 
   Case #IDRETRY    : Result$ = "Retry" 
   Case #IDTRYAGAIN : Result$ = "Try again"    ; (Windows 98/2000+) 
EndSelect 

MessageRequester("Your Selection:",Result$,0) 
cya,
...Danilo
(registered PureBasic user)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Hi-Toro.

In addition to that, here's how you customise the button text, via a hook/hack (ported from VB code). You'll notice that this crashes the PB Compiler (and you'll have to exit PB and relaunch to continue), but this is due to the program retrieving the thread owner handle (which is the PB Compiler). In a standalone program, it works fine :)

I'd suggest using a normal Yes/No/Cancel requester during development, then replace it with this when you're ready to release...

Code: Select all

; Change dialog box buttons... crashes PBCompiler, but the program itself works!

; Try creating an .exe file -- PB will crash, but the .exe version will work just fine when run :)

#IDOK = 1
#IDCANCEL = 2
#IDABORT = 3
#IDRETRY = 4
#IDIGNORE = 5
#IDYES = 6
#IDNO = 7
#IDPROMPT = $FFFF

Global hHook
Global mbtitle$, mbmsg$, mbopt1$, mbopt2$, mbopt3$

; Have to pre-allocate maximum string length, as the dialog box is created with message/title
; with this many spaces (the width of the box comes from this), then the text is hacked into it.

#StringSpace = 80

Procedure MsgBoxHookProc (uMsg, wParam, lParam)
   If uMsg = #HCBT_ACTIVATE
      SetWindowText_ (wParam, mbtitle$)
      SetDlgItemText_ (wParam, #IDABORT, mbopt1$)
      SetDlgItemText_ (wParam, #IDRETRY, mbopt2$)
      SetDlgItemText_ (wParam, #IDIGNORE, mbopt3$)
      SetDlgItemText_ (wParam, #IDPROMPT, mbmsg$)
      UnhookWindowsHookEx_ (hHook)
   EndIf
   ProcedureReturn #False
EndProcedure

Procedure MessageBoxH (parentWindow, title$, message$, button1$, button2$, button3$)
   mbtitle$ = title$
   mbmsg$ = message$
   mbopt1$ = button1$
   mbopt2$ = button2$
   mbopt3$ = button3$
   hInstance = GetModuleHandle_ (0)
   hThreadId = GetCurrentThreadId_ ()
   hHook = SetWindowsHookEx_ (#WH_CBT, @MsgBoxHookProc (), hInstance, hThreadId)
   ProcedureReturn MessageBox_ (parentWindow, Space (#StringSpace), Space (#StringSpace), #MB_ABORTRETRYIGNORE | #MB_ICONINFORMATION)
EndProcedure

MessageBoxH (0, "Search for program...", "Please select the drive to search...", "Search C:\", "Search D:\", "Cancel")


--
See ya,
James L Boyd.
http://www.hi-toro.com/
--
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by ebs.

James,

Do you (or anyone else) know if the icon in the MessageRequester can be set to a custom image, and how?

Regards,
Eric
Originally posted by Hi-Toro

In addition to that, here's how you customise the button text, via a hook/hack (ported from VB code). You'll notice that this crashes the PB Compiler (and you'll have to exit PB and relaunch to continue), but this is due to the program retrieving the thread owner handle (which is the PB Compiler). In a standalone program, it works fine :)

I'd suggest using a normal Yes/No/Cancel requester during development, then replace it with this when you're ready to release...

Code: Select all

; Change dialog box buttons... crashes PBCompiler, but the program itself works!

; Try creating an .exe file -- PB will crash, but the .exe version will work just fine when run :)

#IDOK = 1
#IDCANCEL = 2
#IDABORT = 3
#IDRETRY = 4
#IDIGNORE = 5
#IDYES = 6
#IDNO = 7
#IDPROMPT = $FFFF

Global hHook
Global mbtitle$, mbmsg$, mbopt1$, mbopt2$, mbopt3$

; Have to pre-allocate maximum string length, as the dialog box is created with message/title
; with this many spaces (the width of the box comes from this), then the text is hacked into it.

#StringSpace = 80

Procedure MsgBoxHookProc (uMsg, wParam, lParam)
   If uMsg = #HCBT_ACTIVATE
      SetWindowText_ (wParam, mbtitle$)
      SetDlgItemText_ (wParam, #IDABORT, mbopt1$)
      SetDlgItemText_ (wParam, #IDRETRY, mbopt2$)
      SetDlgItemText_ (wParam, #IDIGNORE, mbopt3$)
      SetDlgItemText_ (wParam, #IDPROMPT, mbmsg$)
      UnhookWindowsHookEx_ (hHook)
   EndIf
   ProcedureReturn #False
EndProcedure

Procedure MessageBoxH (parentWindow, title$, message$, button1$, button2$, button3$)
   mbtitle$ = title$
   mbmsg$ = message$
   mbopt1$ = button1$
   mbopt2$ = button2$
   mbopt3$ = button3$
   hInstance = GetModuleHandle_ (0)
   hThreadId = GetCurrentThreadId_ ()
   hHook = SetWindowsHookEx_ (#WH_CBT, @MsgBoxHookProc (), hInstance, hThreadId)
   ProcedureReturn MessageBox_ (parentWindow, Space (#StringSpace), Space (#StringSpace), #MB_ABORTRETRYIGNORE | #MB_ICONINFORMATION)
EndProcedure

MessageBoxH (0, "Search for program...", "Please select the drive to search...", "Search C:\", "Search D:\", "Cancel")


--
See ya,
James L Boyd.
http://www.hi-toro.com/
--
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by El_Choni.

Try playing with this:

Code: Select all

MessageBoxIndirect_(MsgBoxParams.MSGBOXPARAMS)
MsgBoxParams:

Points to a MSGBOXPARAMS structure that contains information used to display the message box.

You'll find the structure definition in the IDE Structure viewer. This is what each structure member means (WIN32.HLP):

The MSGBOXPARAMS structure contains information used to display a message box. The MessageBoxIndirect function uses this structure.

Members

cbSize

Specifies the structure size, in bytes.

hwndOwner

Identifies the owner window; can be NULL.

hInstance

Identifies the instance containing the icon resource identified by the lpszIcon member, and the string resource identified by the lpszText or lpszCaption member.

lpszText

Points to a null-terminated string, or the identifier of a string resource, that contains the message to be displayed.

lpszCaption

Points to a null-terminated string, or the identifier of a string resource, that contains the message box title. If this member is NULL, the default title Error is used.

dwStyle

Specifies the contents and behavior of the message box. Can be one or more of the values specified in the MessageBoxEx function and one of the following values:

Value Meaning
MB_HELP Adds a Help button to the message box. Choosing the Help button or pressing F1 generates a Help event.
MB_TOPMOST Uses the WS_EX_TOPMOST window style.
MB_USERICON Uses the icon specified by the lpszIcon member.
lpszIcon

Identifies an icon resource. This parameter can be either a null-terminated string or an integer resource identifier passed to the MAKEINTRESOURCE macro. To load a standard icon, the hInstance member should be NULL, and lpszIcon should be one of the values listed with the LoadIcon function. This parameter is ignored if the dwStyle member does not specify the MB_USERICON flag.

dwContextHelpId

Identifies a Help context. If a Help event occurs, this value is specified in the HELPINFO structure that the message box sends to the owner window or callback function.

lpfnMsgBoxCallback

Points to the callback function that processes Help events for the message box. The callback function has the following form:

VOID CALLBACK MsgBoxCallback(LPHELPINFO lpHelpInfo);


If this member is NULL, the message box sends WM_HELP messages to the owner window when help events occur.

dwLanguageId

Specifies the language in which to display the text contained in the predefined push buttons. This value must be in the form returned by the MAKELANGID macro.



El_Choni
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by ebs.

El Choni,

Thanks for the pointer to MessageBoxIndirect_(). It looks like I would have to embed resources into a PureBasic program in order to be able to use MB_USERICON and lpszIcon. As far as I know, this isn't currently possible, right?

Eric
Originally posted by El_Choni

Try playing with this:

Code: Select all

MessageBoxIndirect_(MsgBoxParams.MSGBOXPARAMS)


El_Choni
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Justin.

I think you can't do it with that function. i have a tool to design any kind of dialog, here's an example of the code produced. you can set the icon in the init message.

email me if someone is interested (i'm selling it)

Code: Select all

;Purebasic Window Editor v2.91

;GADGET IDs
#BT_MSHOW=0
#BT_MCANCEL=1

;DLG CONTROLS IDs
#BT_DYES=0
#BT_DNO=1
#Text2=2

;WINDOW ID
#Window1=0

#ICON_SMALL=0

hinst=getmodulehandle_(0)

;Dialog Procedure
Procedure Dialogproc(hdlg,msg,wparam,lparam)
  Select msg

    Case #WM_INITDIALOG
      ;set icon, test with a system icon
      hicon=LoadIcon_(#null,#IDI_APPLICATION)
      
      ;use catchimage() to get a handle to an icon of your choice

      sendmessage_(hdlg,#WM_SETICON,#ICON_SMALL,hicon)
      retval=1

    Case #WM_COMMAND
      Select peekw(@wparam)

        Case #BT_DYES
          EndDialog_(hdlg,1) ;return 1
          retval=1

        Case #BT_DNO
          EndDialog_(hdlg,0) ;return 0
          retval=1

      EndSelect

    Case #WM_CLOSE
      EndDialog_(hdlg,0)

    Default
      retval=0

  EndSelect
  ProcedureReturn retval
EndProcedure

;WINDOW
hwmain=OpenWindow(#Window1,196,110,377,175,#PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_TitleBar|#PB_Window_ScreenCentered,"Main")
CreateGadgetList(WindowID(#Window1))

ButtonGadget(#BT_MSHOW,20,128,90,24,"Show")
ButtonGadget(#BT_MCANCEL,270,128,90,24,"Cancel")

;EVENT LOOP
Repeat
  EventID=WaitWindowEvent()
    Select EventID
      Case #PB_EventGadget
        Select EventGadgetID()
          Case #BT_MSHOW ;show dialog
            select DialogBoxIndirectParam_(hinst,?dialogdata,hwmain,@Dialogproc(),0)
              case 1 ;yes
                ;do something

              case 0 ;no
                ;do something

            endselect

          Case #BT_MCANCEL
            exit=1

        EndSelect
    EndSelect
Until EventID=#PB_EventCloseWindow or exit

end


;DIALOG TEMPLATE (Modal)
datasection
dialogdata:
data.b 193,8,200,20, 0,0,0,1, 3,0, 169,0, 107,0, 167,0, 63,0
data.b 0,0, 0,0, 67,0,117,0,115,0,116,0,111,0,109,0,32,0,77,0,115,0,103,0,32,0,66,0,111,0,120,0,0,0, 1,0, 77,0,83,0,32,0,83,0,97,0,110,0,115,0,32,0,83,0,101,0,114,0,105,0,102,0,0,0 ,0,0

data.b 0,0,1,80, 0,0,0,0, 7,0, 39,0, 60,0, 15,0, 0,0
data.b 66,0,117,0,116,0,116,0,111,0,110,0,0,0, 89,0,101,0,115,0,0,0 ,0,0 ,0,0

data.b 0,0,1,80, 0,0,0,0, 100,0, 39,0, 60,0, 15,0, 1,0
data.b 66,0,117,0,116,0,116,0,111,0,110,0,0,0, 78,0,111,0,0,0 ,0,0

data.b 0,0,0,80, 0,0,0,0, 7,0, 10,0, 60,0, 15,0, 2,0
data.b 83,0,116,0,97,0,116,0,105,0,99,0,0,0, 65,0,114,0,101,0,32,0,121,0,111,0,117,0,32,0,115,0,117,0,114,0,101,0,63,0,0,0 ,0,0 ,0,0
enddatasection
Post Reply