开发者

PeekMessage problem

I tried to test the "Examining a Message Queue" example in this page:

http://msdn.microsoft.com/en-us/library/ms644928(VS.85).aspx

In order to test it I created a simple window with an edit control and some buttons,

but it doesn't work as I expected, It should Display repeatedly the string "Some Text" in the EditControl until

I press a button... but the problem is that It displays t开发者_高级运维he string only the first time and then it seems to block in the PeekMessage loop.

I noticed that placing a DispatchMessage(&msg) call afterward, it seems to work properly.

How can I resolve??? Do I necessarily have to call DispatchMessage(&msg)???

Thanks!

HWND hwnd; 
BOOL fDone; 
MSG msg;

fDone = FALSE; 
while (!fDone) 
{ 
 SetFocus(EditControl);
 SendMessage(EditControl, EM_REPLACESEL, (WPARAM)FALSE, (LPARAM)TEXT("Some Text\r\n"));

 while (PeekMessage(&msg, hwnd, 0, 0, PM_REMOVE)) // It blocks here, if I press any button it always sets fDone to TRUE without exiting the loop 
 { 

// DispatchMessage(&msg); uncomment this and it works 

 switch(msg.message) 
 { 
 case WM_LBUTTONDOWN: 
 case WM_RBUTTONDOWN: 
 case WM_KEYDOWN:
 {
 fDone = TRUE;
 SetFocus(EditControl);
 SendMessage(EditControl, EM_REPLACESEL, (WPARAM)FALSE, (LPARAM)TEXT("fDone set to TRUE\r\n"));
 }
 } 
 } 
}


Well, that doesn't make any sense of course. If the fDone flag is set to TRUE, there's no way it can stay inside the loop and keep calling PeekMessage(). Nor can PeekMessage() block. Blowing the stack frame could have an effect like that, but that's not indicated here and always an explanation of last resort.

The more likely explanation is that this code gets repeatedly executed from the top. Maybe by you calling it from the window procedure. Yes, that can get you in an endless loop easily if you don't call DispatchMessage(). The WM_PAINT message is an obvious candidate, it will just keep firing without letup if you don't call Begin/EndPaint(). This is just a theory of course, can't know for sure without knowing how this code is getting called.


You need a GetMessage loop from that article instead.


You have the same problem as I just had: Basic window creation

You are missing the 'default'-case for your switch, otherwise the window cannot handle all the other cases and it doesn't work.

switch(msg.message) 
 { 
     case WM_LBUTTONDOWN: 
     case WM_RBUTTONDOWN: 
     case WM_KEYDOWN:
     {
         fDone = TRUE;
         SetFocus(EditControl);
         SendMessage(EditControl, EM_REPLACESEL, (WPARAM)FALSE, (LPARAM)TEXT("fDone set to TRUE\r\n"));
     }

     default:
     {
         DispatchMessage(&msg)
     }
 } 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜