Page 1 of 2

Microsoft's recommended timed messagebox

Posted: Wed Mar 21, 2007 5:21 pm
by netmaestro
Here's where they explain the recommended method:

http://support.microsoft.com/?scid=181934

And here's the provided C code converted to PureBasic:

Code: Select all

;***************************************************************************
; Program:         Yet another timed messagebox sample
; Source:          http://support.microsoft.com/?scid=181934
; PB version by:   netmaestro
; Date:            March 21, 2007
; Applies to:      Anyone who likes timed message boxes
; Disclaimer:      Hardly any animals were harmed during the creation of
;                  this software. (one who whined got his feelings hurt)
;***************************************************************************

#MessageBox_Timeout = -1
 
Global g_hwndTimedOwner
Global g_bTimedOut

Procedure MessageBoxTimer(hwnd, uiMsg, idEvent, dwTime)
   g_bTimedOut = #True
   If g_hwndTimedOwner
     EnableWindow_(g_hwndTimedOwner, #True)
   EndIf
   PostQuitMessage_(0)
EndProcedure

Procedure TimedMessageBox(hwndOwner, pszMessage.s, pszTitle.s, flags, dwTimeout)
   Protected idTimer.l, iResult.l
   g_hwndTimedOwner = #Null
   g_bTimedOut = #False
   If hwndOwner And IsWindowEnabled_(hwndOwner)
     g_hwndTimedOwner = hwndOwner
   EndIf
   idTimer.l = SetTimer_(#Null, 0, dwTimeout, @MessageBoxTimer())
   iResult.l = MessageBox_(hwndOwner, pszMessage, pszTitle, flags)
   KillTimer_(#Null, idTimer)
   If g_bTimedOut
     PeekMessage_(@msg.MSG, #Null, #WM_QUIT, #WM_QUIT, #PM_REMOVE)
     iResult = #MessageBox_Timeout 
   EndIf
   ProcedureReturn iResult
EndProcedure

; Little test...

OpenWindow(0,0,0,320,240,"",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
CreateGadgetList(WindowID(0))
ButtonGadget(0,100,200,100,25,"Do it")
Repeat
ev = WaitWindowEvent()
  If ev = #PB_Event_Gadget
    uiResult = TimedMessageBox(#Null, "Does a triangle have three sides?", "Quiz", #MB_YESNO, 5000) ; // NULL first parameter is important. 
    
    Select uiResult 
      Case #IDYES 
         MessageBox_(#Null, "That's right!", "Result", #MB_OK) 
      Case #IDNO 
         MessageBox_(#Null, "Believe it or not, triangles really do have three sides.", "Result", #MB_OK) 
      Case #MessageBox_Timeout 
         MessageBox_(#Null,  "I sensed some hesitation there. The correct answer is Yes.", "Result", #MB_OK) 
    EndSelect 
  EndIf
Until ev = #WM_CLOSE
I didn't comment the PB version but there's billions of comments in the Microsoft support article.

Posted: Wed Mar 21, 2007 5:31 pm
by Trond
Nice, but ... where's the message box?

Posted: Wed Mar 21, 2007 5:34 pm
by thefool
Works nicely here!

Posted: Wed Mar 21, 2007 5:34 pm
by netmaestro
You're kidding, right?

Posted: Wed Mar 21, 2007 5:35 pm
by ts-soft
works fine here, thx

Posted: Wed Mar 21, 2007 5:42 pm
by Trond
netmaestro wrote:You're kidding, right?
No.

Posted: Wed Mar 21, 2007 5:44 pm
by netmaestro
Well, there's something missing from your system then. You can see nothing more exotic than calling the MessageBox_() api is happening. That really shouldn't be a problem on a normal system running almost any version of Windows.

Posted: Wed Mar 21, 2007 5:45 pm
by Trond
I think there must be something wrong with PB. The MessageBox function simply doesn't do anything. But, if I use it from Delphi it works. And MessageRequester() works.

Posted: Wed Mar 21, 2007 5:57 pm
by SCRJ
Works fine here, thx! :D

Posted: Wed Mar 21, 2007 9:06 pm
by rsts
whoops :oops:

program just ends (if you let the timer expire). maybe the timed messagebox just flashes (there is a beep) but nothing I can see appears on the screen.

Posted: Wed Mar 21, 2007 10:00 pm
by Fluid Byte
I just tried to run the code and got the same result as Trond, nothing! Image

Weird thing though, it works with XP sytle turned off! Image

Posted: Wed Mar 21, 2007 10:20 pm
by Derek
Fluid Byte wrote:I just tried to run the code and got the same result as Trond, nothing! Image

Weird thing though, it works with XP sytle turned off! Image
Same here, works without xp skins on.

Posted: Wed Mar 21, 2007 10:21 pm
by utopiomania
Just saved it as 'thenothingdemo.pb' on my desktop and ran it. Surprisingly, nothing happened. :)

Posted: Wed Mar 21, 2007 10:39 pm
by Trond
Ah, it works with XP style off here as well.

Posted: Wed Mar 21, 2007 10:48 pm
by ts-soft
works with XP-Style on or off
WinXP SP 2