2012-08-01 55 views
1

我认为开始一个最小化的过程应该很简单,但我没有前景的运气。如何启动Outlook最小化?如何启动Outlook最小化?

我的尝试是这样的:

[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(); 
} 

回答

0

我已经解决了,但我想听听您的意见,如果你认为该解决方案可以得到改善。 我也贴在我的博客上有一些更详细的解决方案,在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 
    } 
} 
2

您是否尝试过使用ProcessStartInfo.WindowStyle,将其设置为ProcessWindowStyle.Minimized

+0

是,没有运气或者:-( – jwillmer 2012-08-01 17:24:30

+0

@jwillmer:而不是只说“没有运气”可以形容,当你试图 – 2012-08-01 17:31:37

+0

发生了什么?对不起,当然我会:在我的工作电脑我找不到任何区别,但现在在我的家用电脑,似乎加载屏幕是最小化,如果我将值设置为ProcessWindowStyle.Minimized但主程序仍将展开。 – jwillmer 2012-08-01 18:23:34

0

我发现如果您等到Outlook启动并且您发送窗口下面的命令将最小化为托盘。现在唯一的事情,以尽量减少展望做到的是循环,直到它准备好:-)

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

您可以使用此 this.Application.ActiveExplorer().WindowState = Outlook.OlWindowState.olMinimized;

它最大限度地减少您的corrent Outlook窗口 (此= ThisAddIn类)