Page 236 - DCAP408_WEB_PROGRAMMING
P. 236

Windows Programming




                    Notes          14.9.2 A New Function, PeekMessage()

                                   What we will do to solve this dilemma is replace our current GetMessage() function with a new
                                   function, PeekMessage(). This function does essentially the same thing, but with one important
                                   difference: it doesn’t wait for anything. PeekMessage() just looks into the message queue and
                                   checks to see if any messages are waiting. If not, the program will continue on, allowing us to do
                                   what we need.

                                                     Figure  14.1: The  Structure of  a PeekMessage()  Loop



                                         Create the Window






                                                                  PeekMessage()       No
                                           While(TRUE)                                    Handle DirectX Stuff
                                                                  Is there a message?
                                                                Yes

                                                       TranslateMessage()   DispatchMessage()


                                                                                            Message To
                                                                                            WindowProc()

                                   Before we go any further, let’s take a good look at PeekMessage(). Here is it’s prototype.
                                   BOOL  PeekMessage(LPMSG  lpMsg,
                                                        HWND  hWnd,
                                                        UINT  wMsgFilterMin,
                                                        UINT  wMsgFilterMax,
                                                        UINT  wRemoveMsg);
                                   The first four parameters should be familiar to you. They are identical to the four parameters of
                                   GetMessage(). However, the fifth one, wRemoveMsg, is new.
                                   What it does is indicate whether or not the message retrieved from the event queue should stay
                                   on the event queue or come off. We can put either PM_REMOVE or PM_NOREMOVE. The first
                                   one takes the messages off the queue when  they are  read, while  the second  one leaves  the
                                   messages there for later retrieval. We will use the PM_REMOVE value here, and keep things
                                   simple.
                                   So how do we implement this into our  program? Following  is the main loop  from the  last
                                   program we made, modified to use PeekMessage().
                                   //  Enter  the  infinite  message  loop
                                   while(TRUE)
                                   {
                                           //  Check  to  see  if  any  messages  are  waiting  in  the  queue
                                           while(PeekMessage(&msg,  NULL,  0,  0,  PM_REMOVE))




          230                               LOVELY PROFESSIONAL UNIVERSITY
   231   232   233   234   235   236   237   238   239   240   241