博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
定时器
阅读量:4199 次
发布时间:2019-05-26

本文共 2367 字,大约阅读时间需要 7 分钟。

我在view中定义了 函数void CNetworkView::DrawDoc(mdraw *mdr)

然后定义了
void CNetworkView::OnTimer(UINT nIDEvent)
{
   
// TODO: Add your message handler code here and/or call default
    switch(nIDEvent)
    {
   
case 1:
        view
->DrawDoc(mdr);
       
//你对1号的操作;
        break;
   
case 2:
        s2
->Send(CurTime);
       
//对2号的操作;
        break;
   
default:
       
break;
    }
    CView::OnTimer(nIDEvent);
}
再在
void CNetworkView::OnDraw(CDC* pDC)
{
    CNetworkDoc
* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
   
// TODO: add draw code for native data here
    view->SetTimer(1, 1000, NULL);
    view
->SetTimer(2 ,2000, NULL);
}

一、SetTimer表示的是定义个定时器。根据定义指定的窗口,在指定的窗口(CWnd)中实现OnTimer事件,这样,就可以相应事件了。

SetTimer有两个函数。
①一个是全局的函数::SetTimer()
UINT SetTimer(
HWND hWnd, // handle of window for timer messages
UINT nIDEvent, // timer identifier
UINT uElapse, // time-out value
TIMERPROC lpTimerFunc // address of timer procedure
);
其中hWnd 是指向CWnd的指针,即处理Timer事件的窗口类。因此继承CWnd的子类均可以定义SetTimer事件。
②SetTimer() 在CWnd中也有定义,即SetTimer()是CWnd的一个成员函数。CWnd的子类可以调用该函数,来设置触发器。
UINT SetTimer(UINT nIDEvent, UINT nElapse, void (CALLBACK EXPORT* lpfnTimer)(HWND, UINT, UINT, DWORD) );
参数含义:
nIDEvent:是指设置这个定时器的iD,即身份标志,这样在OnTimer()事件中,才能根据不同的定时器,来做不同的事件响应。这个ID是一个无符号的整型。
nElapse
是指时间延迟。单位是毫秒。这意味着,每隔nElapse毫秒系统调用一次Ontimer()。
void (CALLBACK EXPORT* lpfnTimer)(HWND, UINT, UINT, DWORD)
Specifies the address of the application-supplied TimerProc callback function that processes the WM_TIMER messages. If this parameter is NULL, the WM_TIMER messages are placed in the application’s message queue and handled by the CWnd object。
意思是,指定应用程序提供的TimerProc回调函数的地址,来处里这个Timer事件。如果是NULL,系统将交由OnTimer()来处理这个Timer事件。
所以,一般情况下,我们将这个值设为NULL,有设置该定时器的对象中的OnTimer()函数来处理这个事件。
例:SetTimer(1,1000,NULL);
如果我要加入两个或者两个以上的 timer怎么办? 继续用SetTimer函数吧,上次的timer的ID是1,这次可以是2,3,4。
SetTimer(2,1000,NULL);
SetTimer(3,500,NULL);
WINDOWS会协调他们的。当然onTimer函数体也要发生变化,要在函数体内添加每一个timer的处理代码:
onTimer(nIDEvent)
{
    switch(nIDEvent)
    {
        case 1:........;
            break;
        case 2:.......;
            break;
        case 3:......;
            break;
    }
}
二、我们再看看KillTimer()和OnTimer()的定义:
KillTimer同SetTimer()一样,他也有两个,一个是全局的::KillTimer(),另一个是CWnd的一个函数。他的声明如下:
//全局函数
BOOL KillTimer(
HWND hWnd, // handle of window that installed timer
UINT uIDEvent // timer identifier
);
//CWnd函数
BOOL KillTimer(int nIDEvent );
这两个函数表示的意思是将iD为nIDEVENT的定时器移走。使其不再作用。其用法如同SetTimer()一样。
再看看OnTimer()
CWnd::OnTimer
afx_msg void OnTimer(UINT nIDEvent);
ontimer() 是响应CWnd对象产生的WM_Timer消息。nIDEvent表示要响应TIMER事件的ID。

转载地址:http://iauli.baihongyu.com/

你可能感兴趣的文章
【一天一道LeetCode】#47. Permutations II
查看>>
【一天一道LeetCode】#48. Rotate Image
查看>>
【一天一道LeetCode】#56. Merge Intervals
查看>>
【一天一道LeetCode】#57. Insert Interval
查看>>
【一天一道LeetCode】#58. Length of Last Word
查看>>
【一天一道LeetCode】#59. Spiral Matrix II
查看>>
【一天一道LeetCode】#30. Substring with Concatenation of All Words
查看>>
【一天一道LeetCode】#60. Permutation Sequence.
查看>>
【一天一道LeetCode】#62. Unique Paths
查看>>
【一天一道LeetCode】#61. Rotate List
查看>>
【一天一道LeetCode】#63. Unique Paths II
查看>>
【一天一道LeetCode】#36. Valid Sudoku
查看>>
【一天一道LeetCode】#75. Sort Colors
查看>>
【一天一道LeetCode】#76. Minimum Window Substring
查看>>
【计算机网络 第五版】阅读笔记之一:概述
查看>>
【计算机网络 第五版】阅读笔记之二:物理层
查看>>
【计算机网络 第五版】阅读笔记之三:数据链路层
查看>>
【计算机网络 第五版】阅读笔记之四:网络层
查看>>
【计算机网络 第五版】阅读笔记之五:运输层
查看>>
【一天一道LeetCode】#77. Combinations
查看>>