我有一个应用程序,它需要激活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窗口?
发布于 2011-06-28 23:24:24
我找到了一个解决方案;我没有使用任何WINAPI调用,而是简单地使用了Process.Start()。我之前也试过这样做,但它导致现有的Outlook窗口调整大小,这很烦人。秘诀是将/recycle参数传递给Outlook,这将指示它重用现有窗口。调用如下所示:
Process.Start( "OUTLOOK.exe", "/recycle" );发布于 2011-06-15 16:24:11
为什么不尝试将Outlook派生为一个新的进程?我相信它是一个单项应用程序(这里我忘记了我正确的术语),所以如果它已经打开了,它只会把那个放在最前面。
发布于 2017-02-09 01:14:27
这是可行的(您可能需要更改路径):
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();
}https://stackoverflow.com/questions/6234550
复制相似问题