以编程方式激活 Outlook
我有一个应用程序需要在用户单击按钮时激活 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这有效(您可能需要更改路径):
This works (you might have to change the path):
我有时会看到 SetForegroundWindow 失败。尝试使用 SetWindowPos 函数
I've seen SetForegroundWindow fail on occasion. Try using the SetWindowPos function
我想出了一个解决办法;我没有使用任何 WINAPI 调用,而是简单地使用了 Process.Start()。我之前也尝试过此操作,但它导致现有 Outlook 窗口调整大小,这很烦人。秘密是将
/recycle
参数传递给 Outlook,这指示它重用现有窗口。调用看起来像这样: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:为什么不尝试将 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.