ASP.NET 에서 System.Windows.Form.MessageBox.Show alway on Top

2009.08.15 13:03 개발언어/ASP.NET

ASP.NET에서 일반창을 띄우거나 Window MessageBox을 띄우면 웹페이지 뒤로 가는 문제가 있다..
이건 웹팝업이외에는 쓰지를 못한다.

그래서 찾아보니 두가지 경우가 있었다. Web팝업을 만들어 아래와 같이 쓰거나
Windows MessageBox를 수정하여 Alway on Top (항상 위에) 나오게 만드는 방법이다.


이런것도 있다.
http://www.codeproject.com/KB/aspnet/ASP.Net_MessageBox.aspx

Website With MessageBox Interface








ASP.NET의 WebForm을 이용할때 System.Windows.Form.MessageBox.Show를 이용하여
팝업을 처리할 겨우가 생긴다.

그런데 문제는 WebForm뒤로 숨어버리는 게 문제가 된다.

이를 해결하는 방법이다.

http://www.codeproject.com/KB/install/TopMostMessageBox.aspx

Screenshot - TopMostMessageBox.jpg

Introduction

Problem

I was writing some custom installer code for a Visual Studio installer project and had to display messages to the user in my custom installer class. The problem was that when using the standard System.Windows.Forms.MessageBox.Show method the message box was appearing behind the main installer window. There are no options on the MessageBox class to alter this behavior. I could just see the support lines lighting up with calls of the install hanging.

Solution

The solution was to make the window owning the MessageBox a TopMost window. One difficulty with that was that the custom installer class does not have a reference to the main installer window and has no UI of its own. So, a new form had to be created within my custom installer class which could be the owner of the MessageBox. Setting this form as a TopMost window causes the MessageBox to be a TopMost window as well. Thus, the messages displayed by my custom installer would always be visible. Special actions are taken to ensure this new form is not visible since it would merely be a distraction to the user.

This solution was packaged into a wrapper class called TopMostMessageBox.

Using the code

Here is how you could use the TopMostMessageBox in your code.

Collapse
TopMostMessageBox.Show(

"This will appear in a message box that is a topmost window",

"Title", MessageBoxButtons.AbortRetryIgnore);

Here is the declaration of the TopMostMessageBox class.

Collapse
static public class TopMostMessageBox

{

static public DialogResult Show(string message)

{

return Show(message, string.Empty, MessageBoxButtons.OK);

}


static public DialogResult Show(string message, string title)

{

return Show(message, title, MessageBoxButtons.OK);

}


static public DialogResult Show(string message, string title,

MessageBoxButtons buttons)

{

// Create a host form that is a TopMost window which will be the



// parent of the MessageBox.



Form topmostForm = new Form();

// We do not want anyone to see this window so position it off the



// visible screen and make it as small as possible



topmostForm.Size = new System.Drawing.Size(1, 1);

topmostForm.StartPosition = FormStartPosition.Manual;

System.Drawing.Rectangle rect = SystemInformation.VirtualScreen;

topmostForm.Location = new System.Drawing.Point(rect.Bottom + 10,

rect.Right + 10);

topmostForm.Show();

// Make this form the active form and make it TopMost



topmostForm.Focus();

topmostForm.BringToFront();

topmostForm.TopMost = true;

// Finally show the MessageBox with the form just created as its owner



DialogResult result = MessageBox.Show(topmostForm, message, title,

buttons);

topmostForm.Dispose(); // clean it up all the way




return result;

}

}

Points of Interest

  • I tried to make this wrapper mimic the existing MessageBox.Show method, but many overrides of this method are not included in this simple wrapper. However, they can easily be added if you need them.
  • The hidden form is created off the visible screen to prevent any flickering. This is done by finding the size of the virtual desktop and positioning the hidden form just off past the right side of it. I do not use the physical screen size to account for multiple monitors.
  • The combination of Focus(), BringToFront(), and TopMost seemed to be needed to get the MessageBox to show up properly and have input focus.
  • To clean up the hidden form completely I call Dispose on the form.

Other Options

I could have used the MessageBoxIndirect function to accomplish the same thing. However, this is a bit simpler as it does not involve reproducing any Win32 structures.

History

1.0 - 21 Apr 07 - First Posting

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Vance Kessler


Member
I have been developing .NET applications since 2001 and have been working in software development since 1989.
Occupation: Web Developer
Location: United States United States
신고