Howto: Display MessageBox or modal form in office applications

Source: Internet
Author: User

 

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 ,...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.