利用Windows空闲时间绘制随机大小和颜色的矩形
来源:互联网 发布:梦幻手游巅峰数据刷新 编辑:程序博客网 时间:2024/06/11 09:45
Windows 有很多的 “ 空闲时间 ”,在这期间所有的消息队列都是空的,Windows 就在等待键盘或者鼠标的输入。 那么能否在空闲期间从某种程度上获取控制并绘制随机矩形,而一旦有消息加载到程序的消息队列,就释放控制呢?这正是 PeekMessage 函数的 ” 用武之地 “
PeekMessage
The PeekMessage function dispatches incoming sent messages, checks the thread message queue for a posted message, and retrieves the message (if any exist).
BOOL PeekMessage( LPMSG lpMsg, // message information HWND hWnd, // handle to window UINT wMsgFilterMin, // first message UINT wMsgFilterMax, // last message UINT wRemoveMsg // removal options);
Parameters
- lpMsg
- [out] Pointer to an MSG structure that receives message information.
- hWnd
- [in] Handle to the window whose messages are to be examined.
- wMsgFilterMin
- [in] Specifies the value of the first message in the range of messages to be examined.
- wMsgFilterMax
- [in] Specifies the value of the last message in the range of messages to be examined.
- wRemoveMsg
- [in] Specifies how messages are handled. This parameter can be one of the following values.
Value Meaning PM_NOREMOVEMessages are not removed from the queue after processing by PeekMessage.PM_REMOVEMessages are removed from the queue after processing by PeekMessage.You can optionally combine the value PM_NOYIELD with either PM_NOREMOVE or PM_REMOVE. This flag prevents the system from releasing any thread that is waiting for the caller to go idle (seeWaitForInputIdle).
By default, all message types are processed. To specify that only certain message should be processed, specify one of more of the following values.
Value Meaning PM_QS_INPUTWindows 98, Windows 2000: Process mouse and keyboard messages.PM_QS_PAINTWindows 98, Windows 2000: Process paint messages.PM_QS_POSTMESSAGEWindows 98, Windows 2000: Process all posted messages, including timers and hotkeys.PM_QS_SENDMESSAGEWindows 98, Windows 2000: Process all sent messages.
Return Values
If a message is available, the return value is nonzero.
If no messages are available, the return value is zero.
有了这个 PeekMessage 函数,我们只需简单修改一下正常的消息循环,如下:
while (TRUE)
{
if(PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
{
if(msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
[ other program lines to do some work ]
}
return msg.wParam;
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
注意:在这里,必须明确检查 WM_QUIT 消息,在一个正常的消息循环中,不需要这样做,因为当获取一个 WM_QUIT 消息时, GetMessage 函数返回值是 FALSE ( 0 ) 。但是 PeekMessage 函数的返回值是队列中是否有消息,因此检查 WM_QUIT 是必要的。
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
源代码:
#include <windows.h>
#include <stdlib.h>
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
void DrawRectangle (HWND);
int cxClient,cyClient;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
static TCHAR szAppName [] = TEXT ("RandRect");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL,IDI_APPLICATION);
wndclass.hCursor = LoadIcon (NULL,IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wndclass.lpszClassName =szAppName ;
wndclass.lpszMenuName =NULL ;
if(!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT("This Program requires Windows NT!"),szAppName,MB_ICONERROR);
return 0;
}
hwnd = CreateWindow (szAppName, TEXT("Random Rectangles"),WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,nShowCmd);
UpdateWindow(hwnd);
while (TRUE)
{
if(PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
{
if(msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
DrawRectangle(hwnd);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_SIZE:
cxClient = LOWORD (lParam);
cyClient = HIWORD (lParam);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
void DrawRectangle(HWND hwnd)
{
HBRUSH hBrush;
HDC hdc;
RECT rect;
if(cxClient == 0 || cyClient == 0)
return ;
SetRect(&rect, rand() %cxClient, rand() % cyClient, rand() %cxClient, rand() % cyClient);
hBrush = CreateSolidBrush(RGB(rand() %256,rand() %256,rand() %256));
hdc = GetDC(hwnd);
FillRect(hdc, &rect, hBrush);
ReleaseDC(hwnd, hdc);
DeleteObject(hBrush);
}
运行结果:
- 利用Windows空闲时间绘制随机大小和颜色的矩形
- windows 随机矩形绘制
- MFC对话框利用空闲时间的方法
- 利用canvas绘制矩形和圆形
- 在OpenCV中利用鼠标绘制矩形和截取图像的矩形区域
- 利用鼠标绘制矩形和截取图像的矩形区域并保存(OpenCV2.4.5)
- 在OpenCV中利用鼠标绘制矩形和截取图像的矩形区域
- 在OpenCV中利用鼠标绘制矩形和截取图像的矩形区域
- Windows中如何获取键盘和鼠标处于空闲状态的时间
- 转载:Windows中如何获取键盘和鼠标处于空闲状态的时间
- Windows中如何获取键盘和鼠标处于空闲状态的时间
- Windows中如何获取键盘和鼠标处于空闲状态的时间
- 【转】Windows中如何获取键盘和鼠标处于空闲状态的时间
- OpenCV学习笔记(18):在OpenCV中利用鼠标绘制矩形和截取图像的矩形区域
- HTML5 canvas 绘制矩形 改变矩形颜色
- 绘制颜色渐变矩形函数
- opengl练习之绘制任意大小的矩形
- OpenCV鼠标绘制矩形和截取图像的矩形区域
- 编译内核模块Hello World V3.0
- cocos2d-x游戏实例 《简单棋》入门尝试(六) 棋子吃子情况判定
- 2013第一夜徒,及国外朋友们的生活
- 【远控编写01】客户端界面的设计和编写--对话框,列表控件的伸缩
- C++读写ini配置文件
- 利用Windows空闲时间绘制随机大小和颜色的矩形
- 10258 - Contest Scoreboard
- C++的XML编程经验――LIBXML2库使用指南
- WMI技术介绍和应用——查询系统服务
- linux用户管理-2
- CentOS6.3 问题解决记录
- 【android开发记录片】2.自定义/定制 Dialog组件
- 【2013.2.12】上阵父子兵,打仗两模式——Strategy&State
- win CE 的内存管理