Page 1 of 1

[Windows] MessageRequester with timeout

Posted: Mon Jun 07, 2010 12:10 pm
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)

Re: [Windows] MessageRequester with timeout

Posted: Mon Jun 07, 2010 3:36 pm
by rsts
Don't know about the ugly part, but there's some nice stuff there.

Thanks for sharing :D

cheers

Re: [Windows] MessageRequester with timeout

Posted: Mon Jun 07, 2010 4:03 pm
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

Re: [Windows] MessageRequester with timeout

Posted: Mon Jun 07, 2010 4:42 pm
by kenmo
There are a handful of similar solutions here:

http://www.purebasic.fr/english/viewtop ... erequester

Re: [Windows] MessageRequester with timeout

Posted: Mon Jun 07, 2010 6:11 pm
by Little John
I'm using this one by netmaestro.

Regards, Little John