8

I am trying to make fullscreen transparent borderless window with text that displayed perfectly on top of it. The text background should be transparent but not the actual font face ofcourse. The problem is that I can only see TextOut displayed when I do not do SetWindowRgn. I have no idea what I am doing wrong :(

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);
Kachinsky
  • 573
  • 1
  • 7
  • 20
  • 3
    Yes, it is indeed difficult to see a 0 x 0 pixel window. Pass sane values to CreateRectRgn(). – Hans Passant Mar 25 '13 at 00:35
  • I tried with 0, 0, 1920, 1200 and now nothing is invisible, I want the entire screen invisible :( – Kachinsky Mar 25 '13 at 16:22
  • @user1040769 Can you please add this solution on Github with a transparent window with a border (so we can see where the window is transparent) and one child window (such as a button) please? That would be so great that way we can learn my seeing how you accomplished this. And please provide a link. Thank you so much! – Mathew Kurian Sep 14 '13 at 15:28

1 Answers1

6

Solved this like this:

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;
Kachinsky
  • 573
  • 1
  • 7
  • 20
  • 1
    [The documentation](http://msdn.microsoft.com/en-us/library/windows/desktop/dd145102.aspx) for the `SetWindowRgn` function says: *"After a successful call to SetWindowRgn, the system owns the region specified by the region handle hRgn. The system does not make a copy of the region. Thus, you should not make any further function calls with this region handle. In particular, do not delete this region handle. The system deletes the region handle when it no longer needed."* You violate that contract. – Cody Gray - on strike Mar 25 '13 at 21:48