VSTO Outlook 资源管理器之前最小化,之前最大化事件不会触发

本文关键字:事件 Outlook 资源管理器 最小化 VSTO 最大化 | 更新日期: 2023-09-27 18:34:39

我花了最后几个小时弄清楚如何订阅任何Outlook资源管理器的BeforeT最小化和BeforeMaxim事件,但失败了。到目前为止我做了什么:

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然后订阅。我还检查了只有一个资源管理器。不过,没有任何效果。我做错了什么吗?谢谢你的帮助。

VSTO Outlook 资源管理器之前最小化,之前最大化事件不会触发

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

我已将事件处理程序直接添加到资源管理器.之前最小化,而不是您添加的内容。

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));
}