How do I set an umanaged dialog as the Owner of a WinForm Form?
-
Wednesday, April 20, 2011 6:48 PMI need to be able to set a WinForm dialog's Owner's HWND. In unmanaged I have a background thread that gets the HWND for the window in front. The code then calls ::GetParent(frontHWND) to see if it needs to hide a different non-modal MFC dialog. When the WinForm dialog is the frontHWND, I always get NULL back for the GetParent call. I have also tried GetOwner realizing .Net tried to cleanup the difference between Parent and Owner. Looking at the WinForm dialog w/ Spy++, it also say the WinForm has no parent or owner. I have passed in NativeWindow ^natWin = gcnew NativeWindow(); natWin->AssignHandle(IntPtr(hwndParent)); managedDlg->ShowDialog(natWin); The above code didn't set the owner of the WinForm. I tried calling the Win32 SetParent from the WinForm code in OnFormShown(), but the locked up the MFC application and the WinForm. Can someone explain how to get my unmanaged dialog/app to be the owner/parent of the managed winform?
All Replies
-
Wednesday, April 20, 2011 6:52 PMModeratorThe Form will have a Handle property which will give you the HWND of the form window.
http://blog.voidnish.com -
Wednesday, April 20, 2011 7:11 PMI can get the Form's handle. I can't get the form's owner's/parent's handle, b/c when calling the Win32 call GetParent(formHwnd), it returns NULL. Also, Spy++ says the Form's Parent/Owner is NULL.
-
Wednesday, April 20, 2011 7:34 PMModerator
I can get the Form's handle. I can't get the form's owner's/parent's handle, b/c when calling the Win32 call GetParent(formHwnd), it returns NULL. Also, Spy++ says the Form's Parent/Owner is NULL.
Yeah, so what's wrong with that? That probably means the form is a top level window.
http://blog.voidnish.com -
Wednesday, April 20, 2011 7:42 PM
I open the Form by calling NativeWindow ^natWin = gcnew NativeWindow(); natWin->AssignHandle(IntPtr(hwndParent)); managedDlg->ShowDialog(natWin); Therefore, I'm expecting the call to ::GetParent(formHwnd) to return me the HWND that I passed in when I opened the Form with ShowDialog(natWin) earlier. BTW, thanks for the help. -
Wednesday, April 20, 2011 8:13 PMModeratorApparently, that alone does not do what you think it will. You may also need to explicitly set the Parent property.
http://blog.voidnish.com -
Thursday, April 21, 2011 9:15 AMModerator
Hi byronasdf,
I think the GetWindow Function is what you want.
Retrieves a handle to a window that has the specified relationship (Z-Order or owner) to the specified window.
The following is my test code, so you can reference it:
[DllImport("user32.dll", SetLastError = true)] static extern IntPtr GetWindow(IntPtr hWnd, GetWindow_Cmd uCmd); enum GetWindow_Cmd : uint { GW_HWNDFIRST = 0, GW_HWNDLAST = 1, GW_HWNDNEXT = 2, GW_HWNDPREV = 3, GW_OWNER = 4, GW_CHILD = 5, GW_ENABLEDPOPUP = 6 } private void button2_Click(object sender, EventArgs e) { this.label3.Text = GetWindow(this.Handle, GetWindow_Cmd.GW_OWNER).ToString("x"); }
Since I'm not familiar with C++ syntax, so I use the C# code test this function, you just need to use the same function in your C++ project.
And in the native code, you just need to use the SetParent function to set the managed Form as a child to the native window.
If there's any concern, please feel free to let me know.
Have a nice day!
Mike [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.

- Proposed As Answer by Mike Dos ZhangMicrosoft Contingent Staff, Moderator Tuesday, April 26, 2011 6:17 AM
- Marked As Answer by byronasdf Tuesday, April 26, 2011 7:53 PM
-
Tuesday, April 26, 2011 6:16 AMModeratorHi byronasdf,I am writing to check the status of the issue on your side.What about this problem now?Would you mind letting us know the result of the suggestions?Best wishes,
Mike [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.

-
Tuesday, April 26, 2011 7:53 PMSorry for the delay. Calling GetWindow(managedHwnd, GW_OWNER) returns the HWND that I wrap then pass into managedDlg->ShowDialog(WrappedNatWindow). It will also return the HWND of my app if I don't pass anything into ShowDialog(). I haven't had time to go back and test the GetParent(), GetOwner() and understand better why the GetOwner didn't return the HWND I was expecting. I also want to test calling the Win32 SetParent(mngdHwnd, UnmngdHwnd) and see why it was causing my dialog and app to lock up. I'll try to repost back here if I ever make time to retest. Thanks for the help.
-
Thursday, April 28, 2011 5:09 AMModerator
Hi byronasdf,
Owner is the *Window responsible for a control or dialog (for example, responsible for creating/destroying the window).
Parent is the next-senior window* to a control or dialog in the window chain, but isn't actually responsible for it (doesn't necessarily care about its lifecycle, etc). A window's parent can also be its owner.
*Window vs window: Window is an actual window displayed on the screen; window is any object with a HWND (includes buttons, panels, etc).
GetOwner gets the owner of a non-child window
GetParent gets the parent of a child window window
Best wishes,
Mike [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.

- Marked As Answer by Mike Dos ZhangMicrosoft Contingent Staff, Moderator Tuesday, June 21, 2011 10:33 AM


