完整的源程序如下,不执行的语句已在旁边注释
#include "stdafx.h"
#include "time.h"
#include <stdio.h>
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
// TODO: 在此放置代码。
MSG msg; int i;
UINT TimeID=1;
SetTimer(NULL,TimeID,100,NULL);
i=0; // 主消息循环:
while (GetMessage(&msg, NULL, 0, 0))
{
if(GetMessage(&msg, NULL, 0, 0)==WM_TIMER)//当前语句永远不被执行,所以整个if块不执行!
{
if(i==5)
{
MessageBox(NULL,"i=5!","Time",0);
KillTimer(NULL,TimeID);
PostQuitMessage(0);
}
i++;
}
else if(GetMessage(&msg, NULL, 0, 0)==WM_CREATE)//无数次在执行!!我总是要不断地关闭对话框。
{
MessageBox(NULL,"i am created!!","create",0);
}
else if(GetMessage(&msg, NULL, 0, 0)==WM_PAINT)//不执行!!
{
MessageBox(NULL,"i am PAINed!!","paint ",0);
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
} } return (int) msg.wParam;
}
#include "stdafx.h"
#include "time.h"
#include <stdio.h>
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
// TODO: 在此放置代码。
MSG msg; int i;
UINT TimeID=1;
SetTimer(NULL,TimeID,100,NULL);
i=0; // 主消息循环:
while (GetMessage(&msg, NULL, 0, 0))
{
if(GetMessage(&msg, NULL, 0, 0)==WM_TIMER)//当前语句永远不被执行,所以整个if块不执行!
{
if(i==5)
{
MessageBox(NULL,"i=5!","Time",0);
KillTimer(NULL,TimeID);
PostQuitMessage(0);
}
i++;
}
else if(GetMessage(&msg, NULL, 0, 0)==WM_CREATE)//无数次在执行!!我总是要不断地关闭对话框。
{
MessageBox(NULL,"i am created!!","create",0);
}
else if(GetMessage(&msg, NULL, 0, 0)==WM_PAINT)//不执行!!
{
MessageBox(NULL,"i am PAINed!!","paint ",0);
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
} } return (int) msg.wParam;
}
if(msg.message == WM_TIMER)
....不过好像也没有这么用的,看一下<programing windows>吧
到里面还用GetMessage(&msg, NULL, 0, 0)?
感觉应该先while ((Flag=GetMessage(&msg, NULL, 0, 0)))
然后面全用Flag来判断
if(Flag==WM_TIMER)
{
case WM_TIMER:
...
break;
...
}
在循环里只需要调用一个GetMessage。
不然,,呵呵,谁也不知道程序会怎么执行,MessageBox 调用本身就会发出WM_CREATE消息,WM_CREATE
会发送给他的父窗口,当然就进入了循环中,可以把跟踪该成printf
这样判断不行,
GetMessage(&msg,NULL, 0, 0);
if(msg == WM_TIMER){
......
......}
BOOL GetMessage(
LPMSG lpMsg, // address of structure with message
HWND hWnd, // handle of window
UINT wMsgFilterMin, // first message
UINT wMsgFilterMax // last message
);
Parameters
lpMsg
Pointer to an MSG structure that receives message information from the thread's message queue.
比较标准的代码可以参阅《window程序设计》的代码片断
或者参阅msdn中本身的例子程序
MSG msg; int i;
UINT TimeID=1;
SetTimer(NULL,TimeID,100,NULL);
i=0; // 主消息循环:
while (1)
{
GetMessage(&msg, NULL, 0, 0);
if (msg.message==WM_TIMER)
{
if(i==5)
{
MessageBox(NULL,"i=5!,Exit!","Time",0);
KillTimer(NULL,TimeID);
SendMessage(NULL,WM_QUIT,0,0);//为什么此语句不执行?
// PostQuitMessage(0);
}
i++;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
} } return 0;
}
<programing windows>中文名字是<windows程序设计>
如果没时间那就只看第三章,有你要的答案。
MSG msg; int i;
UINT TimeID=1;
SetTimer(NULL,TimeID,100,NULL);
i=0; // 主消息循环:
while (GetMessage(&msg, NULL, 0, 0))
{
if (msg.message==WM_TIMER)
{
if(i==5)
{
MessageBox(NULL,"i=5!,Exit!","Time",0);
KillTimer(NULL,TimeID);
//SendMessage(NULL,WM_QUIT,0,0);//为什么此语句不执行?
PostQuitMessage(0);
}
i++;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
} } return 0;
}