Author Topic: PeekMessage - tip&trick  (Read 906 times)

0 Members and 1 Guest are viewing this topic.

Aurel

  • Guest
PeekMessage - tip&trick
« on: May 26, 2019, 07:37:46 AM »
If you want to use winApi func PeekMessage() in your program like this :

If win <> 0
While PeekMessage( wm, 0, 0, 0, PM_REMOVE) > 0             
      TranslateMessage(wm)
      DispatchMessage(wm)
Wend
InvalidateRect( win, NULL, 0)

End if


then u must use Default return under SELECT block with CASE ELSE
like this :

CASE ELSE
RETURN DefWindowProc(hWnd, wmsg, wParam, lParam)


If you use it outside of SELECT block created window is simply closed .
Just to let you know!  :)

PS: I am in translation of one "sprite C win api example"  :D

Mike Lobanovsky

  • Guest
Re: PeekMessage - tip&trick
« Reply #1 on: May 26, 2019, 02:56:25 PM »
That depends on whether each Case in the Select block returns a proper value individually for its respective window message event exactly as per MSDN. If it does then DefWindowProc may safely stay in any of the two positions. If not then DefWindowProc had better go into Case Else to allow the offending Cases to return more or less safe default zeros directly to the system from such a crooked message pump.

Either way, your tip (or trick) has no practical value without a detailed description of what's going on in each Case of your Select block.

And at any rate you should also understand that the code you painted blue is not a loop. Unlike GetMessage, PeekMessage does not wait for the messages to come. If there are no messages in the queue, it simply falls through to the end of program. Consequently, you have to place your code in an outer loop that should run until a WM_QUIT message appears in the message queue:

Code: OxygenBasic
  1. If win <> 0
  2.     Do
  3.         While PeekMessage(wm, 0, 0, 0, PM_REMOVE) > 0            
  4.             TranslateMessage(wm)
  5.             DispatchMessage(wm)
  6.         Wend
  7.         InvalidateRect(win, NULL, FALSE) ' NB: this is going to generate WM_PAINT messages like hell
  8.    Loop While wm.message <> WM_QUIT
  9. End If
« Last Edit: May 26, 2019, 03:08:42 PM by Mike Lobanovsky »

Brian Alvarez

  • Guest
Re: PeekMessage - tip&trick
« Reply #2 on: May 26, 2019, 07:48:36 PM »
As mike said, it can be safely located in either position. I personally like it outside, because that way
i can abort custom handling of a message, or even just capture it as a notification without disrupting
the natural course of messages. You can always return -1 when you no longer need the default behavior.

Aurel

  • Guest
Re: PeekMessage - tip&trick
« Reply #3 on: May 26, 2019, 09:39:45 PM »
Yo Mike !
Yes you have a right about all points.
Yo Brian !
I already use it outside in my ruben interpreter.

As i said ....
I am playing with translation of one C program
soo..nothing special
I just want to see difference in speed between C and Oxygen..
 :D