使用SetLayeredWindowAttributes API 实现透明窗口

好的,以下是使用 Win32 API 实现创建透明窗口并处理鼠标事件的步骤: 1. 定义窗口类 ```c++ WNDCLASS wndClass = { 0 }; wndClass.style = CS_HREDRAW | CS_VREDRAW; wndClass.lpfnWndProc = WndProc; wndClass.hInstance = hInstance; wndClass.hCursor = LoadCursor(NULL, IDC_ARROW); wndClass.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH); wndClass.lpszClassName = L"MyTransparentWindowClass"; RegisterClass(&wndClass); ``` 2. 创建窗口 ```c++ HWND hWnd = CreateWindowEx( WS_EX_LAYERED | WS_EX_TOPMOST | WS_EX_TRANSPARENT, L"MyTransparentWindowClass", L"My Transparent Window", WS_POPUP, 100, 100, 400, 300, NULL, NULL, hInstance, NULL); ``` 3. 设置窗口透明 ```c++ SetLayeredWindowAttributes(hWnd, RGB(255, 255, 255), 0, LWA_COLORKEY); ``` 4. 处理鼠标事件 ```c++ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_LBUTTONDOWN: int xPos = GET_X_LPARAM(lParam); int yPos = GET_Y_LPARAM(lParam); wchar_t buffer[100]; swprintf_s(buffer, L"Mouse down at (%d, %d)", xPos, yPos); MessageBox(hWnd, buffer, L"Mouse Event", MB_OK); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } ``` 5. 显示窗口 ```c++ ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); ``` 6. 进入消息循环 ```c++ MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } ``` 完整的代码示例: ```c++ #include <Windows.h> LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { // 定义窗口类 WNDCLASS wndClass = { 0 }; wndClass.style = CS_HREDRAW | CS_VREDRAW; wndClass.lpfnWndProc = WndProc; wndClass.hInstance = hInstance; wndClass.hCursor = LoadCursor(NULL, IDC_ARROW); wndClass.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH); wndClass.lpszClassName = L"MyTransparentWindowClass"; RegisterClass(&wndClass); // 创建窗口 HWND hWnd = CreateWindowEx( WS_EX_LAYERED | WS_EX_TOPMOST | WS_EX_TRANSPARENT, L"MyTransparentWindowClass", L"My Transparent Window", WS_POPUP, 100, 100, 400, 300, NULL, NULL, hInstance, NULL); // 设置窗口透明 SetLayeredWindowAttributes(hWnd, RGB(255, 255, 255), 0, LWA_COLORKEY); // 显示窗口 ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); // 进入消息循环 MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int)msg.wParam; } LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_LBUTTONDOWN: int xPos = GET_X_LPARAM(lParam); int yPos = GET_Y_LPARAM(lParam); wchar_t buffer[100]; swprintf_s(buffer, L"Mouse down at (%d, %d)", xPos, yPos); MessageBox(hWnd, buffer, L"Mouse Event", MB_OK); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } ``` 注意事项: 1. 使用 WS_EX_TRANSPARENT 样式可以让窗口透明,并且不会阻止鼠标消息的传递。 2. 使用 SetLayeredWindowAttributes 函数设置窗口透明属性,其中第二个参数为透明色,可以设置为 RGB(255, 255, 255) 表示白色,因为白色是系统默认的透明色。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值