以编程方式激活Outlook

Pra*_*ian 1 c# outlook .net-3.5 outlook-2010

我有一个应用程序,当用户单击按钮时需要激活Outlook(如果它正在运行).我尝试过以下但是没有用.

在窗口类中声明:

[DllImport( "user32.dll" )]
private static extern bool SetForegroundWindow( IntPtr hWnd );
[DllImport( "user32.dll" )]
private static extern bool ShowWindowAsync( IntPtr hWnd, int nCmdShow );
[DllImport( "user32.dll" )]
private static extern bool IsIconic( IntPtr hWnd );
Run Code Online (Sandbox Code Playgroud)

在按钮Click处理程序中:

// Check if Outlook is running
var procs = Process.GetProcessesByName( "OUTLOOK" );

if( procs.Length > 0 ) {
  // IntPtr hWnd = procs[0].MainWindowHandle; // Always returns zero
  IntPtr hWnd = procs[0].Handle;

  if( hWnd != IntPtr.Zero ) {
    if( IsIconic( hWnd ) ) {
      ShowWindowAsync( hWnd, SW_RESTORE );

    }
    SetForegroundWindow( hWnd );

  }
}
Run Code Online (Sandbox Code Playgroud)

无论Outlook当前是最小化到任务栏还是最小化到系统托盘或最大化,这都不起作用.如何激活Outlook窗口?

Pra*_*ian 6

我想出了一个解决方案; 而不是使用我简单使用的任何WINAPI调用Process.Start().我之前也试过这个,但它导致现有的Outlook窗口调整大小,这很烦人.秘诀是将/recycle参数传递给Outlook,这指示它重用现有窗口.电话看起来像这样:

Process.Start( "OUTLOOK.exe", "/recycle" );
Run Code Online (Sandbox Code Playgroud)