When using System.Windows.Forms.ShowDialog(IWin32Window) should I be able to pass in an IWin32Window representing any window handle and have it be modal with respect to that window?
As part of an IE7 extension I'm trying to open a window modal with respect to an IE tab. It's not the currently selected tab, but I can get the hwnd of the tab ok. However, when I pass this to ShowDialog my Form is shown but it's not modal with respect to anything: I can still do things in IE, including in the tab that's supposed to be the owner. My form is shown floating above the IE windows and it stays on top, so it's not like it's just opened as a normal form but it's not correctly modal.
Using Spy++ I can find my Form and it's Owner handle is correctly set.
Does this mean that something has gone wrong, or I'm doing something wrong? Any thoughts on how to make my Form correctly modal?
fyi I'm using this wrapper class to create an IWin32Window from a hwnd (thanks Ryan!)
/// <summary>
/// Wrapper class so that we can return an IWin32Window given a hwnd
/// </summary>
public class WindowWrapper : System.Windows.Forms.IWin32Window
{
public WindowWrapper(IntPtr handle)
{
_hwnd = handle;
}
public IntPtr Handle
{
get { return _hwnd; }
}
private IntPtr _hwnd;
}
UPDATE: Using IE7 & .net 2.0
UPDATE: Playing around some more with Spy++ and the handles it exposes, I find that if I use a different hwnd then I can make my window modal to the tab:
I was using the tab's hwnd as suggested by the IWebBrowser2.HWND doc, which in Spy++ appears as class TabWindowClass. It has a child of class Shell DocObject View, which has a child of Internet Explorer_Server. If I use the hwnd of the Internet Explorer_Server then it works correctly, eg when I click with the mouse on other tabs IE reacts normally, when i click with the mouse on the tab of interest it plays the windows d'oh sound and doesn't do anything.
I don't yet know how to programatically get the Internet Explorer_Server hwnd but it should be possible.
Also FWIW while playing with other window handles I was generally able to make my form modal to other applications and dialogs. So I guess the answer to my question is 'many but not all handles'... possibly it depends on the application?
UPDATE: Another side-note: The original reason I wanted to make my form modal to the tab instead of the whole window is that when opening a MessageBox from my form, passing the form as owner, the MessageBox would not always open on top of my form. If a new IE tab had just been opened but wasn't active then the MessageBox would be hidden and that tab would start flashing. However, since IE was disabled with my form opened modal it wasn't possible to switch to that tab, so IE would be frozen. I thought that opening my form modal to the tab would solve this, but I've found another solution is to avoid using MessageBox: if I use a second form and ShowDialog(this) from my first form then the second form correctly opens to the front. So it seems that Form.ShowDialog() works better than MessageBox.Show() in some cases More discussion here.