我编写了一个简单的 Windows 应用程序,用于解码和显示 WM_PAINT 消息。我处理鼠标消息和键盘消息。有趣的是;我没有收到任何鼠标双击消息。我收到普通的鼠标单击(向下和向上)消息,但没有双击消息。
有什么想法吗?谢谢。
{新年快乐}
///////////////////////////////////////////////////////////////////////////////////////////////////
// WindowsProject1.cpp : Defines the entry point for the application.
//
// Displays decoded mouse and keyboard messages.
//
// Has support for saving and restoring the window
// size and position to and from the registry.
///////////////////////////////////////////////////////////////////////////////////////////////////
#include "framework.h"
#include "WindowsProject1.h"
#include "WindowRegistry.h" // WindowPlacement/Registry class
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
WCHAR szTitle[MAX_LOADSTRING]; // The title bar text
WCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: Place code here.
// Initialize global strings
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_WINDOWSPROJECT1, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WINDOWSPROJECT1));
MSG msg;
// Main message loop:
while (GetMessage(&msg, nullptr, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEXW wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WINDOWSPROJECT1));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_WINDOWSPROJECT1);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassExW(&wcex);
}
//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window. RestoreWindowPlacement is
// called to restore the window to the size and position it last had.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // Store instance handle in our global variable
HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
WindowRegistry wr;
wr.Init(hWnd);
DWORD dw;
if (!wr.isOK())
{
// Occurs if this is the first time running and SaveWindowPlacement not invoked
dw = GetLastError();
}
else
{
wr.RestoreWindowPlacement();
}
return TRUE;
}
//
// FUNCTION: GetMessageText(UINT)
//
// PURPOSE: Decodes message type
//
const TCHAR* GetMessageText(UINT message)
{
switch (message)
{
case WM_MOUSEMOVE: return _T("WM_MOUSEMOVE");
case WM_LBUTTONDOWN: return _T("WM_LBUTTONDOWN");
case WM_LBUTTONUP: return _T("WM_LBUTTONUP");
case WM_LBUTTONDBLCLK: return _T("WM_LBUTTONDBLCLK");
case WM_RBUTTONDOWN: return _T("WM_RBUTTONDOWN");
case WM_RBUTTONUP: return _T("WM_RBUTTONUP");
case WM_RBUTTONDBLCLK: return _T("WM_RBUTTONDBLCLK");
case WM_MBUTTONDOWN: return _T("WM_MBUTTONDOWN");
case WM_MBUTTONUP: return _T("WM_MBUTTONUP");
case WM_MBUTTONDBLCLK: return _T("WM_MBUTTONDBLCLK");
case WM_MOUSEWHEEL: return _T("WM_MOUSEWHEEL");
case WM_XBUTTONDOWN: return _T("WM_XBUTTONDOWN");
case WM_XBUTTONUP: return _T("WM_XBUTTONUP");
case WM_XBUTTONDBLCLK: return _T("WM_XBUTTONDBLCLK");
case WM_KEYDOWN: return _T("WM_KEYDOWN");
case WM_KEYUP: return _T("WM_KEYUP");
case WM_CHAR: return _T("WM_CHAR");
case WM_DEADCHAR: return _T("WM_DEADCHAR");
case WM_SYSKEYDOWN: return _T("WM_SYSKEYDOWN");
case WM_SYSKEYUP: return _T("WM_SYSKEYUP");
case WM_SYSCHAR: return _T("WM_SYSCHAR");
case WM_SYSDEADCHAR: return _T("WM_SYSDEADCHAR");
default: return _T("NOT_FOUND");
}
}
//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static UINT sequence = 0;
#define MAX_MESSAGES 50
typedef struct
{
UINT sequence;
UINT message;
WPARAM wParam;
LPARAM lParam;
} mqstruct;
static mqstruct mq[MAX_MESSAGES];
WindowRegistry wr;
switch (message)
{
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
TEXTMETRIC tm;
GetTextMetrics(hdc, &tm);
int x = 10;
int y = 10;
#define MAX_BUFFER_LEN 100
TCHAR sz[MAX_BUFFER_LEN];
const int TabStopPositionsMouse[] =
{
23 * tm.tmAveCharWidth,
54 * tm.tmAveCharWidth,
66 * tm.tmAveCharWidth,
78 * tm.tmAveCharWidth,
96 * tm.tmAveCharWidth,
124 * tm.tmAveCharWidth
};
const int TabStopPositionsKeyboard[] =
{
23 * tm.tmAveCharWidth,
54 * tm.tmAveCharWidth,
85 * tm.tmAveCharWidth,
123 * tm.tmAveCharWidth
};
#define SIZEOFINT(p) (sizeof(p) / sizeof(int))
int cbsz;
for (int i = 0; i < MAX_MESSAGES; i++)
{
if (mq[i].message >= WM_MOUSEFIRST && mq[i].message <= WM_MOUSELAST)
{
// Format and display mouse message
StringCchPrintf(sz, MAX_BUFFER_LEN,
_T("Sequence: %08d\tMessage: %s\tX: %05d\tY: %05d\tdWheel: %+05d\tVKeyStatus: 0x%08X\t"),
mq[i].sequence, GetMessageText(mq[i].message), (short)(LOWORD(mq[i].lParam)), (short)(HIWORD(mq[i].lParam)), (short)(HIWORD(mq[i].wParam)), (LOWORD(mq[i].wParam)));
cbsz = lstrlen(sz); // using 96 out of 100
TabbedTextOut(hdc, x, y, sz, cbsz, SIZEOFINT(TabStopPositionsMouse), TabStopPositionsMouse, 10);
}
else
{
// Format and display keyboard message
StringCchPrintf(sz, MAX_BUFFER_LEN,
_T("Sequence: %08d\tMessage: %s\tlParam: 0x%016llX\twParam: 0x%016llX\t"),
mq[i].sequence, GetMessageText(mq[i].message), mq[i].lParam, mq[i].wParam);
cbsz = lstrlen(sz); // using 92 out of 100
TabbedTextOut(hdc, x, y, sz, cbsz, SIZEOFINT(TabStopPositionsKeyboard), TabStopPositionsKeyboard, 10);
}
y += tm.tmHeight;
}
EndPaint(hWnd, &ps);
}
break;
case WM_MOUSEMOVE:
case WM_LBUTTONDOWN:
case WM_LBUTTONUP:
case WM_LBUTTONDBLCLK:
case WM_RBUTTONDOWN:
case WM_RBUTTONUP:
case WM_RBUTTONDBLCLK:
case WM_MBUTTONDOWN:
case WM_MBUTTONUP:
case WM_MBUTTONDBLCLK:
case WM_MOUSEWHEEL:
case WM_XBUTTONDOWN:
case WM_XBUTTONUP:
case WM_XBUTTONDBLCLK:
case WM_KEYDOWN:
case WM_KEYUP:
case WM_CHAR:
case WM_DEADCHAR:
case WM_SYSKEYDOWN:
case WM_SYSKEYUP:
case WM_SYSCHAR:
case WM_SYSDEADCHAR:
{
// Shift message queuee array up by one
for (int i = MAX_MESSAGES - 1; i > 0; i--)
{
mq[i].sequence = mq[i-1].sequence;
mq[i].message = mq[i-1].message;
mq[i].wParam = mq[i-1].wParam;
mq[i].lParam = mq[i-1].lParam;
}
// Add the current message to the top of the queue
mq[0].sequence = ++sequence;
mq[0].message = message;
mq[0].lParam = lParam;
mq[0].wParam = wParam;
// Repaint client window without erasing it - Forces WM_PAINT message
InvalidateRect(hWnd, NULL, false);
UpdateWindow(hWnd);
}
break;
case WM_DESTROY:
//
DWORD dw;
wr.Init(hWnd);
if (!wr.isOK())
{
dw = GetLastError();
}
else
{
wr.SaveWindowPlacement();
}
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
我将 CS_DBLCLKS 添加到窗口类中。这解决了问题。非常感谢退休忍者。