我不知道我应该使用什么标题,但请阅读此内容。
我正在制作一个在鼠标指针位置显示颜色的程序。我想使用 WM_INPUT 因为 WM_MOUSEMOVE 更新不够快。
这是我的一些主要代码。
#include <Windows.h>
#include <WindowsX.h>
/* void Cls_OnInput(HWND hWnd, UINT inputCode, HRAWINPUT hRawInput) */
#define HANDLE_WM_INPUT(hWnd, wParam, lParam, fn) \
((fn)((hWnd), GET_RAWINPUT_CODE_WPARAM(wParam), (HRAWINPUT)(lParam)), 0L)
static LPCTSTR main_window_class_name = TEXT("MainWindow");
static HDC hdc_screen;
static COLORREF color;
LRESULT CALLBACK MainWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
BOOL MainWindow_OnCreate(HWND hWnd, LPCREATESTRUCT lpCreateStruct);
void MainWindow_OnDestroy(HWND hWnd);
void MainWindow_OnPaint(HWND hWnd);
void MainWindow_OnInput(HWND hWnd, UINT inputCode, HRAWINPUT hRawInput);
ATOM RegisterMainWindowClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(wcex);
wcex.style = 0;
wcex.lpfnWndProc = MainWindowProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = (HICON)LoadImage(NULL, IDI_APPLICATION, IMAGE_ICON, 0, 0, LR_SHARED);
wcex.hCursor = (HCURSOR)LoadImage(NULL, IDC_ARROW, IMAGE_CURSOR, 0, 0, LR_SHARED);
wcex.hbrBackground = GetSysColorBrush(COLOR_BTNFACE);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = main_window_class_name;
wcex.hIconSm = wcex.hIcon;
return RegisterClassEx(&wcex);
}
HWND CreateMainWindow(HINSTANCE hInstance)
{
return CreateWindow(main_window_class_name, TEXT("WhatColor"), WS_CAPTION | WS_SYSMENU, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, HWND_DESKTOP, NULL, hInstance, NULL);
}
LRESULT CALLBACK MainWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
HANDLE_MSG(hWnd, WM_CREATE, MainWindow_OnCreate);
HANDLE_MSG(hWnd, WM_DESTROY, MainWindow_OnDestroy);
HANDLE_MSG(hWnd, WM_PAINT, MainWindow_OnPaint);
HANDLE_MSG(hWnd, WM_INPUT, MainWindow_OnInput);
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
}
BOOL MainWindow_OnCreate(HWND hWnd, LPCREATESTRUCT lpCreateStruct)
{
RAWINPUTDEVICE rid[1];
UNREFERENCED_PARAMETER(lpCreateStruct);
rid[0].usUsagePage = 1;
rid[0].usUsage = 2;
rid[0].dwFlags = 0;
rid[0].hwndTarget = hWnd;
RegisterRawInputDevices(rid, 1, sizeof(rid[0]));
hdc_screen = CreateDC(TEXT("DISPLAY"), NULL, NULL, NULL);
return TRUE;
}
void MainWindow_OnDestroy(HWND hWnd)
{
UNREFERENCED_PARAMETER(hWnd);
DeleteDC(hdc_screen);
PostQuitMessage(EXIT_SUCCESS);
}
void MainWindow_OnPaint(HWND hWnd)
{
static PAINTSTRUCT ps;
BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
}
void MainWindow_OnInput(HWND hWnd, UINT inputCode, HRAWINPUT hRawInput)
{
static RAWINPUT raw_input;
static UINT raw_input_size = sizeof(raw_input);
static POINT point;
UNREFERENCED_PARAMETER(inputCode);
GetRawInputData(hRawInput, RID_HEADER, &raw_input, &raw_input_size, sizeof(raw_input.header));
if (raw_input.header.dwType == RIM_TYPEMOUSE)
{
GetCursorPos(&point);
color = GetPixel(hdc_screen, point.x, point.y);
DeleteBrush(SetClassLong(hWnd, GCL_HBRBACKGROUND, (LONG)CreateSolidBrush(color)));
InvalidateRect(hWnd, NULL, TRUE);
}
}
问题是程序似乎一次又一次地处理 WM_INPUT 消息,并且主窗口卡住了。如果我删除
color = GetPixel(hdc_screen, point.x, point.y);
窗口是正常的,但我无法得到我想要的颜色。
我认为如果您尝试测试我的代码会更好。
我应该怎么做才能解决这个问题?