Attach window to window of another process

后端 未结 3 1946
北海茫月
北海茫月 2020-12-09 14:18

My WPF application has more than one window, I want to attach some of these windows to a window of another process. My problem is that once I attach my window it becomes inv

相关标签:
3条回答
  • 2020-12-09 14:49

    I'm not sure what you need to do with overlapped windows, but from MSDN:

    For compatibility reasons, SetParent does not modify the WS_CHILD or WS_POPUP window styles of the window whose parent is being changed. Therefore, if hWndNewParent is NULL, you should also clear the WS_CHILD bit and set the WS_POPUP style after calling SetParent. Conversely, if hWndNewParent is not NULL and the window was previously a child of the desktop, you should clear the WS_POPUP style and set the WS_CHILD style before calling SetParent.

    0 讨论(0)
  • 2020-12-09 14:50
    private void TryToAttach(IntPtr ownerHandle)
    {
        if(ownerHandle == IntPtr.Zero)
        {
            return;
        }
        try
        {
            var helper = new WindowInteropHelper(window) { Owner = ownerHandle };
        }
        catch(Exception e)
        {
            Logger.Error(e, "Could not attach window.");
        }
    }
    
    0 讨论(0)
  • 2020-12-09 15:13

    I found that I could do this without even using the setParent call. I used HwndSource class as follows:

    MyWindow window = new MyWindow();
    window.ShowActivated = true;
    
    HwndSourceParameters parameters = new HwndSourceParameters();
    
    parameters.WindowStyle = 0x10000000 | 0x40000000;
    parameters.SetPosition(0, 0);
    parameters.SetSize((int)window.Width, (int)window.Height);
    parameters.ParentWindow = newParent;
    parameters.UsesPerPixelOpacity = true;
    HwndSource src = new HwndSource(parameters);
    
    src.CompositionTarget.BackgroundColor = Colors.Transparent;
    src.RootVisual = (Visual)window.Content;
    

    This is working great now without any problems.

    0 讨论(0)
提交回复
热议问题