非常抱歉,由于我是语言模型AI,有些信息没有顾及到。关于窗口类的注册问题,以下是更正后的代码:
class DesktopMaskLayer {
public:
DesktopMaskLayer();
~DesktopMaskLayer();
bool init();
void show();
void hide();
void repaint();
private:
HWND m_hWnd;
HDC m_hDC;
HBITMAP m_hBitmap;
UINT m_nWidth;
UINT m_nHeight;
static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
};
DesktopMaskLayer::DesktopMaskLayer() {
m_hWnd = NULL;
m_hDC = NULL;
m_hBitmap = NULL;
m_nWidth = 0;
m_nHeight = 0;
}
DesktopMaskLayer::~DesktopMaskLayer() {
if (m_hBitmap) DeleteObject(m_hBitmap);
if (m_hDC) DeleteDC(m_hDC);
}
bool DesktopMaskLayer::init() {
bool result = false;
m_hDC = GetDC(NULL);
m_nWidth = GetSystemMetrics(SM_CXVIRTUALSCREEN);
m_nHeight = GetSystemMetrics(SM_CYVIRTUALSCREEN);
m_hBitmap = CreateCompatibleBitmap(m_hDC, m_nWidth, m_nHeight);
if (m_hBitmap) {
HINSTANCE hInstance = GetModuleHandle(NULL);
WNDCLASSEX wcex = { 0 };
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.lpfnWndProc = WndProc;
wcex.hInstance = hInstance;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.lpszClassName = _T("DesktopMaskLayer");
if (RegisterClassEx(&wcex)) {
m_hWnd = CreateWindowEx(WS_EX_LAYERED | WS_EX_TRANSPARENT | WS_EX_TOPMOST,
_T("DesktopMaskLayer"), _T(""), WS_POPUP,
GetSystemMetrics(SM_XVIRTUALSCREEN),
GetSystemMetrics(SM_YVIRTUALSCREEN), m_nWidth, m_nHeight, NULL, NULL, hInstance, NULL);
if (m_hWnd) {
SetLayeredWindowAttributes(m_hWnd, RGB(0, 0, 0), 128, LWA_COLORKEY | LWA_ALPHA);
ShowWindow(m_hWnd, SW_SHOW);
result = true;
}
}
}
return result;
}
void DesktopMaskLayer::show() {
if (m_hWnd) SetWindowPos(m_hWnd, HWND_TOPMOST, GetSystemMetrics(SM_XVIRTUALSCREEN), GetSystemMetrics(SM_YVIRTUALSCREEN), m_nWidth, m_nHeight, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);
repaint();
}
void DesktopMaskLayer::hide() {
if (m_hWnd) SetWindowPos(m_hWnd, HWND_BOTTOM, GetSystemMetrics(SM_XVIRTUALSCREEN), GetSystemMetrics(SM_YVIRTUALSCREEN), m_nWidth, m_nHeight, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);
}
void DesktopMaskLayer::repaint() {
if (m_hWnd && m_hDC && m_hBitmap) {
HDC hMemDC = CreateCompatibleDC(m_hDC);
if (hMemDC) {
HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemDC, m_hBitmap);
// 在此处调用API函数,绘制遮罩效果
BitBlt(hMemDC, 0, 0, m_nWidth, m_nHeight, NULL, NULL, NULL, BLACKNESS);
SelectObject(hMemDC, hOldBitmap);
DeleteDC(hMemDC);
UpdateLayeredWindow(m_hWnd, NULL, NULL, NULL, m_hDC, NULL, RGB(0, 0, 0), NULL, ULW_COLORKEY | ULW_ALPHA);
}
}
}
LRESULT CALLBACK DesktopMaskLayer::WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
}
return 0;
case WM_CLOSE:
{
DestroyWindow(hWnd);
}
return 0;
case WM_DESTROY:
{
PostQuitMessage(0);
}
return 0;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
MSG msg;
DesktopMaskLayer maskLayer;
if (maskLayer.init()) {
maskLayer.show();
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
maskLayer.hide();
}
return 0;
}
对窗口类的注册进行了修改,修正了代码错误。