using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; using System.Windows.Forms; namespace TencentMsg { public class Win32API { [DllImport("user32.dll", EntryPoint = "FindWindow")] private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); [DllImport("user32.dll", EntryPoint = "SendMessageA")] public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam); [DllImport("user32.dll", EntryPoint = "SendMessageA")] public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam); ///<summary> /// 设置指定窗口为当前活动窗口 ///</summary> ///<param name="hWnd">窗口句柄</param> [DllImport("User32.DLL")] private static extern bool SetForegroundWindow(IntPtr hWnd); ///<summary> /// 向指定窗口发送字符串 ///</summary> [DllImport("User32.dll", EntryPoint = "SendMessage")] private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam); [DllImport("shell32.dll")] public static extern int ShellExecute(IntPtr hwnd, StringBuilder lpszOp, StringBuilder lpszFile, StringBuilder lpszParams, StringBuilder lpszDir, int FsShowCmd); [DllImport("user32.dll")] private static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, int lParam); [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, string lparam); [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]//查找窗口 private static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll")] public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount); [DllImport("user32.dll")] private static extern int GetWindowTextW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount); [DllImport("user32.dll")] private static extern int GetClassNameW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount); private delegate bool WNDENUMPROC(IntPtr hWnd, int lParam); static IntPtr Game; const int BM_CLICK = 0xF5; public static bool FindWind(string sTitle){ IntPtr hwnd = FindWindow(null, sTitle); return (hwnd != IntPtr.Zero) ; } public class WindowInfo { public IntPtr hWnd; public string szClassName; public string szWindowName; } //寻找系统的全部窗口 public static WindowInfo[] GetAllDesktopWindows() { List<WindowInfo> wndList = new List<WindowInfo>(); EnumWindows(delegate(IntPtr hWnd, int lParam) { WindowInfo wnd = new WindowInfo(); StringBuilder sb = new StringBuilder(256); //get hwnd wnd.hWnd = hWnd; //get window name GetWindowTextW(hWnd, sb, sb.Capacity); wnd.szWindowName = sb.ToString(); //get window class GetClassNameW(hWnd, sb, sb.Capacity); wnd.szClassName = sb.ToString(); //Console.WriteLine("Window handle=" + wnd.hWnd.ToString().PadRight(20) + " szClassName=" + wnd.szClassName.PadRight(20) + " szWindowName=" + wnd.szWindowName); //add it into list if (wnd.szClassName == "TXGuiFoundation" && wnd.szWindowName=="查找") { wndList.Add(wnd); } return true; }, 0); return wndList.ToArray(); } public static bool FindSearchWind(){ IntPtr maindHwnd = FindWindow("TXGuiFoundation", ""); if(maindHwnd != IntPtr.Zero){ IntPtr childHwnd = FindWindowEx(maindHwnd, IntPtr.Zero, null, "登录"); if (childHwnd != IntPtr.Zero) { SendMessage(childHwnd, BM_CLICK, 0, 0); } else { MessageBox.Show("没有找到子窗口"); } } return (maindHwnd != IntPtr.Zero) ; } } }
more: https://www.cnblogs.com/tary2017/articles/8031782.html