我有一个应用程序需要在用户单击按钮时激活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窗口?
不幸的是,不能正常工作。我试过'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