In a winformProgramMessageBox has the following features: there is a "host" form related to it, that is, MessageBox pops up in the event handler of this form and its controls, and when MessageBox is closed, the focus is automatically switched to the "host" form. modal form has the same characteristics.
However, if the winform program starts the Office application and you need to display MessageBox or modal form when processing some events in the office object model, you may encounter some problems.
For example, in a winform program, add a commandbarbutton (toolbar button) in Excel for programming, and then process the click event of this object. If a MessageBox needs to pop up in this object, when MessageBox is disabled, the focus is not switched to the Excel application. which window does the focus switch?
Assume that the following two forms exist in this window: form1 and form2. If the Excel application is started in the form2 or form2 control event, the focus will be switched to form2.
This indicates that MessageBox regards form2 as a "host" form, but its behavior violates our expectation for the program. Can we specify the "host" form of MessageBox by ourselves? After reading msdn, we can see that MessageBox. Show has an overloaded form:
MessageBox. Show (iwin32window ,...);
Obviously, we need this overload, but there is no way in the office object model to return iwin32window objects. This is a problem, isn't it?
Here are the solutions to this problem:
Public class implements wwrap: iwin32window
{
Private intptr m_handle;
Public intptr handle
{
Get {return m_handle ;}
}
Public synchronized wwrap (intptr handle)
{
M_handle = handle;
}
}
Are you clear? All we need is a handle.
[Dllimport ("user32.dll")]
Public static extern intptr findwindow (lpclassname, lpwindowname );
By introducing the Win32 API, we can easily get the handle of the main form of the Excel application. Well, I want you to know how to do it :)
Modal form is also mentioned in the title. Do you see the following method :)
Form. showdialog (iwin32window ,...