Re: Win32 Q & requesting A
- Posted by Derek Parnell <derekp at solace.com.au> Sep 20, 2000
- 435 views
>I was wanting to know if theres a way that i can incorporate a >while-loop as >i would in DOS32 or console mode application in a Win32 GUI app using >Win32lib. > >Any answers would greatly contribute to the development of my free platform Hi, well of course you can, but you may not want to. It all depends on what you are trying to achieve. Windows programming is based on a different model (paradigm) to console apps. It uses an "event" model. This simply means that your code only gets to do anything at specific times. Once you run the WinMain() procedure, you hand control over to Windows. Then the only bits of your code that execute are the onXXX() handlers you have written and told Win32Lib about, such as onClick(), onClose() etc... Because of Windows event model, these routines are run by Win32Lib on the occurance of some specific event. Now, depending on what you're trying to do, you can set up two types of handlers that get control more frequently than the normal handler routines. These are onEvent() and onTimer(). First the onTimer() handler. This fires every so often, at a rate that you can set. For example... ------------------ constant Win = create(Window,"Timer Window",0,Default,Default,200,100,0), TimerA = 1, -- can be any number, but must be unique for each timer TimerB = 2 procedure tick( integer timerId ) -- do something ... if timerID = TimerA then .... elsif timerId = TimerB then ... else -- should never happen end if end procedure onTimer [Win] = routine_id( "tick" ) setTimer( Win, TimerA, 1000 ) -- tick once per second setTimer( Win, TimerB, 250 ) -- tick once per 0.25 seconds WinMain( Win, Normal ) ------------------ The drawback to this is that it is possible to miss some ticks if the window is very busy. Secondly, the onEvent() handler. This fires for every event message that windows sends to your application. For example... ------------------ constant Win = create(Window,"Event Window",0,Default,Default,200,100,0) procedure anEvent( atom iMsg, atom wParam, atom lParam ) -- iMsg is the Windows message code -- wParam and lParam are data particular to the message code. -- To have windows ignore a message, use returnValue(). -- To have windows process the message as normal, do not use returnValue() -- do something ... end procedure onEvent [Win] = routine_id( "anEvent" ) WinMain( Win, Normal ) ------------------ The drawback to this is that it can fire *very* often and at irregular intervals, including never! If the user of the application is not doing anything (and no Timer has been set) then this never fires. However, it also fires for every pixel that the mouse moves over! To set up a "wait" loop inside an handler may be counter productive, as it stops window processing. If you are trying to detect certain keys being used, it would be better to use the onKeyPress() handler. This fires for all (printable) keys. Use the onKeyDown or onKeyUp for the arrow keys etc... Hope all this helps in some way. ----- cheers, Derek Parnell