C # Creating a window - defining a parent window

I want to create using a C# window with the parent set for my specific handle, this is another handle to the process window.

Does anyone know how to do this?

Hi,

+6
source share
2 answers

If I understand your question correctly, you can achieve what you want using something like this:

 class Win32Window : IWin32Window { IntPtr handle; public Win32Window(IntPtr handle) { this.handle = handle; } public IntPtr Handle { get { return this.handle; } } } static void Main() { IntPtr targetParent = // Get handle to the parent window new MainForm().ShowDialog(new Win32Window(targetParent)); } 

This will turn MainForm child window of the specified window, which will always be displayed above it. I am using ShowDialog in the example, but this should also work for Show . This is typical of Windows Forms.

In WPF, you can try the following:

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

I quickly tried this after showing the WPF window and it seemed to work as expected, but WPF knowledge is not so great.

+7
source

I believe Handle will be read-only; therefore, the .Parent property is read-only. However, the .Owner property has a getter and setter ( MSDN link ) ... however you must have a link to the parent window.

Without additional information, I cannot provide much more.

If your parent candidate is an unmanaged window, check this link.

+3
source

Source: https://habr.com/ru/post/889160/


All Articles