[Windows] MessageRequester with timeout

Share your advanced PureBasic knowledge/code with the community.
Nituvious
Addict
Addict
Posts: 1029
Joined: Sat Jul 11, 2009 4:57 am
Location: United States

[Windows] MessageRequester with timeout

Post by Nituvious »

I wrote this up after a new member had asked(here) if MessageRequester could time out.
I know this is really ugly, but I encourage that this be modified so we may all learn how to do it the proper way! :)

My excuses for bad code:
I've been up all night and haven't had my coffee yet!
The cat made me do it.
The dog ate my homework.
etc.

Code: Select all

;Global Title.s,Timeout,hWnd
Global *Timer=AllocateMemory(255)
Prototype ProtoFindWindow(Class.l,Title.s)
Prototype ProtoSendMessage(Window,Message,ParamL,ParamR)
Declare MsgTimeout(*x)

Procedure MsgBox(Title.s,Body.s,Timeout,Flags)
	PokeI(*Timer,Timeout)
	CreateThread(@MsgTimeout(),Timer)
	MessageRequester(Title.s,Body.s,Flags)
EndProcedure

Procedure MsgTimeout(*x)
	For x = 0 To PeekI(*Timer)
		Delay(1000)
	Next
	If OpenLibrary(0,"User32.dll") = 0
		Debug "Library error"
	Else
		FindAWindow.ProtoFindWindow = GetFunction(0, "FindWindowA")
		SendAMessage.ProtoSendMessage = GetFunction(0, "SendMessageA")
		hWnd = FindAWindow(0,Title.s)
		SendAMessage(hWnd,#WM_CLOSE,0,0)
		CloseLibrary(0)
		End
	EndIf
EndProcedure


MsgBox("My Title","My awesome timed message box!",5,#MB_OKCANCEL|#MB_ICONERROR)
▓▓▓▓▓▒▒▒▒▒░░░░░
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: [Windows] MessageRequester with timeout

Post by rsts »

Don't know about the ugly part, but there's some nice stuff there.

Thanks for sharing :D

cheers
User avatar
skywalk
Addict
Addict
Posts: 4221
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: [Windows] MessageRequester with timeout

Post by skywalk »

Hi,
It would be cool if the timeout did not end the calling program. :wink:
Remove the End statement and the MsgBox waits for a mouse click.
Not sure if the Window handle is actually being found to receive the close message?

Code: Select all

      CloseLibrary(0)
      End                   ;<-- Remove this and the MsgBox waits for user input.
PB4.5rc2 x86, winxp pro sp3
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
kenmo
Addict
Addict
Posts: 2047
Joined: Tue Dec 23, 2003 3:54 am

Re: [Windows] MessageRequester with timeout

Post by kenmo »

There are a handful of similar solutions here:

http://www.purebasic.fr/english/viewtop ... erequester
Little John
Addict
Addict
Posts: 4791
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: [Windows] MessageRequester with timeout

Post by Little John »

I'm using this one by netmaestro.

Regards, Little John
Post Reply