settimer too!

Sevens7 2006-08-28 07:22:06
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
UINT time_out = 3000;

SetTimer(NULL, 0, time_out, (TIMERPROC)TimerHandler);

MSG message;
while(TRUE)
{
while (::PeekMessage(&message, NULL, 0, 0, PM_REMOVE))
{
::TranslateMessage(&message);
::DispatchMessage(&message);
}
}

return 0;

}

VOID CALLBACK TimerHandler( HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
{
printf("Hello World!\n");

}

加入下面这段代码
while (::PeekMessage(&message, NULL, 0, 0, PM_REMOVE))
{
::TranslateMessage(&message);
::DispatchMessage(&message);
}
}
使settimer 起作用
但是这样占用CPU不是太多?
有没有别的办法?
还有这断代码什么意思?
...全文
162 9 打赏 收藏 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
思危 2006-08-28
  • 打赏
  • 举报
回复
在控制台中使用定时器,
while (::PeekMessage(&message, NULL, 0, 0, PM_REMOVE))
{
::TranslateMessage(&message);
::DispatchMessage(&message);
}
}
可以使程序不退出,且不阻塞窗口的消息循环
菜牛 2006-08-28
  • 打赏
  • 举报
回复
这是在没有窗口的情况下使用定时器的例子。
菜牛 2006-08-28
  • 打赏
  • 举报
回复
这是在没有窗口的情况下使用定时器的例子。
believefym 2006-08-28
  • 打赏
  • 举报
回复
参考这个

#include <windows.h>

/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/* Make the class name into a global variable */
char szClassName[ ] = "WindowsApp";

int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)

{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */

/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);

/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default color as the background of the window */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;

/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"Windows App", /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
544, /* The programs width */
375, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);

/* Make the window visible on the screen */
ShowWindow (hwnd, nFunsterStil);

/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}

/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}


/* This function is called by the Windows function DispatchMessage() */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_KEYDOWN:
MessageBox(NULL,"a","b",MB_OK);
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}

return 0;
}
believefym 2006-08-28
  • 打赏
  • 举报
回复
而且还没有窗口注册之类的。。。
believefym 2006-08-28
  • 打赏
  • 举报
回复
这段代码好像不伦不类啊
控制台的入口函数,win32的程序体
Dean 2006-08-28
  • 打赏
  • 举报
回复
将 while (::PeekMessage(&message, NULL, 0, 0, PM_REMOVE)) 替换为
while (::GetMessage(&message, NULL, 0, 0))
就不占用CPU了

代码就是一个通过Windows的消息机制来实现的简单定时器,每隔3秒打印一次
believefym 2006-08-28
  • 打赏
  • 举报
回复
nIDEvent
[in] Specifies a nonzero timer identifier.

楼主定时器ID为0也行?
InwIvled 2006-08-28
  • 打赏
  • 举报
回复
这段代码是用来进行消息循环的,用来接受消息(这里主要是WM_TIMER消息)
标准Windows程序应该都有,如果不循环,一个main跑一次就结束了,就无法收到定时消息了

16,550

社区成员

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

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

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