使用分层窗口实现桌面遮罩层C++代码
- User
使用分层窗口实现桌面遮罩层C++代码
- Assistant
以下是一个简单的分层窗口实现桌面遮罩层的C++代码示例:
#include <Windows.h> LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam); int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow) { // 注册窗口类 const wchar_t CLASS_NAME[] = L"My Class"; WNDCLASS wc = {0}; wc.lpfnWndProc = WindowProc; wc.hInstance = hInstance; wc.lpszClassName = CLASS_NAME; RegisterClass(&wc); // 创建分层窗口 HWND hwndLayered = CreateWindowEx( WS_EX_LAYERED | WS_EX_TOOLWINDOW, // style CLASS_NAME, // class name L"", // window title WS_POPUP, // window style 0, 0, // position GetSystemMetrics(SM_CXSCREEN), // width GetSystemMetrics(SM_CYSCREEN), // height NULL, // parent NULL, // menu hInstance, // instance NULL // additional data ); SetLayeredWindowAttributes(hwndLayered, RGB(0, 0, 0), 200, LWA_ALPHA); // 显示窗口 ShowWindow(hwndLayered, nCmdShow); // 消息循环 MSG msg = {0}; while (GetMessage(&msg, NULL, 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; } return DefWindowProc(hwnd, uMsg, wParam, lParam); }在这个示例中,我们注册了一个窗口类,创建了一个分层窗口,并将它的样式设置为
WS_EX_LAYERED和WS_EX_TOOLWINDOW,使其成为一个分层窗口,并且不会在任务栏和 Alt+Tab 列表中显示。然后,我们使用
SetLayeredWindowAttributes函数将窗口的透明度设置为 200,使其变成遮罩层。最后,我们进入消息循环,等待用户关闭窗口。 - User
需要处理WM_PAINT消息
- Assistant
是的,当窗口被重绘时,需要处理 WM_PAINT 消息,以确保遮罩层一直处于正确的状态。
以下是修改后的代码:
#include <Windows.h> LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam); int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow) { // 注册窗口类 const wchar_t CLASS_NAME[] = L"My Class"; WNDCLASS wc = {0}; wc.lpfnWndProc = WindowProc; wc.hInstance = hInstance; wc.lpszClassName = CLASS_NAME; RegisterClass(&wc); // 创建分层窗口 HWND hwndLayered = CreateWindowEx( WS_EX_LAYERED | WS_EX_TOOLWINDOW, // style CLASS_NAME, // class name L"", // window title WS_POPUP, // window style 0, 0, // position GetSystemMetrics(SM_CXSCREEN), // width GetSystemMetrics(SM_CYSCREEN), // height NULL, // parent NULL, // menu hInstance, // instance NULL // additional data ); SetLayeredWindowAttributes(hwndLayered, RGB(0, 0, 0), 200, LWA_ALPHA); // 显示窗口 ShowWindow(hwndLayered, nCmdShow); // 消息循环 MSG msg = {0}; while (GetMessage(&msg, NULL, 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); FillRect(hdc, &ps.rcPaint, (HBRUSH)CreateSolidBrush(RGB(0, 0, 0))); EndPaint(hwnd, &ps); } return 0; } return DefWindowProc(hwnd, uMsg, wParam, lParam); }在上面的代码中,我们添加了一个处理 WM_PAINT 消息的函数。在该函数中,我们使用 BeginPaint 和 EndPaint 函数获取并释放 Device Context,并在窗口上绘制一个黑色的矩形,这样遮罩层就被绘制出来了。
- User
启动后鼠标处于等待状态
- Assistant
在分层窗口中,鼠标事件需要通过 SetWindowLongPtr 函数设置钩子函数来处理,否则鼠标事件会被穿透到下面的窗口。
以下是修改后的代码:
#include <Windows.h> LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam); LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam); HHOOK g_hMouseHook = NULL; int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow) { // 注册窗口类 const wchar_t CLASS_NAME[] = L"My Class"; WNDCLASS wc = {0}; wc.lpfnWndProc = WindowProc; wc.hInstance = hInstance; wc.lpszClassName = CLASS_NAME; RegisterClass(&wc); // 创建分层窗口 HWND hwndLayered = CreateWindowEx( WS_EX_LAYERED | WS_EX_TOOLWINDOW, // style CLASS_NAME, // class name L"", // window title WS_POPUP, // window style 0, 0, // position GetSystemMetrics(SM_CXSCREEN), // width GetSystemMetrics(SM_CYSCREEN), // height NULL, // parent NULL, // menu hInstance, // instance NULL // additional data ); SetLayeredWindowAttributes(hwndLayered, RGB(0, 0, 0), 200, LWA_ALPHA); // 设置钩子函数 g_hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseProc, hInstance, 0); if (g_hMouseHook == NULL) { MessageBox(NULL, L"Error hooking mouse events", L"Error", MB_OK | MB_ICONERROR); return 1; } // 显示窗口 ShowWindow(hwndLayered, nCmdShow); // 消息循环 MSG msg = {0}; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } // 卸载钩子函数 UnhookWindowsHookEx(g_hMouseHook); 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); FillRect(hdc, &ps.rcPaint, (HBRUSH)CreateSolidBrush(RGB(0, 0, 0))); EndPaint(hwnd, &ps); } return 0; } return DefWindowProc(hwnd, uMsg, wParam, lParam); } LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam) { if (nCode == HC_ACTION) { POINT ptMouse; GetCursorPos(&ptMouse); ScreenToClient(GetDesktopWindow(), &ptMouse); SetCursorPos(ptMouse.x, ptMouse.y); } return CallNextHookEx(g_hMouseHook, nCode, wParam, lParam); }在上面的代码中,我们添加了一个 MouseProc 函数作为钩子函数,用于处理鼠标事件。当鼠标移动时,我们将其位置转换为当前分层窗口的客户区坐标,并使用 SetCursorPos 函数将鼠标重置到该位置。
需要注意的是,SetCursorPos 函数会触发鼠标事件,会再次调用钩子函数,因此我们需要使用 CallNextHookEx 函数将事件传递给下一个钩子函数。
- User
要支持多显示器
- Assistant
支持多显示器很简单,只需要获取每一个显示器的尺寸,并调用 CreateWindowEx 函数来创建不同大小的分层窗口即可。
以下是修改后的代码:
#include <Windows.h> LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam); LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam); HHOOK g_hMouseHook = NULL; int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow) { // 注册窗口类 const wchar_t CLASS_NAME[] = L"My Class"; WNDCLASS wc = {0}; wc.lpfnWndProc = WindowProc; wc.hInstance = hInstance; wc.lpszClassName = CLASS_NAME; RegisterClass(&wc); // 显示所有的分层窗口 int nNumMonitors = GetSystemMetrics(SM_CMONITORS); for (int nIndex = 0; nIndex < nNumMonitors; nIndex++) { MONITORINFOEX mi = {0}; mi.cbSize = sizeof(MONITORINFOEX); GetMonitorInfo( MonitorFromPoint( {0, 0}, MONITOR_DEFAULTTOPRIMARY ), &mi ); HWND hwndLayered = CreateWindowEx( WS_EX_LAYERED | WS_EX_TOOLWINDOW, // style CLASS_NAME, // class name L"", // window title WS_POPUP, // window style mi.rcMonitor.left, // x-position mi.rcMonitor.top, // y-position mi.rcMonitor.right - mi.rcMonitor.left, // width mi.rcMonitor.bottom - mi.rcMonitor.top, // height NULL, // parent NULL, // menu hInstance, // instance NULL // additional data ); SetLayeredWindowAttributes(hwndLayered, RGB(0, 0, 0), 200, LWA_ALPHA); g_hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseProc, hInstance, 0); if (g_hMouseHook == NULL) { MessageBox(NULL, L"Error hooking mouse events", L"Error", MB_OK | MB_ICONERROR); return 1; } ShowWindow(hwndLayered, nCmdShow); } // 消息循环 MSG msg = {0}; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } // 卸载钩子函数 UnhookWindowsHookEx(g_hMouseHook); 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); FillRect(hdc, &ps.rcPaint, (HBRUSH)CreateSolidBrush(RGB(0, 0, 0))); EndPaint(hwnd, &ps); } return 0; } return DefWindowProc(hwnd, uMsg, wParam, lParam); } LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam) { if (nCode == HC_ACTION) { POINT ptMouse; GetCursorPos(&ptMouse); ScreenToClient(GetDesktopWindow(), &ptMouse); SetCursorPos(ptMouse.x, ptMouse.y); } return CallNextHookEx(g_hMouseHook, nCode, wParam, lParam); }在代码中,我们使用 GetSystemMetrics 函数获取系统上连接的显示器数量,然后用一个循环为每一个显示器创建一个分层窗口。
使用 GetMonitorInfo 函数获取当前显示器的详细信息,包括其尺寸和位置,并使用这些信息来设置分层窗口的位置和大小。
此外,由于每一个分层窗口都需要钩子函数来处理鼠标事件,因此我们需要在循环中为每一个分层窗口都设置相应的钩子函数。
最后,我们在消息循环结束时卸载钩子函数。
- User
使用分层窗口实现桌面遮罩层并支持多显示器C++代码
- Assistant
以下是一个结合了分层窗口实现桌面遮罩层和支持多显示器的C++代码示例:
#include <Windows.h> LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam); LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam); HHOOK g_hMouseHook = NULL; int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow) { // 注册窗口类 const wchar_t CLASS_NAME[] = L"My Class"; WNDCLASS wc = {0}; wc.lpfnWndProc = WindowProc; wc.hInstance = hInstance; wc.lpszClassName = CLASS_NAME; RegisterClass(&wc); // 显示所有的分层窗口 int nNumMonitors = GetSystemMetrics(SM_CMONITORS); for (int nIndex = 0; nIndex < nNumMonitors; nIndex++) { MONITORINFOEX mi = {0}; mi.cbSize = sizeof(MONITORINFOEX); GetMonitorInfo( MonitorFromPoint( {0, 0}, MONITOR_DEFAULTTOPRIMARY ), &mi ); HWND hwndLayered = CreateWindowEx( WS_EX_LAYERED | WS_EX_TOOLWINDOW, // style CLASS_NAME, // class name L"", // window title WS_POPUP, // window style mi.rcMonitor.left, // x-position mi.rcMonitor.top, // y-position mi.rcMonitor.right - mi.rcMonitor.left, // width mi.rcMonitor.bottom - mi.rcMonitor.top, // height NULL, // parent NULL, // menu hInstance, // instance NULL // additional data ); SetLayeredWindowAttributes(hwndLayered, RGB(0, 0, 0), 200, LWA_ALPHA); g_hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseProc, hInstance, 0); if (g_hMouseHook == NULL) { MessageBox(NULL, L"Error hooking mouse events", L"Error", MB_OK | MB_ICONERROR); return 1; } ShowWindow(hwndLayered, nCmdShow); } // 消息循环 MSG msg = {0}; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } // 卸载钩子函数 UnhookWindowsHookEx(g_hMouseHook); 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); FillRect(hdc, &ps.rcPaint, (HBRUSH)CreateSolidBrush(RGB(0, 0, 0))); EndPaint(hwnd, &ps); } return 0; } return DefWindowProc(hwnd, uMsg, wParam, lParam); } LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam) { if (nCode == HC_ACTION) { POINT ptMouse; GetCursorPos(&ptMouse); // 获取当前鼠标所在的显示器的位置和尺寸 HMONITOR hMonitor = MonitorFromPoint(ptMouse, MONITOR_DEFAULTTONEAREST); MONITORINFOEX mi = {0}; mi.cbSize = sizeof(MONITORINFOEX); GetMonitorInfo(hMonitor, &mi); // 将鼠标位置转换为当前显示器的客户区坐标 ScreenToClient(hwnd, &ptMouse); ptMouse.x -= mi.rcMonitor.left; ptMouse.y -= mi.rcMonitor.top; // 使用 SetCursorPos 函数将鼠标重置到当前位置 SetCursorPos(mi.rcMonitor.left + ptMouse.x, mi.rcMonitor.top + ptMouse.y); } return
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.