Set Form Owner from HWND

B

Brian Reed

I am trying to set the owner of my modeless dialog from an HWND that I have
to the main form. I tried creating an IntPtr and then calling
Form.FromHandle(), but that returned a null control.

Here is my code:

IntPtr ipFw = new IntPtr(m_pFramework.GetMainWindow());
SWF.Control ctrl = SWF.Form.FromHandle(ipFw); // Control comes back as
NULL.

SWF.Form frmParent = (SWF.Form)ctrl;



Any thoughts?

Thanks, Brian
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi Brian,

Control.FromHandle return the control that is currently associated with the
specified handle.
Which means that if you have an HWND for the window you haven't created
using the WindowsForms classes the method will return *null* because no such
an object exists.

If you haven't created the window your own the only way I can think of to
set the owner of the form is to use PInvoke and say SetParent API. However I
haven't try this and I don't know if everything is going to be ok since the
form won't have a chance to update its internal stuff. Anyways, don't forget
that the owner and the owned form has to be created from the same process
 
G

Guest

I ended up using SetWindowLong with GWL_HWNDPARENT. This sounds like it might set the parent type, but it really sets the owner of the window

Thanks for your help. I was hoping there would a .NET way to parent to an HWND.
 
C

Claes Bergefall

It is. Override the CreateParams property. The corresponding structure
has a parent property where you can put your HWND

/claes


Brian said:
I ended up using SetWindowLong with GWL_HWNDPARENT. This sounds like it
might set the parent type, but it really sets the owner of the window.
Thanks for your help. I was hoping there would a .NET way to parent to an
HWND.
 
S

Saurabh

I used this methos to set the parent window (unmanaged) of the .NET form
(Managed) but by doing this, the parent does not know about this child. so
in case of MDI Client, I could not get it to Ctrl - Tab to my inserted form.
When I maximise the other child windows, they maximise into the parent and i
get the close / minimise / maximise buttons below the main one. but in case
of the .NET form, it maximises only to the client area. Has anybody done
this properly?

TIA,

--Saurabh

Brian said:
I ended up using SetWindowLong with GWL_HWNDPARENT. This sounds like it
might set the parent type, but it really sets the owner of the window.
Thanks for your help. I was hoping there would a .NET way to parent to an
HWND.
 
S

Stoitcho Goutsev \(100\) [C# MVP]

I ended up using SetWindowLong with GWL_HWNDPARENT. This sounds like it
might set the parent type, but it really sets the owner of the window.

Setting parent for overlapped and popup windows creates owner/owned relation
ship. Using SetWindowLong is correct, but SetParent has simpler portotype
and do the same thing in the case you use it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top