以编程方式激活Outlook

| 我有一个需要在用户单击按钮时激活Outlook(如果正在运行)的应用程序。我已经尝试了以下方法,但是它不起作用。 在window类中声明:
[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 );
在按钮“ 1”处理程序内:
// 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 );

  }
}
无论当前将Outlook最小化到任务栏还是最小化到系统任务栏或最大化,这都不起作用。如何激活Outlook窗口?     
已邀请:
        我想出了一个解决方案;而不是使用任何WINAPI调用,我只使用了
Process.Start()
。我也较早尝试过此方法,但它导致现有Outlook窗口的大小发生了调整,这很烦人。秘密是将“ 4”参数传递给Outlook,这指示它重新使用现有窗口。调用看起来像这样:
Process.Start( \"OUTLOOK.exe\", \"/recycle\" );
    
        为什么不尝试将Outlook生成为新进程?我相信这是一个单项应用程序(我在这里忘记了我的正确术语),因此,如果该应用程序已经打开,它将使该应用程序走在前列。     
        这可行(您可能必须更改路径):
public static void StartOutlookIfNotRunning()
{
    string OutlookFilepath = @\"C:\\Program Files (x86)\\Microsoft Office\\Office12\\OUTLOOK.EXE\";
    if (Process.GetProcessesByName(\"OUTLOOK\").Count() > 0) return;
    Process process = new Process();
    process.StartInfo = new ProcessStartInfo(OutlookFilepath);
    process.Start();
}
    
        我已经看到SetForegroundWindow有时会失败。尝试使用SetWindowPos函数     

要回复问题请先登录注册