关于OnTimer,为什么会不好用

AIHUA 2002-01-30 09:06:36
小弟在CListView类中设置OnTimer事件在OnInitialUpdate中SetTimer。原本以为和对话框中的定时器一样,没想到却不好用。我跟踪了一下,发现SetTimer执行成功,而且OnTimer也执行了一次,但仅仅执行了这一次。这是什么原因,是不是需要其他设置。
...全文
154 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangleen 2002-01-30
  • 打赏
  • 举报
回复
微软的兄弟,给点分吧。谢谢!!!
AIHUA 2002-01-30
  • 打赏
  • 举报
回复
to: acptvc(微软全球技术中心 VC技术支持) 
我回去试试先,好用的话,明天加分
顺便问一下 你是:微软亚洲技术中心 VC技术支持?
hujun614 2002-01-30
  • 打赏
  • 举报
回复
可能是因为是从CListView继承的原因吧,你从其它窗口中用定时器吧,CListView可能将定时器另作它用了。
acptvc 2002-01-30
  • 打赏
  • 举报
回复
PRB: OnTimer() Is Not Called Repeatedly for a List Control
Q200054

The information in this article applies to:
• The Microsoft Foundation Classes (MFC), included with:
o Microsoft Visual C++, 32-bit Enterprise Edition, version 4.2
o Microsoft Visual C++, 32-bit Professional Edition, version 4.2
o Microsoft Visual C++, 32-bit Enterprise Edition, version 5.0
o Microsoft Visual C++, 32-bit Professional Edition, version 5.0
o Microsoft Visual C++, 32-bit Enterprise Edition, version 6.0
o Microsoft Visual C++, 32-bit Professional Edition, version 6.0
o Microsoft Visual C++, 32-bit Learning Edition, version 6.0


SYMPTOMS
If you call the SetTimer function to send periodic WM_TIMER messages to a list control, you may find that the WM_TIMER message handler (the OnTimer function) for a list control is called only twice.

CAUSE
The WM_TIMER handler in the list control calls the KillTimer function.

RESOLUTION
If you define the timer ID in the SetTimer call, do not call the default handler for WM_TIMER.

STATUS
This behavior is by design.

MORE INFORMATION
The list control uses the timer for editing labels, and for scrolling. When you handle the timer message, if the timer ID is your own timer, don't call the default handler (CListCtrl::OnTimer).

In the sample code below, without the if clause in the CMyListCtrl::OnTimer function, the default WM_TIMER handler is always called, which destroys both the control's timer and your own timer. As a result, you should see a trace statement for each timer.

void CGensdiView::OnInitialUpdate()
{
CView::OnInitialUpdate();

m_pCtrl = new CMyListCtrl;
m_pCtrl->Create(WS_VISIBLE | WS_CHILD | WS_BORDER
| LVS_REPORT | LVS_EDITLABELS,
CRect(0, 0, 100, 150), this, 2134);

m_pCtrl->InsertColumn(0, "one");
m_pCtrl->InsertItem(0, "0 0 0 0 0 0 0 0 0 0 0");
m_pCtrl->InsertItem(1, "1 1 1 1 1 1 1 1 1 1 1");
m_pCtrl->InsertItem(2, "2 2 2 2 2 2 2 2 2 2 2");
}

void CGensdiView::OnLButtonDown(UINT nFlags, CPoint point)
{
m_pCtrl->m_timerID = m_pCtrl->SetTimer(14, 500, NULL);
TRACE("timer %d is set\n", m_pCtrl->m_timerID);
}

CMyListCtrl::CMyListCtrl()
{
m_timerID = -1;
}

void CMyListCtrl::OnTimer(UINT nIDEvent)
{
if (nIDEvent != m_timerID)
CListCtrl::OnTimer(nIDEvent);
TRACE("OnTimer %d\n", nIDEvent);
}



微软亚洲技术中心 VC技术支持

本贴子仅供CSDN的用户作为参考信息使用。其内容不具备任何法律保障。您需要考虑到并承担使用此信息可能带来的风险。具体事项可参见使用条款 (http://www.csdn.net/microsoft/terms.shtm)。
AIHUA 2002-01-30
  • 打赏
  • 举报
回复
你可以随便建一个多文档工程,式一下就知
zhakewei 2002-01-30
  • 打赏
  • 举报
回复
source code?
AIHUA 2002-01-30
  • 打赏
  • 举报
回复
那位好心人能过来看看,小弟给分一向爽快,只要好用。

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧