Imp Eun
Imp Eun

Reputation: 1

Why does IsWindow() function (winuser.h) not recognize OpenTK's WindowPtr as a valid window handle?

I'm working on an OpenTK project in C# to create a transparent window that lets mouse events pass through. I want to make this OpenTK window click-through by adjusting its extended window styles. However, I'm running into an issue where the IsWindow function doesn't recognize WindowPtr from OpenTK as a valid window handle.

Here is a relevant part of my code:

[DllImport("user32.dll", SetLastError = true)]
private static extern bool IsWindow(IntPtr hWnd);

public static void MakeWindowClickThrough(IntPtr hWnd)
{
        int exStyle = GetWindowLong(hWnd, GWL_EXSTYLE);
        SetWindowLong(hWnd, GWL_EXSTYLE, exStyle | WS_EX_LAYERED | WS_EX_TRANSPARENT);
}

protected override void OnRenderFrame(FrameEventArgs e)
{
    base.OnRenderFrame(e);

    if (!_clickThroughSet)
    {
        IntPtr hWnd = (nint)WindowPtr;
        if (IsWindow(hWnd))
        {
            MakeWindowClickThrough(hWnd);
            _clickThroughSet = true;
            Console.WriteLine("Click-through style set.");
        }
        else
        {
            Console.WriteLine("Waiting for a valid window handle...");
        }
    }

    // ... rest of the rendering code
}

In OnRenderFrame, I try to check if the WindowPtr is a valid window handle using IsWindow(hWnd). However, IsWindow consistently returns false, and the console prints "Waiting for a valid window handle...". My questions:

  1. Why is IsWindow not recognizing WindowPtr from OpenTK as a valid window handle?
  2. Is there a different way to get a valid handle that IsWindow would recognize, or am I misusing WindowPtr?
  3. Has anyone encountered this issue when working with OpenTK, and is there an alternative approach for setting click-through on an OpenTK window?

Upvotes: -1

Views: 48

Answers (1)

Damien_The_Unbeliever
Damien_The_Unbeliever

Reputation: 239636

If you want to obtain an HWND for making any Win32 API calls related to windows, you should be passing your WindowPtr to the GetWin32Window function.

Even if multiple frameworks or libraries have a concept of a "Window Handle" it doesn't mean that those frameworks will recognise each others handles or know what to do with them, in general.

Upvotes: 1

Related Questions