以编程方式激活 Outlook

发布于 2024-11-12 23:15:25 字数 874 浏览 4 评论 0原文

我有一个应用程序需要在用户单击按钮时激活 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 );

在按钮 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 );

  }
}

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

I have an application which needs to activate Outlook (if it is running) when the user clicks on a button. I have tried the following but it isn't working.

Declared in the window class:

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

Within the button Click handler:

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

  }
}

This doesn't work irrespective of whether Outlook is currently minimized to taskbar or minimized to system tray or maximized. How do I activate the Outlook window?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

独留℉清风醉 2024-11-19 23:15:26

这有效(您可能需要更改路径):

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

This works (you might have to change the path):

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();
}
丶视觉 2024-11-19 23:15:26

我有时会看到 SetForegroundWindow 失败。尝试使用 SetWindowPos 函数

I've seen SetForegroundWindow fail on occasion. Try using the SetWindowPos function

如若梦似彩虹 2024-11-19 23:15:25

我想出了一个解决办法;我没有使用任何 WINAPI 调用,而是简单地使用了 Process.Start()。我之前也尝试过此操作,但它导致现有 Outlook 窗口调整大小,这很烦人。秘密是将 /recycle 参数传递给 Outlook,这指示它重用现有窗口。调用看起来像这样:

Process.Start( "OUTLOOK.exe", "/recycle" );

I figured out a solution; instead of using any WINAPI calls I simply used Process.Start(). I'd tried this earlier too but it caused the existing Outlook window to resize, which was annoying. The secret is to pass the /recycle argument to Outlook, this instructs it to reuse the existing window. The call looks like this:

Process.Start( "OUTLOOK.exe", "/recycle" );
离鸿 2024-11-19 23:15:25

为什么不尝试将 Outlook 作为一个新进程生成呢?我相信它是一个单条目应用程序(我在这里忘记了正确的术语),因此如果它已经打开,它只会将该应用程序带到最前面。

Why not try spawning Outlook as a new process? I believe it is a single-entry application (I'm forgetting my proper terminology here), so that if it is already open, it will just bring that one to forefront.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文