强制调用WM_PAINT
Forcing the recall of WM_PAINT
我使用CreateWindowEx
创建了一个窗口,它运行良好。窗口打开。
在我处理消息(特别是WM_PAINT
(的函数中,我使用GetCursorPos
获取光标位置,然后在那里绘制一个矩形。但是,我希望窗口像光标一样不断更新和重新绘制这个矩形。
我创建了一个像下面这样的线程来尝试并强制执行:
DWORD WINAPI RedrawLoop(LPVOID lpParam) {
HWND handle = (HWND)lpParam;
while (true) {
RECT lpRect;
GetClientRect(handle, &lpRect);
InvalidateRect(handle, &lpRect, TRUE);
UpdateWindow(handle);
}
return 0;
}
然而,这并不奏效。我已经检查了传入的句柄是否与线程外的窗口句柄相同。
我也尝试过连续发送SendMessage(handle, WM_PAINT, 0, 0);
和RedrawWindow(handle, NULL, NULL, 0);
,但没有成功。
首先,您可以通过处理WM_MOUSEMOVE
消息来获得当前光标位置,并使当前窗口无效。
然后通过WM_PAINT
消息中的Rectangle
函数绘制一个矩形。(注:坐标原点为屏幕左上角(
这是示例:
#include <Windows.h>
#include <commctrl.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("hello windows");
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 = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
}
hwnd = CreateWindow(szAppName,
TEXT("the hello program"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while (GetMessageW(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
static POINT pt;
switch (message)
{
case WM_CREATE:
{
}
case WM_MOUSEMOVE:
{
Sleep(100);
GetCursorPos(&pt);
InvalidateRect(hwnd, NULL, FALSE);
return 0;
}
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rect);
Rectangle(hdc, pt.x-rect.right/4 , pt.y-rect.bottom/4, pt.x + rect.right / 4, pt.y + rect.bottom / 4);
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
相关文章:
- "WM" C++是什么意思?
- 如何访问委托的paint()函数中的另一个索引?
- 直接在QlistView上绘制小部件,并使用QStyleDitemdelegate :: Paint()
- QT4:在所有qgraphicsItem上的单个qgraphictem rapers paint()上的update()
- 什么是窗口最大化/最小化/恢复的WM Windows消息
- Qt. 方法显示文本() 如果我在 QStyledItemDelegate 中定义了 paint() 方法不起作用
- 如何在我的子类控件paint方法中在默认的paint结果之上绘制一些东西
- 请求X11在C或C++中合成WM的图像
- Qt QTableView paint() 事件无限期交付,占用 100% 的 CPU 内核
- Paint QPushButton with QLinearGradient
- Paint Job Estimator C++
- Q即使宽度和高度都为非零,也不调用ridget paint
- Qt paint事件崩溃
- 如何使用插槽实时更新paint()
- paint函数中的QStyleOptionViewItem参数-如何处理它
- 除非先使用MS Paint打开/保存文件,否则带有QRCode位图的LoadImage()将失败
- 如何将使用Qt Paint Application绘制的图像传输到Mat openCV
- OpenGL新手,致力于"paint"程序
- 如果在WM/CE平台中用于卸载,DMProcessConfigXML将关闭正在运行的应用程序
- 重写QGraphicsItem的paint()和mouseEvents()