2012-03-05 52 views
1

实验的几个小时后和谷歌搜索,我终于来到了什么我可以计算出我自己的结尾,所以这里是我现在有:Outlook 2010加载项,我如何取消关闭事件,最小化窗口? (C#)

private void ThisAddIn_Startup(object sender, System.EventArgs e) 
{ 
    Application.Startup += new 
     Outlook.ApplicationEvents_11_StartupEventHandler(
     ApplicationObject_Startup); 
    ((Outlook.ApplicationEvents_11_Event)Application).Quit += new 
     Outlook.ApplicationEvents_11_QuitEventHandler(
     ApplicationObject_Quit); 
} 
void ApplicationObject_Startup() 
{ 
    MessageBox.Show("Startup Event"); 
    ((Outlook.ExplorerEvents_10_Event)Application.ActiveExplorer()).Close += new 
     Outlook.ExplorerEvents_10_CloseEventHandler(
     ExplorerObject_Close); 
} 

void ApplicationObject_Quit() 
{ 
    MessageBox.Show("Quit Event"); 
} 

void ExplorerObject_Close() 
{ 
    MessageBox.Show("Explorer Close Event"); 
} 

所有这些作品,当我关闭Outlook时,我会看到资源管理器关闭事件并按顺序退出事件消息框。然而,这个观点似乎已经关闭了,我不知道如何取消这些事件(对于其他一些事件,有一个布尔值Cancel可以设置为false,但不适用于这些事件?),或发送一个最小化事件(我一直无法如何解决这个问题)。

如果有人有任何建议,我会很感激。我在工作中有一些闲暇时间,并且认为我会尝试学习一些插件开发的东西,并且同时解决一个非常讨厌的前景部分!

编辑:我也曾尝试:

Application.ActiveExplorer().WindowState = Outlook.OlWindowState.olMinimized; 

在启动时只需立即最小化窗口。它最大限度地减少了前景,但不会将系统托盘放到系统托盘上(这很有趣,可能实际上是一个错误,因为最小化设置为最小化托盘...)但是,如果我可以摆脱关闭/退出事件(S)我可以至少只是最小化任务栏的窗口。

+0

据我所知,无法阻止Outlook关闭AddIn。 – user7116 2012-03-05 20:31:34

+0

如果不在Addin那么如何?你有没有资源可以指给我? – paycheck87 2012-03-05 20:56:51

+0

和我只是得到虚假的希望通过看到设置的方法的返回值从VB取消事件的错误? http://msdn.microsoft.com/en-us/library/ff862184.aspx – paycheck87 2012-03-05 20:58:21

回答

2
private void ThisAddIn_Startup(object sender, System.EventArgs e) 
    { 
     try 
     { 
      // Assign startup and quit events 
      Application.Startup += new Outlook.ApplicationEvents_11_StartupEventHandler(ApplicationObject_Startup); 
      ((Outlook.ApplicationEvents_11_Event)Application).Quit += new Outlook.ApplicationEvents_11_QuitEventHandler(ApplicationObject_Quit); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message, "EXCEPTION", MessageBoxButtons.OK, MessageBoxIcon.Error); 
     } 
    } 

    private void ThisAddIn_Shutdown(object sender, System.EventArgs e) 
    { 
     try 
     { 
      // Remove the startup and quit events 
      Application.Startup -= new Outlook.ApplicationEvents_11_StartupEventHandler(ApplicationObject_Startup); 
      ((Outlook.ApplicationEvents_11_Event)Application).Quit -= new Outlook.ApplicationEvents_11_QuitEventHandler(ApplicationObject_Quit); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message, "EXCEPTION", MessageBoxButtons.OK, MessageBoxIcon.Error); 
     } 
    } 

    void ApplicationObject_Startup() 
    { 
     try 
     { 
      // Minimize to taskbar 
      Application.ActiveExplorer().WindowState = Outlook.OlWindowState.olMinimized; 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message, "EXCEPTION", MessageBoxButtons.OK, MessageBoxIcon.Error); 
     } 
    } 

    void ApplicationObject_Quit() 
    { 
     try 
     { 
      // Restart outlook minimized 
      ProcessStartInfo psiOutlook = new ProcessStartInfo("OUTLOOK.EXE", "/recycle"); 
      psiOutlook.WindowStyle = ProcessWindowStyle.Minimized; 
      Process.Start(psiOutlook); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message, "EXCEPTION", MessageBoxButtons.OK, MessageBoxIcon.Error); 
     } 
    } 

这不是很干净,但这对我很有用。它只是在您关闭Outlook时启动一个新的Outlook实例,并始终在启动时最小化Outlook。主要缺点是,如果不先禁用外接程序或在任务管理器中查杀“outlook.exe”,则无法再真正关闭Outlook。不是最好的解决方案,但它确实可以确保您永远不会错过电子邮件或日历提醒,因为您意外关闭了Outlook。

编辑(10/02/12):我更新了代码的重启展望部分。它现在用/ recycle开关启动outlook.exe。这会尝试在现有窗口中重新加载Outlook。它还使用最小化的窗口样式重新启动Outlook。这可以防止Outlook的加载窗口出现。