创建跟随蒙版窗口的代码
本帖最后由 Panel 于 2023-3-7 16:53 编辑用途很多,自己探索
效果图如下:
```cpp
#include <windows.h>
HWND hWnd;
HWND hOverlayWnd; //蒙版窗口句柄
RECT winRect; //窗口属性 尺寸
HDC hdc; //设备上下文
HBRUSH hbrush; //画刷
HPEN hPen; //画笔
void SetLayerTransparent(HWND hWnd)
{
BOOL isTransParent = FALSE;
if (isTransParent == FALSE)
{
DWORD exStyle = GetWindowLong(hWnd, GWL_EXSTYLE);
exStyle |= WS_EX_LAYERED;
SetWindowLong(hWnd, GWL_EXSTYLE, exStyle);
SetLayeredWindowAttributes(hWnd, RGB(0, 0, 0), 200, LWA_ALPHA);
isTransParent = TRUE;
}
else
{
SetLayeredWindowAttributes(hWnd, RGB(0, 0, 0), 200, LWA_ALPHA);
isTransParent = FALSE;
}
}
LRESULT OverWndMsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg)
{
//窗口重绘 窗口绘制消息
case WM_DESTROY:
PostQuitMessage(0);
case WM_CLOSE:
DestroyWindow(hWnd);
break;
default:
break;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
int CALLBACK WinMain(HINSTANCE hIns, HINSTANCE hPreIns, LPSTR lpCmdLine, int nShowcmd)
{
hWnd = FindWindowA(NULL, "[ LordPE Deluxe ] by yoda");//需要覆盖的窗口标题
MSG msg; //windows消息
ZeroMemory(&msg, sizeof(msg)); //初始化窗口信息
GetWindowRect(hWnd, &winRect); //获取CSGO窗口相关属性信息 大小 尺寸 位置
//开始创建通明窗口
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(wc)); //初始化窗口类
wc.cbSize = sizeof(wc); //窗口的大小,一般都用sizeof获取标准大小
wc.style = CS_HREDRAW | CS_VREDRAW; //水平和垂直重绘
wc.lpfnWndProc = (WNDPROC)OverWndMsgProc; //窗口消息回调函数 用来记录你在窗户上进行的一些操作
//并返回操作的结果
wc.hInstance = GetModuleHandle(NULL); //窗口实例句柄,不依赖任何项
wc.hCursor = LoadCursor(NULL, IDC_ARROW); //窗口光标句柄 样式
wc.hbrBackground = (HBRUSH)RGB(0, 0, 0);//窗口背景颜色 白色
wc.lpszClassName = L"OverlayWindow"; //窗口类的名称
RegisterClassEx(&wc); //注册窗口类
//创建蒙版窗口
hOverlayWnd = CreateWindowEx(WS_EX_LAYERED | WS_EX_TRANSPARENT, wc.lpszClassName, L"over", WS_POPUP, winRect.left, winRect.top, winRect.right - winRect.left, winRect.bottom - winRect.top, NULL, NULL, wc.hInstance, NULL);
//判断窗口是否创建成功
if (hOverlayWnd == NULL) {
return 0;
}
//SetLayeredWindowAttributes(hOverlayWnd, RGB(0, 0, 0), 100, ULW_COLORKEY); //设置窗口透明度
SetLayerTransparent(hOverlayWnd);
ShowWindow(hOverlayWnd, SW_SHOW); //显示窗口
UpdateWindow(hOverlayWnd);
while (msg.message != WM_QUIT)
{
//确保窗口始终在最前面
SetWindowPos(hOverlayWnd, HWND_TOPMOST, winRect.left, winRect.top, winRect.right - winRect.left, winRect.bottom - winRect.top, SWP_SHOWWINDOW);
GetWindowRect(hWnd, &winRect);
//处理蒙版窗口消息
if (PeekMessage(&msg, hOverlayWnd, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg); //把虚拟键盘消息转化为字符消息
DispatchMessage(&msg); //把消息教给消息窗体
}
}
}
```
学到了~~~~
页:
[1]