none
如何将WinForm窗口的拥有者设定为WPF窗口 RRS feed

  • 问题

  • 如何将WinForm窗口的拥有者设定为WPF窗口  如何实现:Form.Owner=WPFWindow

    注明:不是MDI父子窗口.只让WinForm总在WPF指定的单一窗口之上非所有窗口置顶

    2011年11月9日 5:25

答案

  • 参考下面代码:

    [DllImport("user32.dll")]
    
    private static extern int SetWindowLong(HandleRef hWnd, int nIndex, int dwNewLong);
    
    /// <summary>
    /// sets the owner of a System.Windows.Forms.Form to a System.Windows.Window
    /// </summary>
    /// <param name="form"></param>
    /// <param name="owner"></param>
    public static void SetOwner(System.Windows.Forms.Form form, System.Windows.Window owner)
    {
        WindowInteropHelper helper = new WindowInteropHelper(owner);
        SetWindowLong(new HandleRef(form, form.Handle), -8, helper.Handle.ToInt32());
    }
    


     

    best regards,
    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年11月11日 5:48
    版主
  • 感谢回复,也许你写的代码哪里有错误,没有测试成功,不过,我已解决此问题,非API方法,参考网上示例.

    另外,我曾经试用过另外的API,但是变成了MDI了,非想要效果.

    2011年11月12日 11:46

全部回复

  • 参考下面代码:

    [DllImport("user32.dll")]
    
    private static extern int SetWindowLong(HandleRef hWnd, int nIndex, int dwNewLong);
    
    /// <summary>
    /// sets the owner of a System.Windows.Forms.Form to a System.Windows.Window
    /// </summary>
    /// <param name="form"></param>
    /// <param name="owner"></param>
    public static void SetOwner(System.Windows.Forms.Form form, System.Windows.Window owner)
    {
        WindowInteropHelper helper = new WindowInteropHelper(owner);
        SetWindowLong(new HandleRef(form, form.Handle), -8, helper.Handle.ToInt32());
    }
    


     

    best regards,
    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年11月11日 5:48
    版主
  • 感谢回复,也许你写的代码哪里有错误,没有测试成功,不过,我已解决此问题,非API方法,参考网上示例.

    另外,我曾经试用过另外的API,但是变成了MDI了,非想要效果.

    2011年11月12日 11:46