My application is vb6 executable, but some new forms in the system are written in C #. I would like to be able to set the owner property of a C # form using a handle in the main application window so that the dialogs stay on top when you add a tab between my application and other applications.
I can get hwnd of the main application window. I'm not sure what I can do from there?
UPDATE October 20, 2008 at 17:06:
Scott
Thanks for the answer. I overlooked that the parameter of the Show / ShowDialog method was not of type Form - I was looking only at the Owner property.
I changed the code that I use from the above a bit - we have a component that basically loads our forms and calls ShowDialog. My code is as follows:
Form launchTarget = FormFactory.GetForm(xxx); // psuedo-code for generic form loader launchTarget.StartPosition = FormStartPosition.CenterParent; IWin32Window parentWindow = GetWindowFromHwnd(hwnd); launchTarget.ShowDialog(parentWindow);
GetWindowFromHwnd is the version of your code:
private IWin32Window GetWindowFromHost(int hwnd) { IWin32Window window = null; IntPtr handle = new IntPtr(hwnd); try { NativeWindow nativeWindow = new NativeWindow(); nativeWindow.AssignHandle(handle); window = nativeWindow; } finally { handle = IntPtr.Zero; } return window; }
Unfortunately, this is not what I was hoping for. The form is displayed modally, but it does not appear in the correct position and does not stay on top when I leave and return to the parent window. Our modals do not display tasks on the taskbar, so the window would seem to βdisappearβ (although it is still on the Alt-Tab window list). This means that I may not have a suitable hwnd. If you have any other suggestions, please respond. Thanks again.
UPDATE November 10, 2008 at 16:25
One note - if you include it in a method call in an attempt to /, finally, as in Scott's second post, the call in the finally block should be:
parentWindow.ReleaseHandle();
c # winforms hwnd
mcw0933
source share