《windows核心编程》笔记(四)

定时器示例:

#include <tchar.h>

// The caption of our message box
TCHAR g_szCaption[] = TEXT("Timed Message Box");


// How many seconds we'll display the message box
int g_nSecLeft = 0;


// This is STATIC window control ID for a message box
#define ID_MSGBOX_STATIC_TEXT    0x0000ffff
//控件得值為即xFFFF

VOID WINAPI MsgBoxTimeout(PVOID pvContext, BOOLEAN fTimeout) 
{
   
// NOTE: Due to a thread race condition, it is possible (but very unlikely)
   
// that the message box will not be created when we get here.
   HWND hwnd = FindWindow(NULL, g_szCaption);

   
if (hwnd != NULL) 
   
{
      
// The window does exist; update the time remaining.
      TCHAR sz[100];
      wsprintf(sz, TEXT(
"You have %d seconds to respond"), g_nSecLeft--);
      SetDlgItemText(hwnd, ID_MSGBOX_STATIC_TEXT, sz);

      
if (g_nSecLeft == 0
      
{
         
// The time is up; force the message box to exit.
         EndDialog(hwnd, IDOK);
      }

   }
 
   
else 
   
{
      
// The window does not exist yet; do nothing this time.
      
// We'll try again in another second.
   }

}


int WINAPI _tWinMain(HINSTANCE hinstExe, HINSTANCE, PTSTR pszCmdLine, int
{

   chWindows9xNotAllowed();

   
// How many seconds we'll give the user to respond
   g_nSecLeft = 10;

   
// Create a multishot 1 second timer that begins firing after 1 second.
   HANDLE hTimerQTimer;
   CreateTimerQueueTimer(
&hTimerQTimer, NULL, MsgBoxTimeout, NULL, 100010000);

   
// Display the message box
   MessageBox(NULL, TEXT("You have 10 seconds to respond"), g_szCaption, MB_OK);

   
// Cancel the timer & delete the timer queue
   DeleteTimerQueueTimer(NULL, hTimerQTimer, NULL);

   
// Let us know if the user responded or if we timed-out.
   MessageBox(NULL, (g_nSecLeft == 0? TEXT("Timeout") : TEXT("User responded"), TEXT("Result"), MB_OK);

   
return(0);
}


posted on 2008-06-09 19:39 Phinecos(洞庭散人) 阅读(260) 评论(0)  编辑 收藏 所属分类: C/C++/VC++


标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
"五向定位"职业成长路线公开课(上海、南京、大连)
Google站内搜索


相关链接:

 




导航

统计

公告

与我联系

留言簿(65)

我参与的团队

随笔分类(734)

随笔档案(627)

相册

我的好友

我的站点

积分与排名

最新评论

阅读排行榜

评论排行榜

60天内阅读排行