我的目标是为我的应用程序创建一个自定义最大化按钮,我还想在 Windows 11 上的按钮上触发捕捉布局菜单。为此,我按照此处的指南进行操作,但它不起作用。
这里是示例代码,出于测试目的,我取一个矩形 {point(0,0), size(200,200)},当鼠标悬停在其上方并且我收到 WM_NCHITTEST 消息时,我按照指南中的说明返回 HTMAXBUTTON 但快照布局菜单不显示。
#include <stdio.h>
#ifndef UNICODE
#define UNICODE
#endif
#include <windows.h>
#include <windowsx.h>
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
// Register the window class.
const wchar_t CLASS_NAME[] = L"Sample Window Class";
WNDCLASS wc = { };
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
// Create the window.
HWND hwnd = CreateWindowEx(
0, // Optional window styles.
CLASS_NAME, // Window class
L"Test Window", // Window text
WS_OVERLAPPEDWINDOW, // Window style
// Size and position
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, // Parent window
NULL, // Menu
hInstance, // Instance handle
NULL // Additional application data
);
if (hwnd == NULL)
{
return 0;
}
ShowWindow(hwnd, nCmdShow);
// Run the message loop.
MSG msg = { };
while (GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
// All painting occurs here, between BeginPaint and EndPaint.
FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW+1));
EndPaint(hwnd, &ps);
}
return 0;
case WM_NCHITTEST:
{
// Get the point in screen coordinates.
// GET_X_LPARAM and GET_Y_LPARAM are defined in windowsx.h
POINT point = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
// Map the point to client coordinates.
::MapWindowPoints(nullptr, hwnd, &point, 1);
RECT maximizeRect {0, 0, 200, 200};
// If the point is in your maximize button then return HTMAXBUTTON
if (::PtInRect(&maximizeRect, point))
{
printf("maxmize button rect %d\n", rand());
fflush(stdout);
return HTMAXBUTTON;
}
break;
}
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
maximizeRect
是客户端坐标,而不是最大化按钮矩形。有一个 HitTestNCA 示例,它在屏幕坐标而不是客户端坐标中进行计算,并且工作正常。另一个 win32-window-custom-titlebar 示例。
我制作了一个应用程序,其中自定义标题栏是使用 HTML 和 CSS 制作的。我没有使用默认标题栏。将鼠标悬停在自定义最大化按钮上是否可以触发快照菜单?如果可以的话,你能建议一下如何做吗?