1

我得到了这个代码:

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”

非常感谢!

4

1 回答 1

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;
}
于 2014-06-05T10:03:31.423 回答