源代码是书上的 运行的没问题 如果在添加一个窗口
wndclass.lpszClassName = szClass2;
RegisterClass(&wndclass);
hwnd2 = CreateWindow(szClass2, TEXT("TEST"),
WS_OVERLAPPEDWINDOW,
10, 10, 50, 50,
NULL, NULL, hInstance, NULL);
ShowWindow(hwnd2, iCmdShow);
UpdateWindow(hwnd2);
定时器就不起作用了 哪位大哥知道为什么呀
#include <windows.h>
#define ID_TIMER 1
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("Beeper1");
static TCHAR szClass2[] = TEXT("test");
HWND hwnd1;
HWND hwnd2;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("Program requires Windows NT!"),
szAppName, MB_ICONERROR);
return 0;
}
hwnd1 = CreateWindow(szAppName, TEXT("Beeper1 Timer Demo"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
ShowWindow(hwnd1, iCmdShow);
UpdateWindow(hwnd1);
// 下面是我添加出错的
wndclass.lpszClassName = szClass2;
RegisterClass(&wndclass);
hwnd2 = CreateWindow(szClass2, TEXT("TEST"),
WS_OVERLAPPEDWINDOW,
10, 10, 50, 50,
NULL, NULL, hInstance, NULL);
ShowWindow(hwnd2, iCmdShow);
UpdateWindow(hwnd2);
//上面是我添加的
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static BOOL fFlipFlop = FALSE;
HBRUSH hBrush;
HDC hdc;
PAINTSTRUCT ps;
RECT rc;
switch (message)
{
case WM_CREATE:
//if(hwnd==hwnd1)
SetTimer(hwnd, ID_TIMER, 1000, NULL);
return 0;
case WM_TIMER:
MessageBeep(-1);
fFlipFlop = !fFlipFlop;
InvalidateRect(hwnd, NULL, FALSE);
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rc);
hBrush = CreateSolidBrush(fFlipFlop ? RGB(255, 0, 0) : RGB(0, 0, 255));
FillRect(hdc, &rc, hBrush);
EndPaint(hwnd, &ps);
DeleteObject(hBrush);
return 0;
case WM_DESTROY:
KillTimer(hwnd, ID_TIMER);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
3 个解决方案
#1
两个wndclass使用同一个WndProc,导致SetTimer以同样参数执行了两次。
#2
即使SetTimer一次也不在WndProc里面执行也是一样 用SetTimer(NULL,NULL,1000,ProcTimer) 在WinMain函数 创建一个定时器 窗口过程不创建SetTimer 也不接受WM_TIMER消息 结果也是一样。。。。。。 同样的代码用32位汇编写没问题 换成C 就出现问题了 不知道咋回事
#3
仅供参考:
#pragma comment(lib,"user32")
#include <stdio.h>
#include <time.h>
#include <sys/timeb.h>
#include <windows.h>
CRITICAL_SECTION cs_log;
char datestr[16];
char timestr[16];
char mss[4];
void log(char *s) {
struct tm *now;
struct timeb tb;
EnterCriticalSection(&cs_log);
ftime(&tb);
now=localtime(&tb.time);
sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);
sprintf(timestr,"%02d:%02d:%02d",now->tm_hour ,now->tm_min ,now->tm_sec );
sprintf(mss,"%03d",tb.millitm);
printf("%s %s.%s %s",datestr,timestr,mss,s);
LeaveCriticalSection(&cs_log);
}
VOID CALLBACK myTimerProc1(
HWND hwnd, // handle of window for timer messages
UINT uMsg, // WM_TIMER message
UINT idEvent, // timer identifier
DWORD dwTime // current system time
) {
log("In myTimerProc1\n");
}
VOID CALLBACK myTimerProc2(
HWND hwnd, // handle of window for timer messages
UINT uMsg, // WM_TIMER message
UINT idEvent, // timer identifier
DWORD dwTime // current system time
) {
log("In myTimerProc2\n");
}
int main() {
int i;
MSG msg;
InitializeCriticalSection(&cs_log);
SetTimer(NULL,0,1000,myTimerProc1);
SetTimer(NULL,0,2000,myTimerProc2);
for (i=0;i<20;i++) {
Sleep(500);
log("In main\n");
if (GetMessage(&msg,NULL,0,0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
DeleteCriticalSection(&cs_log);
return 0;
}
//2018-04-17 10:07:54.218 In main
//2018-04-17 10:07:54.718 In myTimerProc1
//2018-04-17 10:07:55.218 In main
//2018-04-17 10:07:55.718 In myTimerProc2
//2018-04-17 10:07:56.218 In main
//2018-04-17 10:07:56.218 In myTimerProc1
//2018-04-17 10:07:56.718 In main
//2018-04-17 10:07:56.718 In myTimerProc1
//2018-04-17 10:07:57.218 In main
//2018-04-17 10:07:57.718 In myTimerProc2
//2018-04-17 10:07:58.218 In main
//2018-04-17 10:07:58.218 In myTimerProc1
//2018-04-17 10:07:58.718 In main
//2018-04-17 10:07:58.718 In myTimerProc1
//2018-04-17 10:07:59.218 In main
//2018-04-17 10:07:59.718 In myTimerProc2
//2018-04-17 10:08:00.218 In main
//2018-04-17 10:08:00.218 In myTimerProc1
//2018-04-17 10:08:00.718 In main
//2018-04-17 10:08:00.718 In myTimerProc1
//2018-04-17 10:08:01.218 In main
//2018-04-17 10:08:01.718 In myTimerProc2
//2018-04-17 10:08:02.218 In main
//2018-04-17 10:08:02.218 In myTimerProc1
//2018-04-17 10:08:02.718 In main
//2018-04-17 10:08:02.718 In myTimerProc1
//2018-04-17 10:08:03.218 In main
//2018-04-17 10:08:03.718 In myTimerProc2
//2018-04-17 10:08:04.218 In main
//2018-04-17 10:08:04.218 In myTimerProc1
//2018-04-17 10:08:04.718 In main
//2018-04-17 10:08:04.718 In myTimerProc1
//2018-04-17 10:08:05.218 In main
//2018-04-17 10:08:05.718 In myTimerProc2
//2018-04-17 10:08:06.218 In main
//2018-04-17 10:08:06.218 In myTimerProc1
//2018-04-17 10:08:06.718 In main
//2018-04-17 10:08:06.718 In myTimerProc1
//2018-04-17 10:08:07.218 In main
//2018-04-17 10:08:07.718 In myTimerProc2
#1
两个wndclass使用同一个WndProc,导致SetTimer以同样参数执行了两次。
#2
即使SetTimer一次也不在WndProc里面执行也是一样 用SetTimer(NULL,NULL,1000,ProcTimer) 在WinMain函数 创建一个定时器 窗口过程不创建SetTimer 也不接受WM_TIMER消息 结果也是一样。。。。。。 同样的代码用32位汇编写没问题 换成C 就出现问题了 不知道咋回事
#3
仅供参考:
#pragma comment(lib,"user32")
#include <stdio.h>
#include <time.h>
#include <sys/timeb.h>
#include <windows.h>
CRITICAL_SECTION cs_log;
char datestr[16];
char timestr[16];
char mss[4];
void log(char *s) {
struct tm *now;
struct timeb tb;
EnterCriticalSection(&cs_log);
ftime(&tb);
now=localtime(&tb.time);
sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);
sprintf(timestr,"%02d:%02d:%02d",now->tm_hour ,now->tm_min ,now->tm_sec );
sprintf(mss,"%03d",tb.millitm);
printf("%s %s.%s %s",datestr,timestr,mss,s);
LeaveCriticalSection(&cs_log);
}
VOID CALLBACK myTimerProc1(
HWND hwnd, // handle of window for timer messages
UINT uMsg, // WM_TIMER message
UINT idEvent, // timer identifier
DWORD dwTime // current system time
) {
log("In myTimerProc1\n");
}
VOID CALLBACK myTimerProc2(
HWND hwnd, // handle of window for timer messages
UINT uMsg, // WM_TIMER message
UINT idEvent, // timer identifier
DWORD dwTime // current system time
) {
log("In myTimerProc2\n");
}
int main() {
int i;
MSG msg;
InitializeCriticalSection(&cs_log);
SetTimer(NULL,0,1000,myTimerProc1);
SetTimer(NULL,0,2000,myTimerProc2);
for (i=0;i<20;i++) {
Sleep(500);
log("In main\n");
if (GetMessage(&msg,NULL,0,0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
DeleteCriticalSection(&cs_log);
return 0;
}
//2018-04-17 10:07:54.218 In main
//2018-04-17 10:07:54.718 In myTimerProc1
//2018-04-17 10:07:55.218 In main
//2018-04-17 10:07:55.718 In myTimerProc2
//2018-04-17 10:07:56.218 In main
//2018-04-17 10:07:56.218 In myTimerProc1
//2018-04-17 10:07:56.718 In main
//2018-04-17 10:07:56.718 In myTimerProc1
//2018-04-17 10:07:57.218 In main
//2018-04-17 10:07:57.718 In myTimerProc2
//2018-04-17 10:07:58.218 In main
//2018-04-17 10:07:58.218 In myTimerProc1
//2018-04-17 10:07:58.718 In main
//2018-04-17 10:07:58.718 In myTimerProc1
//2018-04-17 10:07:59.218 In main
//2018-04-17 10:07:59.718 In myTimerProc2
//2018-04-17 10:08:00.218 In main
//2018-04-17 10:08:00.218 In myTimerProc1
//2018-04-17 10:08:00.718 In main
//2018-04-17 10:08:00.718 In myTimerProc1
//2018-04-17 10:08:01.218 In main
//2018-04-17 10:08:01.718 In myTimerProc2
//2018-04-17 10:08:02.218 In main
//2018-04-17 10:08:02.218 In myTimerProc1
//2018-04-17 10:08:02.718 In main
//2018-04-17 10:08:02.718 In myTimerProc1
//2018-04-17 10:08:03.218 In main
//2018-04-17 10:08:03.718 In myTimerProc2
//2018-04-17 10:08:04.218 In main
//2018-04-17 10:08:04.218 In myTimerProc1
//2018-04-17 10:08:04.718 In main
//2018-04-17 10:08:04.718 In myTimerProc1
//2018-04-17 10:08:05.218 In main
//2018-04-17 10:08:05.718 In myTimerProc2
//2018-04-17 10:08:06.218 In main
//2018-04-17 10:08:06.218 In myTimerProc1
//2018-04-17 10:08:06.718 In main
//2018-04-17 10:08:06.718 In myTimerProc1
//2018-04-17 10:08:07.218 In main
//2018-04-17 10:08:07.718 In myTimerProc2