C#创建窗口 – 定义父窗口

前端之家收集整理的这篇文章主要介绍了C#创建窗口 – 定义父窗口前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想创建使用C#窗口,设置父级到我定义的句柄,
这是另一个进程窗口句柄.

有人知道怎么做吗?

问候,

解决方法

如果我正确地理解了你的问题,你应该能够通过使用这样的东西来实现你想要的东西:
  1. class Win32Window : IWin32Window
  2. {
  3. IntPtr handle;
  4.  
  5. public Win32Window(IntPtr handle) { this.handle = handle; }
  6.  
  7. public IntPtr Handle
  8. {
  9. get { return this.handle; }
  10. }
  11. }
  12.  
  13. static void Main()
  14. {
  15. IntPtr targetParent = // Get handle to the parent window
  16.  
  17. new MainForm().ShowDialog(new Win32Window(targetParent));
  18. }

这将使MainForm成为指定窗口的子窗口,使其始终显示在其上方.我在示例中使用了ShowDialog,但这也适用于Show.这是Windows窗体特有的.

在WPF中,您可以尝试以下操作:

  1. var helper = new WindowInteropHelper(/* your Window instance */);
  2.  
  3. helper.Owner = // Set with handle for the parent

我在显示WPF窗口之后很快尝试了这个,它似乎按预期工作,但WPF知识并不是那么好.

猜你在找的C#相关文章