查找一下相关的Windows API,可以通过标题查找窗口。
windows api
/// <summary>
/// 获取系统窗体句柄
/// </summary>
/// <param name="lpClassName"></param>
/// <param name="lpWindowName"></param>
/// <returns></returns>
[DllImport("User32.dll", EntryPoint = "FindWindow")]
public static extern IntPtr FindWindow(string lpClassName, string
lpWindowName);
通过该方法可以获取当前打开窗口的句柄.通过句柄创建你所要的窗体类型对象..判断标题是不是“文件”。
注意:这个api用的时候的class名称你需要用句柄查看器来看..不是 class demo的名称就是“demo” 可能是“1233-demo ”
同上,window api可以做到
public delegate bool EnumWindowsProc(IntPtr p_Handle, int p_Param);
[DllImport("user32.dll")]
public static extern int EnumWindows(EnumWindowsProc ewp, int lParam);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowText(IntPtr hWnd, out STRINGBUFFER text, int nMaxCount);
[DllImport("user32.dll")]
public static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern IntPtr GetWindowThreadProcessId(IntPtr hwnd, ref int lpdwProcessId);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct STRINGBUFFER
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 512)]
public string szText;
}
private void button1_Click(object sender, EventArgs e)
{
MessageWindwos();
}
public void MessageWindwos()
{
EnumWindowsProc _EunmWindows = new EnumWindowsProc(NetEnumWindows);
EnumWindows(_EunmWindows, 0);
}
private bool NetEnumWindows(IntPtr p_Handle, int p_Param)
{
if (!IsWindowVisible(p_Handle))return true;
STRINGBUFFER _TitleString = new STRINGBUFFER();
GetWindowText(p_Handle, out _TitleString, 256);
MessageBox.Show(_TitleString.szText); ///获取的窗体
return true;
}
获取说有窗体 ...获取控件和这个差不过 使用
public delegate bool EnumWindowsProc(IntPtr p_Handle, int p_Param);
[DllImport("user32.dll")]
public static extern int EnumChildWindows(IntPtr hWndParent, EnumWindowsProc ewp, int lParam);
/// <summary>
/// 获取所有控件的信息
/// </summary>
/// <param name="p_Handle">窗体句饼</param>
/// <returns></returns>
public LoadControl(IntPtr p_Handle)
{
EnumWindowsProc _EunmControl = new EnumWindowsProc(NetEnumControl);
Win32API.EnumChildWindows(p_Handle, _EunmControl, 0);
return _ControlList;
}
private bool NetEnumControl(IntPtr p_Handle, int p_Param)
{
STRINGBUFFER _TextString = new STRINGBUFFER();
GetWindowText(p_Handle, out _TextString, 256);
MessageBox.Show(_TextString.szText);
return true;
}