1
votes

I'm trying to invoke a WPF dialog from a child window of a Win32 application and was able to achieve this using the Window Handle (HWND) of the invoking window and passing it to the WPF code where I use the WindowInteropHelper class to set the Owner property to the handle of the invoking window. The code snippet below shows the WPF code where I'm setting the Owner property.

    public void ShowModal(IntPtr ownerWindow)
    {            
        WindowInteropHelper helper = new WindowInteropHelper(this);
        helper.Owner = ownerWindow;
        this.ShowDialog();
    }

I'm getting the HWND of the invoking dialog using the Windows function as shown below:

HWND handle = GetTopWindow(GetActiveWindow());

While this works as expected functionally (the WPF dialog invokes as a modal dialog), it gets invoked at the top left corner of the screen. I've even set the WindowStartupLocation="CenterOwner" property in the XAML code. It works perfectly fine when invoked from a WPF window but not when Win32 window is involved. I guess I'm missing something in the WPF-Win32 interop here although it seems to me that the problem lies with the HWND retrieved from GetTopWindow(GetActiveWindow()).

UPDATE: I replaced the above code to get the HWND of the invoking window to the code below and now the WPF dialog always invokes at the center of the screen irrespective of the position of the window that invokes it.

            HWND hWnd = GetActiveWindow();
            if (hWnd != NULL)
            {
                hWnd = FindWindowEx(hWnd, NULL, "Window1", NULL);
                if (hWnd != NULL)
                {
                    hWnd = FindWindowEx(hWnd, NULL, "Window2", NULL);
                }
            }

Here Window2 is the Window that invokes the WPF dialog.

1

1 Answers

-1
votes

In your ShowModal() can you try :

this.Owner = App.MainWindow;