200分求在全局钩子中使用TIMER ! 请贴上源码!如能解决另开版加分! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我的做法是在WH_CBT上設置鈎子,在創建窗口時用自己proc替換窗口的Proc,並以窗口的名義創建Timer,在proc中處理WM_TIMER消息。注意該調原窗口proc的地方要調用原來的proc 如果需要全局的 Timer, 创建一个这样子的线程试试DWORD WINAPI ThreadProc(LPVOID lpParameter){ HANDLE hTimer = OpenWaitableTimer(SYNCHRONIZE, FALSE, _T("Global\\CSTimer")); if (hTimer == NULL) CreateWaitableTimer(NULL, FALSE, _T("Global\\CSTimer")); while (TRUE) { DWORD dwWait = MsgWaitForMultipleObjects(1, &hTimer, FALSE, INFINITE, QS_ALLEVENTS); if (dwWait == WAIT_OBJECT_0) { // Get the timer event... } else if (dwWait == WAIT_OBJECT_0 + 1) { MSG msg; if ( !GetMessage(&msg, NULL, 0, 0) ) break; } } CloseHandle(hTimer); return 0;} 才发现写错了一点:if (hTimer == NULL) hTimer = CreateWaitableTimer(NULL, FALSE, _T("Global\\CSTimer")); 类似这个#include <windows.h>#include <stdio.h>#include <conio.h>unsigned long WINAPI Thread(PVOID pvoid);void main(){ DWORD dwThreadId; printf("use timer in workthread of console application<masterz>\n"); HANDLE hThread = CreateThread( NULL, // no security attributes 0, // use default stack size Thread, // thread function 0, // argument to thread function 0, // use default creation flags &dwThreadId); DWORD dwwait=WaitForSingleObject(hThread,1000*30); switch(dwwait) { case WAIT_ABANDONED: printf("main thread WaitForSingleObject return WAIT_ABANDONED\n"); break; case WAIT_OBJECT_0: printf("main thread WaitForSingleObject return WAIT_OBJECT_0\n"); break; case WAIT_TIMEOUT: printf("main thread WaitForSingleObject return WAIT_TIMEOUT\n"); break; } CloseHandle(hThread); _getch();}unsigned long WINAPI Thread(PVOID pvoid){ MSG msg; PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE); UINT timerid=SetTimer(NULL,111,3000,NULL); BOOL bRet; int count =0; while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0) { if (bRet == -1) { // handle the error and possibly exit } else if(msg.message==WM_TIMER) { count++; printf("WM_TIMER in work thread count=%d\n",count); if(count>4) break; } else { TranslateMessage(&msg); DispatchMessage(&msg); } } KillTimer(NULL,timerid); printf("thread end here\n"); return 0;} 如何改变一个Button的字体颜色? char 数组转换为VARIANT类型数组 有关SNMP++的东西.希望高手指点下.. SOCKET 缓充区问题 如何获取默认打印机? 多线程调用同一全局函数会不会锁死 想做一个画笔的程序,但怎样实现画布(Canvas)的效果呢? 在DLL中输出的函数是按一个顺序输出的,那么有没有可能改变呢? 一个source edite ?一个关于对话筐的简单问题,真的给分哦! 关于window2000外壳服务的程序(高手请进!!!) 新手请教DLL的问题!
注意該調原窗口proc的地方要調用原來的proc
{
HANDLE hTimer = OpenWaitableTimer(SYNCHRONIZE, FALSE, _T("Global\\CSTimer")); if (hTimer == NULL)
CreateWaitableTimer(NULL, FALSE, _T("Global\\CSTimer")); while (TRUE)
{
DWORD dwWait = MsgWaitForMultipleObjects(1, &hTimer, FALSE, INFINITE, QS_ALLEVENTS);
if (dwWait == WAIT_OBJECT_0)
{
// Get the timer event...
}
else if (dwWait == WAIT_OBJECT_0 + 1)
{
MSG msg;
if ( !GetMessage(&msg, NULL, 0, 0) )
break;
}
} CloseHandle(hTimer); return 0;
}
hTimer = CreateWaitableTimer(NULL, FALSE, _T("Global\\CSTimer"));
#include <stdio.h>
#include <conio.h>
unsigned long WINAPI Thread(PVOID pvoid);
void main()
{
DWORD dwThreadId;
printf("use timer in workthread of console application<masterz>\n");
HANDLE hThread = CreateThread(
NULL, // no security attributes
0, // use default stack size
Thread, // thread function
0, // argument to thread function
0, // use default creation flags
&dwThreadId);
DWORD dwwait=WaitForSingleObject(hThread,1000*30);
switch(dwwait)
{
case WAIT_ABANDONED:
printf("main thread WaitForSingleObject return WAIT_ABANDONED\n");
break;
case WAIT_OBJECT_0:
printf("main thread WaitForSingleObject return WAIT_OBJECT_0\n");
break;
case WAIT_TIMEOUT:
printf("main thread WaitForSingleObject return WAIT_TIMEOUT\n");
break;
}
CloseHandle(hThread);
_getch();
}unsigned long WINAPI Thread(PVOID pvoid)
{
MSG msg;
PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
UINT timerid=SetTimer(NULL,111,3000,NULL);
BOOL bRet;
int count =0;
while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
{
if (bRet == -1)
{
// handle the error and possibly exit
}
else
if(msg.message==WM_TIMER)
{
count++;
printf("WM_TIMER in work thread count=%d\n",count);
if(count>4)
break;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
KillTimer(NULL,timerid);
printf("thread end here\n");
return 0;
}