VSTO Outlook Explorer BeforeMinimize,BeforeMaximize事件不会触发

时间:2014-12-19 09:57:22

标签: c# .net events outlook vsto

我花了最后几个小时搞清楚如何订阅任何Outlook资源管理器的BeforeMinimize和BeforeMaximize事件并且失败了。到目前为止我做了什么:

public partial class ThisAddIn
{
    Outlook.Explorer explorer;
    Outlook.Application application;
    Outlook.ExplorerEvents_10_BeforeMinimizeEventHandler beforeMinimizeEventHandler;

    ThisAddin_Startup()
    {
        //... create custom Task pane

        application = Globals.ThisAddIn.Application;
        explorer = application.ActiveExplorer();
        beforeMinimizeEventHandler = new Outlook.ExplorerEvents_10_BeforeMinimizeEventHandler(explorer_BeforeMinimize);
        explorer.BeforeMinimize += beforeMinimizeEventHandler;

    }

    void explorer_BeforeMinimize(ref bool Cancel)
    {
        System.Windows.Forms.MessageBox.Show("BeforeMinimize");
        Cancel = true;
    }
}

该事件永远不会被解雇。我还尝试了其他方法,例如将explorer转换为Outlook.ExplorerEvents_10_Event然后订阅。我还检查过只有一个资源管理器。但是没有任何作用。 我做错了吗?谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

对您的代码进行了一些修改,我发现现在一切正常。

我已将事件处理程序直接添加到Explorer.BeforeMinimize,而不是您添加的内容。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;

namespace OutlookAddIn4
{
    public partial class ThisAddIn
    {

        Outlook.Explorer explorer;
        Outlook.Application application;

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            application = Globals.ThisAddIn.Application;
            explorer = application.ActiveExplorer();
            explorer.BeforeMinimize += explorer_BeforeMinimize;
        }

        void explorer_BeforeMinimize(ref bool Cancel)
        {
            System.Windows.Forms.MessageBox.Show("BeforeMinimize");
            Cancel = true;
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }

        #endregion
    }
}

编译后,启动代码编译为

private void ThisAddIn_Startup(object sender, EventArgs e)
{
    this.application = Globals.ThisAddIn.Application;
    this.explorer = this.application.ActiveExplorer();
    (new ComAwareEventInfo(typeof(ExplorerEvents_10_Event), "BeforeMinimize")).AddEventHandler(this.explorer, new ExplorerEvents_10_BeforeMinimizeEventHandler(this.explorer_BeforeMinimize));
}