透明的win32窗口和文本

时间:2013-03-25 00:00:08

标签: winapi text transparent

我正在尝试制作全屏透明无边框窗口,文本在其上方完美显示。文本背景应该是透明的,但不是实际的字体面。问题是,当我不做SetWindowRgn时,我只能看到TextOut显示。我不知道我做错了什么:(

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;
   hInst = hInstance;

   DWORD Flags1 = WS_EX_COMPOSITED | WS_EX_LAYERED | WS_EX_NOACTIVATE | WS_EX_TOPMOST | WS_EX_TRANSPARENT;
   DWORD Flags2 = WS_POPUP;

   hWnd = CreateWindowEx(Flags1, szWindowClass, szTitle, Flags2, 0, 0, 1920, 1200, 0, 0, hInstance, 0);

   if(!hWnd)return FALSE;

   HRGN GGG = CreateRectRgn(0, 0, 0, 0);
   SetWindowRgn(hWnd, GGG, false);

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   DeleteObject(GGG);

   return TRUE;
}

    case WM_PAINT:
    hdc = BeginPaint(hWnd, &ps);

    SetBkMode(hdc, TRANSPARENT);
    TextOut(hdc, 50, 50, L"MY TEXT", lstrlen(L"MY TEXT"));

    EndPaint(hWnd, &ps);

1 个答案:

答案 0 :(得分:5)

解决这个问题:

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;
   hInst = hInstance;

   DWORD Flags1 = WS_EX_COMPOSITED | WS_EX_LAYERED | WS_EX_NOACTIVATE | WS_EX_TOPMOST | WS_EX_TRANSPARENT;
   DWORD Flags2 = WS_POPUP;

   hWnd = CreateWindowEx(Flags1, szWindowClass, szTitle, Flags2, 0, 0, 1920, 1200, 0, 0, hInstance, 0);

   if(!hWnd)return FALSE;

   HRGN GGG = CreateRectRgn(0, 0, 1920, 1200);
   InvertRgn(GetDC(hWnd), GGG);
   SetWindowRgn(hWnd, GGG, false);

   COLORREF RRR = RGB(255, 0, 255);
   SetLayeredWindowAttributes(hWnd, RRR, (BYTE)0, LWA_COLORKEY);

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   DeleteObject(GGG);

   return TRUE;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId, wmEvent;
    PAINTSTRUCT ps;
    HDC hdc;
    RECT rect;

    switch (message)
    {
    case WM_ERASEBKGND:

        GetClientRect(hWnd, &rect);
        FillRect((HDC)wParam, &rect, CreateSolidBrush(RGB(255, 0, 255)));

        break;