2014-10-07 74 views
0

我通常会运行下面列出的方法来最大化图标化窗口;然而,当涉及到Outlook时,有时会最大化一个邮件(消息),我已打开,而不是父应用程序(Outlook);它只是拉起了它发现的任何前景,我需要父母,我怎么能做到这一点?检索父窗口处理C#(试图最大化Outlook)

我试过使用WINAPI GetAncestor,我也试过GetParent。

public static bool EventChecking(string progr) 
    { 
     int bb = 0; 
     if (Process.GetProcessesByName(progr).Length > 0) 
     { 
      bb++; 
     } 

     if (bb == 0) 
     { 
      return false; 
     } 

     foreach (Process ddcd in Process.GetProcesses()) 
     { 
      if (ddcd.ProcessName.Contains(progr)) 
      { 
       if (ddcd.MainWindowHandle != IntPtr.Zero) 
       { 
        pointer = ddcd.MainWindowHandle; 
        if (IsIconic(pointer)) 
        { 
         SendMessage(pointer, 0x112, 0xF120, 0); 
        } 

        SetForegroundWindow(pointer); 
       } 
      }; 

     } 
     return true; 
    } 

编辑:

最近我也试过:

if (ddcd.MainWindowTitle.EndsWith("- Outlook")) 

,它仍然拉起单个电子邮件

+2

或许,这可能会帮助 - http://stackoverflow.com/questions/637652/get-the-handle-of-a-window-with-not-fully-known-title-c – 2014-10-07 23:45:36

+0

它仍然是拉动打开单个电子邮件;我现在搜索结束与“ - 展望”只有主窗口结束与此字符串不是单个电子邮件; – ModS 2014-10-08 00:31:40

回答

1

我也遇到了麻烦,通过C#与Outlook工作,但我有Win32调用取得了一些成功。下面是通过检查标题来查找Outlook主窗口的一种方法。

您也可以尝试EnumWindows,但由于回调需要付出更多努力才能实现。

using System.Text; 
using System.Runtime.InteropServices; 

public IntPtr GetOutlookHandle() 
{ 
    string windowClass = "rctrl_renwnd32"; 
    uint GW_HWNDNEXT = 2; 
    IntPtr firstHandle = new IntPtr(0); 
    IntPtr handle = new IntPtr(0); 

    // Look for a Window with the right Class 
    firstHandle = FindWindow(windowClass, null); 

    // Nothing Found 
    if (firstHandle.Equals(IntPtr.Zero)) return IntPtr.Zero; 

    // Remember where we started to avoid an infinite loop 
    handle = firstHandle; 

    do 
    { 
     // Check the Caption to find the Main Window 
     if (GetWindowCaption(handle).EndsWith(" - Microsoft Outlook")) 
     { 
      return handle; 
     } 

     // Get the next Window with the same Class 
     handle = GetWindow(handle, GW_HWNDNEXT); 

    } while (handle != firstHandle); 

    // Didn't find any matches 
    return IntPtr.Zero; 
} 


private static string GetWindowCaption(IntPtr windowHandle) 
{ 
    // Determine Length of Caption 
    int length = GetWindowTextLength(windowHandle); 
    StringBuilder sb = new StringBuilder(length + 1); 

    // Get Window Caption 
    GetWindowText(windowHandle, sb, sb.Capacity); 
    return sb.ToString(); 
} 

[DllImport("user32.dll", SetLastError = true)] 
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName); 

[DllImport("user32.dll", SetLastError = true)] 
private static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd); 

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount); 

[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] 
private static extern int GetWindowTextLength(IntPtr hWnd); 
+0

谢谢!对此,我真的非常感激 ! :d – ModS 2014-10-12 05:14:00