问题
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