找到外部窗口标题

时间:2014-06-05 09:29:40

标签: c# findwindow

我收到了这段代码:

using System.Runtime.InteropServices;

[DllImportAttribute("User32.dll")]
private static extern int FindWindow(String ClassName, String WindowName);
[DllImport("User32")]
private static extern int ShowWindow(int hWnd, int nCmdShow);
private const int SW_HIDE = 0;


int hWnd = FindWindow(null, Microsoft Excel - Book1);
if (hWnd > 0)
{
    ShowWindow(hWnd, SW_HIDE);
}

但有时候我会用OpenOffice.org操作Book1 ..我的问题是,我怎么能SW_HIDE不同的Windows标题?

如果存在Microsoft Excel - Book1标题

如果Book1 - OpenOffice.org Calc标题存在

也许有可能找到Windows标题部分" Book1"

非常感谢!

1 个答案:

答案 0 :(得分:0)

使用以下代码获取所有已打开窗口的列表。

[DllImport("user32.dll")]
        static extern bool EnumWindows(EnumDelegate lpfn, IntPtr lParam);

然后编写委托函数如下:

public string[] win_List = new string[50];
int i = 0;
public bool lpfn(IntPtr hWnd, int lParam)
{
    StringBuilder stbrTitle = new StringBuilder(255);
    int titleLength = GetWindowText(hWnd, stbrTitle, stbrTitle.Capacity + 1);
    string strTitle = stbrTitle.ToString();
    if (IsWindowVisible(hWnd) && string.IsNullOrEmpty(strTitle) == false)
    {
        win_List[i++] = strTitle;                      
    }
    return true;
}


public string[] GetWinList()
{
    EnumDelegate del_fun = new EnumDelegate(lpfn);
    EnumWindows(del_fun, IntPtr.Zero);
    return win_List;
}