VC驿站

 找回密码
 加入驿站

QQ登录

只需一步,快速开始

搜索
查看: 1093|回复: 2

[已解决]SetTimer不执行回调函数,请师兄们指点

[复制链接]
37_avatar_middle
最佳答案
1 
在线会员 发表于 2021-2-17 23:48:08 | 显示全部楼层 |阅读模式
#include "stdafx.h"
#include <iostream>
#include <string>

#include <windows.h>
#include <iostream>
using namespace std;

void CALLBACK fun(HWND hDlg, UINT uMsg, UINT iTimerID, DWORD dwTime)
{
       
        cout <<2<< endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
        int c=SetTimer(NULL, NULL,100 ,fun);

        cout <<c<< endl;
        Sleep(1000);
        //SetTimer(NULL,1,100,fun);

        return 0;
}

最佳答案
31_avatar_small
2021-2-18 11:41:21
时钟需要消息循环,而控制台程序没有消息循环,要在控制台中使用时钟需要添加消息循环
完整实例
  1. #include "windows.h"
  2. #include "stdio.h"

  3. VOID CALLBACK TimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime);

  4. int main(int argc, char* argv[])
  5. {
  6.         UINT_PTR nTimerID = SetTimer(NULL, WM_TIMER, 5000, (TIMERPROC)TimerProc);

  7.         printf("Timer created. ID = %d\n", nTimerID);
  8.         MSG msg;
  9.         while(GetMessage(&msg, 0, 0, 0)) {
  10.                 DispatchMessage(&msg);
  11.         }
  12.         return 0;
  13. }

  14. VOID CALLBACK TimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
  15. {
  16.         printf("%08x, %d, %d, %u\n", uMsg, idEvent, dwTime, GetTickCount());
  17. }
复制代码




上一篇:寻求vc大佬帮忙 处理优化下 源码 QQ管家总是报毒
下一篇:有大佬知道如何用mfc打开并显示.prt文件吗
31_avatar_middle
最佳答案
62 
在线会员 发表于 2021-2-18 11:41:21 | 显示全部楼层    本楼为最佳答案   
bestAnswer
时钟需要消息循环,而控制台程序没有消息循环,要在控制台中使用时钟需要添加消息循环
完整实例
  1. #include "windows.h"
  2. #include "stdio.h"

  3. VOID CALLBACK TimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime);

  4. int main(int argc, char* argv[])
  5. {
  6.         UINT_PTR nTimerID = SetTimer(NULL, WM_TIMER, 5000, (TIMERPROC)TimerProc);

  7.         printf("Timer created. ID = %d\n", nTimerID);
  8.         MSG msg;
  9.         while(GetMessage(&msg, 0, 0, 0)) {
  10.                 DispatchMessage(&msg);
  11.         }
  12.         return 0;
  13. }

  14. VOID CALLBACK TimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
  15. {
  16.         printf("%08x, %d, %d, %u\n", uMsg, idEvent, dwTime, GetTickCount());
  17. }
复制代码

评分

参与人数 1驿站币 +2 热心值 +2 收起 理由
58_avatar_small thzzl + 2 + 2 很给力!

查看全部评分

37_avatar_middle
最佳答案
1 
ico_lz  楼主| 发表于 2021-2-18 00:02:37 | 显示全部楼层
SetTimer不执行回调函数,请师兄们指点 传入窗口句柄也不行
您需要登录后才可以回帖 登录 | 加入驿站 qq_login

本版积分规则

×【发帖 友情提示】
1、请回复有意义的内容,请勿恶意灌水;
2、纯数字、字母、表情等无意义的内容系统将自动删除;
3、若正常回复后帖子被自动删除,为系统误删的情况,请重新回复其他正常内容或等待管理员审核通过后会自动发布;
4、感谢您对VC驿站一如既往的支持,谢谢合作!

关闭

站长提醒上一条 /2 下一条

QQ|小黑屋|手机版|VC驿站 ( 辽ICP备09019393号-4 )|网站地图wx_jqr

GMT+8, 2023-9-29 14:52

Powered by CcTry.CoM

© 2009-2021 cctry.com

快速回复 返回顶部 返回列表