C++ 如何创建透明窗口 为游戏蒙上绘制白板?

直接上代码吧。


LRESULT WindowProc_Overlay(HWND hWnd,UINT message,WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_PAINT:
        break;
    case WM_CREATE:
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    case WM_CLOSE:
        DestroyWindow(g_OverlayWnd);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }

    return 0;
}

BOOL GameIsForegroundWindow()
{
    HWND hWnd = GetForegroundWindow();
    if (hWnd == ::FindWindow(NULL, _T("Counter-Strike Source")))
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}

void _stdcall CreateOverlayWindow()
{
    GetWindowRect(g_hWnd, &g_WinRect);
    g_WinRect.top += TOP_HEIGHT;

    WNDCLASSEX wc;
    ZeroMemory(&wc, sizeof(wc));
    wc.cbSize = sizeof(wc);
    wc.style = CS_HREDRAW | CS_VREDRAW;  //水平和垂直重绘
    wc.lpfnWndProc = (WNDPROC)WindowProc_Overlay;
    wc.hInstance = GetModuleHandle(NULL);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)RGB(0, 0, 0);
    wc.lpszClassName = _T("Overlay");

    RegisterClassEx(&wc);    // 注册窗体

    g_WndWidth = (g_WinRect.right - g_WinRect.left);
    g_WndHeight = (g_WinRect.bottom - g_WinRect.top);

    g_OverlayWnd = CreateWindowEx(WS_EX_LAYERED | WS_EX_TRANSPARENT, wc.lpszClassName,
        _T("OverlayWindow"), WS_POPUP, g_WinRect.left, g_WinRect.top,
        g_WndWidth,
        g_WndHeight,
        NULL, NULL, wc.hInstance, NULL);

    if (g_OverlayWnd == NULL)
    {
        return;
    }

    SetLayeredWindowAttributes(g_OverlayWnd, RGB(0, 0, 0), 0, ULW_COLORKEY);

    ShowWindow(g_OverlayWnd, SW_SHOW);
    UpdateWindow(g_OverlayWnd);

    MSG msg;
    ZeroMemory(&msg, sizeof(msg));

    while (msg.message != WM_QUIT)
    {
        if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }

        if (GameIsForegroundWindow() == TRUE)
        {
            SetWindowPos(g_OverlayWnd, HWND_TOPMOST, g_WinRect.left, g_WinRect.top,
                g_WndWidth,
                g_WndHeight,
                SWP_SHOWWINDOW);
        }
        else
        {
            SetWindowPos(g_OverlayWnd, HWND_TOPMOST, g_WinRect.left, g_WinRect.top,
                g_WndWidth,
                g_WndHeight,
                SWP_SHOWWINDOW);
        }

        MoveWindow(g_OverlayWnd, g_WinRect.left, g_WinRect.top,
            g_WndWidth,
            g_WndHeight,
            TRUE);

        HWND hWnd = GetForegroundWindow();
        RECT OverlayRect = { 0, 0,g_WndWidth ,g_WndHeight };

        if (hWnd == ::FindWindow(NULL, _T("Counter-Strike Source")))
        {
            HDC hDC = GetDC(g_OverlayWnd);  //此处为蒙板窗口句柄

            HBRUSH hBrush = (HBRUSH)GetStockObject(BLACK_BRUSH);     //空画刷

            SelectObject(hDC, hBrush);

            FillRect(hDC, &OverlayRect, hBrush);

            DeleteObject(hBrush);
            ReleaseDC(g_OverlayWnd, hDC);   //此处为蒙板窗口句柄
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员陈子青

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值