How to start Outlook minimized?

被刻印的时光 ゝ 提交于 2019-12-11 08:12:04

问题


I think starting a process minimized should be simple but I had no luck with outlook. How can I start Outlook minimized?

My attempt was this:

[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);

static void Main(string[] args)
{
    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.FileName = "OUTLOOK.EXE";

    IntPtr hWnd = Process.Start(startInfo).Handle;

    bool state = false;
    if (!hWnd.Equals(IntPtr.Zero))
        state = ShowWindowAsync(hWnd, 2);

    // window values: http://msdn.microsoft.com/en-us/library/windows/desktop/ms633548(v=vs.85).aspx

    Console.WriteLine(state.ToString());
    Console.Read();
}

回答1:


Have you tried using ProcessStartInfo.WindowStyle, setting it to ProcessWindowStyle.Minimized?




回答2:


I have found out that if you wait until Outlook have started and you send the command below the window will minimize to tray. Now the only thing to accomplish in order to minimize outlook is to loop till it is ready :-)

var hWnd = Process.Start(startInfo);
ShowWindowAsync(hWnd.MainWindowHandle, 2);



回答3:


I have solved it but I like to hear your comments if you think the solution can be improved. I also have posted the solution on my blog with some more details at http://jwillmer.de/blog/2012/08/01/how-to-start-outlook-minimized-with-c/

[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);

// console application entry point
static void Main()
{
    // check if process already runs, otherwise start it
    if(Process.GetProcessesByName("OUTLOOK").Count().Equals(0))
        Process.Start("OUTLOOK");

    // get running process
    var process = Process.GetProcessesByName("OUTLOOK").First();

    // as long as the process is active
    while (!process.HasExited)
    {
        // title equals string.Empty as long as outlook is minimized
        // title starts with "öffnen" (engl: opening) as long as the programm is loading
        string title = Process.GetProcessById(process.Id).MainWindowTitle;

        // "posteingang" is german for inbox
        if (title.ToLower().StartsWith("posteingang"))
        {
            // minimize outlook and end the loop
            ShowWindowAsync(Process.GetProcessById(process.Id).MainWindowHandle, 2);
            break;
        }

        //wait awhile
        Thread.Sleep(100);

        // place for another exit condition for example: loop running > 1min
    }
}



回答4:


You can use this this.Application.ActiveExplorer ().WindowState = Outlook.OlWindowState.olMinimized;

It minimizing your corrent outlook window (this = ThisAddIn class)



来源:https://stackoverflow.com/questions/11763528/how-to-start-outlook-minimized

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!