直接上代码吧。
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); //此处为蒙板窗口句柄
}
}
}