Microsoft's recommended timed messagebox

Share your advanced PureBasic knowledge/code with the community.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Microsoft's recommended timed messagebox

Post 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.
Last edited by netmaestro on Wed Mar 21, 2007 11:48 pm, edited 1 time in total.
BERESHEIT
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Nice, but ... where's the message box?
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

Works nicely here!
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

You're kidding, right?
BERESHEIT
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

works fine here, thx
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

netmaestro wrote:You're kidding, right?
No.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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.
Last edited by netmaestro on Wed Mar 21, 2007 5:46 pm, edited 1 time in total.
BERESHEIT
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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.
SCRJ
User
User
Posts: 93
Joined: Sun Jan 15, 2006 1:36 pm

Post by SCRJ »

Works fine here, thx! :D
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Post 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.
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post 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
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Post 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.
User avatar
utopiomania
Addict
Addict
Posts: 1655
Joined: Tue May 10, 2005 10:00 pm
Location: Norway

Post by utopiomania »

Just saved it as 'thenothingdemo.pb' on my desktop and ran it. Surprisingly, nothing happened. :)
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Ah, it works with XP style off here as well.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

works with XP-Style on or off
WinXP SP 2
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
Post Reply