以编程方式激活Outlook
问题描述:
我有一个应用程序需要在用户单击按钮时激活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窗口?
答
我想出了一个解决方案;我只是使用Process.Start()
而不是使用任何WINAPI调用。我之前也试过这个,但它导致现有的Outlook窗口调整大小,这很烦人。秘诀是将/recycle
参数传递给Outlook,这指示它重新使用现有窗口。调用看起来是这样的:
Process.Start("OUTLOOK.exe", "/recycle");
答
为什么不尝试将Outlook产生为新进程?我相信这是一个单一入口的应用程序(我在这里忘记了我的正确术语),所以如果它已经打开,它就会把它推到最前面。
+0
我试过了,它确实唤醒了当前正在运行的Outlook实例,但是窗口被调整大小,所以很烦人。 – Praetorian 2011-06-15 13:11:04
答
此作品(您可能需要更改路径):
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();
}
不幸的是,不能正常工作。我试过'SetWindowPos(hWnd,IntPtr.Zero,0,0,500,500,SWP_NOMOVE | SWP_NOSIZE)'。即使Outlook最小化,“SetForegroundWindow”和“SetWindowPos”的返回值都表示成功,并且“IsIconic”总是返回false。我开始认为这是Outlook 2010的一些怪癖。 – Praetorian 2011-06-08 15:29:40
哎哟......奇怪的行为。作为一种潜在的解决方法,可以尝试启动用户的默认电子邮件应用程序,而不是直接关注Outlook。 (假设Outlook是他们的默认...) – 2011-06-10 13:27:12
我该怎么做? – Praetorian 2011-06-10 14:32:28