GetActiveWindow() as a modal dialog owner

Topics: Developer Forum
Apr 18, 2008 at 12:28 AM

if (parent == IntPtr.Zero)
	parent = Native.Windows.GetActiveWindow();
// use parent as an owner to a modal dialog
if the user is really unlucky, this will make the desktop window owner of the modal dialog. First thing a modal dialog does is disabling its owner. When you disable a window, all its child windows are disabled as well. You see where this is going. IntPtr.Zero is a perfectly valid value for the owner - causing the dialog to be unowned, instead of disabling random windows...
Apr 18, 2008 at 8:00 AM
Don't worry: cannot happen because GetActiveWindow() returns the current active window attached to the calling thread. ([url:Documentation on MSDN|
By the way, System.Windows.Forms.MessageBox does the exact same thing (check with Reflector), therefore I bet it's safe and legit to do it. ;)
Apr 18, 2008 at 10:32 AM
Oh, then it's probably fine. But how does one make an unowned dialog / message box then?
Apr 18, 2008 at 11:11 PM
To create an unowned dialog (not modal) you'll have to create an instance of TaskDialog (which makes sense, because you'll most likely will have to keep a reference to the dialog) and then call Show() with a null pointer.
However, the static methods try to map as closely as possible to those of MessageBox, I think it's safe to assume that TaskDialogs will most likely be used as standard modal dialogs.