Has anyone ever tried to place a messagebox at a particular position or part of the screen ? Have you noticed any weird behaviour with messageboxes in .NET ? I was recently browsing the experts-exchange site and i found a question about positioning messageboxes.
It said that owner parameter in the MessageBox's static method, is not being considered at all while showing the actual messagebox. Here's the actual syntax for the messagebox's Show method.
public static DialogResult Show( IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, MessageBoxOptions options );
Now say inside a WinForm's Load event, if i call
MessageBox.Show(this, "Hello World !");
where this stands for the 'owner' here of type IWin32Window, meaning that the messagebox should be the modal dialog for the current window and it should be displayed in the center of its parent. But the weirdest part is yet to kick in. For some reason, even if you pass or set this parameter, it is being ignored or not considered and only the default window or the ActiveDesktop window is being taken for the IWin32Window. So what happens is that the MessageBox always shows up in the center of the screen and not anywhere else.
I think i have found out the reason for this bug or whatever the flaw is .. The MessageBox class in System.Windows.Forms namespace has a private static method with the signature
private static DialogResult ShowCore(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, MessageBoxOptions options)
This method basically checks whether the owner is null or not and then it calls the MessageBox function in the User32.dll ... Now if the passes owner to this function is null, then the activewindow handle is taken and passed on to the nativemethod call. The MessageBox then calls the SafeNativeMethods.MessageBox and passes the params that it has obtained which in turn is a wrapper around the system's MessageBox function.This function takes the window handle as one of its parameters but i think internally this is always pointing to the ActiveDesktop window's handle ... that is why the messagebox always ends up popping in the middle of screen instead of the middle of form ...
To test this out and to confirm it, i checked out the MsgBox function in VB and voila ! It gives the same expected behaviour of placing the messagebox right in the center of the screen and never on the activeform as the implementation should have been.
So basically unless MS changes the user32.dll this problem cannot be solved wrt messageboxes.
I feel releived on finding that this is NOT A BUG in the framework as many have suggested in some of the newsgroups !!