检索父窗口处理C#(尝试最大化outlook)

时间:2014-10-07 23:39:30

标签: c#

我通常会运行下面列出的方法来最大化图标化窗口; 然而,当涉及到outlook时,有时它会最大化我打开的Mail(消息)而不是父应用程序(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"))

它仍然会提取单个电子邮件

1 个答案:

答案 0 :(得分: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);