Outlook窗口落后 - C#Windows应用程序

时间:2012-02-14 20:10:07

标签: c# outlook

我在Windows应用程序中从代码中打开一封新的Outlook电子邮件。有没有办法把焦点放在新窗口上(而不是在后面闪烁橙色)?

谢谢!

1 个答案:

答案 0 :(得分:2)

尝试使用PInvoke:

public class MoveToForeground
{
    [DllImportAttribute("User32.dll")]
    private static extern int FindWindow(string ClassName, string WindowName);

    [DllImport("user32.dll", EntryPoint="SetWindowPos")]
    public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);

    const int SWP_NOMOVE = 0x0002;
    const int SWP_NOSIZE = 0x0001;            
    const int SWP_SHOWWINDOW = 0x0040;
    const int SWP_NOACTIVATE = 0x0010;

    public static void DoOnProcess(string processName)
    {
        var process = Process.GetProcessesByName(processName);
        if (process.Length > 0)
        {
            int hWnd = FindWindow(null, process[0].MainWindowTitle.ToString());
            SetWindowPos(new IntPtr(hWnd), 0, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW | SWP_NOACTIVATE);
        }
    }
}


MoveToForeground.DoOnProcess("OUTLOOK.EXE");