MessageBox() - How to make it safe?
Posted: Tue Apr 17, 2018 1:01 am
				
				Hi,
I haven't found a way to subclass the MessageRequester() and using a different font in it (I need a monospaced font!)
so I've created a normal window that looks like it.
We are supposed to have only one! Repeat .. Until loop regardless of how many windows we are using.
So my problem with my solution is, that it's using its own loop here. I could integrate it into a main loop that decides
per window what to do but I'd like to put this thing into a module to allow easier integration in small programs (that
don't necessarily use a window on its own)...
Additionally, will I run into problems as well because I'm using #PB_Any for all of my gadgets here, knowing that a larger
application with it's own window will get there gadget ids via an enumeration?
			I haven't found a way to subclass the MessageRequester() and using a different font in it (I need a monospaced font!)
so I've created a normal window that looks like it.
We are supposed to have only one! Repeat .. Until loop regardless of how many windows we are using.
So my problem with my solution is, that it's using its own loop here. I could integrate it into a main loop that decides
per window what to do but I'd like to put this thing into a module to allow easier integration in small programs (that
don't necessarily use a window on its own)...
Additionally, will I run into problems as well because I'm using #PB_Any for all of my gadgets here, knowing that a larger
application with it's own window will get there gadget ids via an enumeration?
Code: Select all
#SQ = Chr(39)
#DCRLF = #CRLF$ + #CRLF$
; Possible icons:
; #IDI_ERROR, #IDI_EXCLAMATION, #IDI_QUESTION, #IDI_INFORMATION
Procedure MessageBox(title.s="", message.s="", width.i=0, height.i=0, icon.i=#IDI_ERROR, exit.b=#True)
  Protected.i minwidth = 460, minHeight = 325
  Protected.i hWnd, hButton, hContainer, hFont, hText
  Protected.b endLoop = #False
  ; Make sure we reach at least the minimum width & height
  If width < minwidth : width = minwidth : EndIf
  If height < minHeight : height = minHeight : EndIf
  hWnd = OpenWindow(#PB_Any, 0, 0, width, height, title, #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  If hWnd
    hButton = ButtonGadget(#PB_Any, width - 95, height - 35, 85, 25, "OK")
    hContainer = ContainerGadget(#PB_Any, 0, 0, width, height - 45)
      SetGadgetColor(hContainer, #PB_Gadget_BackColor, $FFFFFF)
      ImageGadget(#PB_Any, 25, 25, 20, 20, LoadIcon_(0, icon))
      hFont = LoadFont(#PB_Any, "Consolas", 8)
      If hFont
        SetGadgetFont(#PB_Default, FontID(hFont))
        hText = TextGadget(#PB_Any, 70, 25, width - 80, height - 70, message)
        SetGadgetColor(hText, #PB_Gadget_BackColor, $FFFFFF)
      EndIf
    CloseGadgetList()
    SetActiveGadget(hButton)
    StickyWindow(hWnd, #True)
    AddKeyboardShortcut(hWnd, #PB_Shortcut_Escape, 11)
    AddKeyboardShortcut(hWnd, #PB_Shortcut_Return, 12)
    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_Gadget, #PB_Event_CloseWindow
          endLoop = #True
        Case #PB_Event_Menu
          Select EventMenu()
            Case 11, 12 ; Escape or return key
              endLoop = #True
          EndSelect
      EndSelect
    Until endLoop = #True
  EndIf
  RemoveKeyboardShortcut(hWnd, #PB_Shortcut_All)
  If exit : End : EndIf
EndProcedure
message = #SQ + "Our app name" + #SQ + #DCRLF + " was started without parameters!" + #DCRLF +
          "Command line parameters" + #CRLF$ +
          "=======================" + #CRLF$ +
          "Required:" + #CRLF$ +
          "/file=''     | File that contains all items to rename" + #CRLF$ +
          "/editor=''   | Full path to the editor to start" + #DCRLF +
          "Optional:" + #CRLF$ +
          "/timeout=<x> | When to quit automatically [Default: 60]" + #CRLF$ +
          "/flags=<x>   | Binary combined value      [Default: 0]" + #CRLF$ +
          "               1 = Use a preview window" + #CRLF$ +
          "               2 = Allow only one instance" + #DCRLF +
          "This process will exit now!"
MessageBox("App name", message)