How to use Region (Win GDI) APIs on top of Xaml Island window

Shyam Butani 235 Reputation points
2024-10-15T09:20:10.1033333+00:00

Hello,

I want to draw different shapes in WinRT/C++ application using Region APIs. It is Windows Desktop Application with Xaml Island.

I'm creating child window of the Xaml Island window, and then trying to use Region APIs to draw rectangle on this child window.

It is not working. Child window is visible on top of Xaml Island window but Region API is not painting on this child window.

Can you please help me understand why it's not working? Below is the code snippet to understand what I'm trying to do.

void DrawRectRegion(HWND hwnd) 
{
    RECT rect = { 10, 10, 200, 100 };
    HDC hdc = GetDC(hwnd);

    // Create a region
    HRGN hRegion = CreateEllipticRgn(rect.left, rect.top, rect.right, rect.bottom);

    if (hRegion != NULL) {
        // Set the region as the clipping region
        SelectClipRgn(hdc, hRegion);

        // Draw something within the clipping region
        FillRect(hdc, &rect, (HBRUSH)(COLOR_WINDOW + 2));

        // Clean up
        DeleteObject(hRegion);
    }
}

// ... creating Xaml Island container and get it's window handle - hWndXamlIsland

// Create child window
HWND child = CreateWindowExW(WS_EX_LAYERED, szWindowClass, L"Dummy text", WS_VISIBLE | WS_CHILD, 20, 20, 1000, 1000, hWndXamlIsland, NULL, NULL, NULL);

// Draw Rectangle on this child window
DrawRectRegion(child);

// ... message loop

Thanks.

Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
792 questions
Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,650 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.