2012-08-01 104 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類)