第一次接触win32api,刚开始的时候有点迷迷糊糊的。
Windows API 就是windows应用程序接口。 win api向上就是windows应用程序,向下就是windows操作系统核心。
刚开始的时候都是自己一步一步摸索,但是网上的东西都是一些
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
诸如此类的,不知道怎么调用,这对于一个新手来说还是比较郁闷的。
今天花了2个小时总结了一些常用的操作窗口的win32 api的知识,当然不仅是只有函数的标明,包括怎么调用他,希望新手有帮助,我也是新手。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WinformHandle { public partial class 句柄的获取 : Form { public 句柄的获取() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string str = string.Empty; IntPtr i = FindWindow("MozillaWindowClass", null); IntPtr i3 = FindWindow(null, "Teamviewer"); textBox1.AppendText("根据Teamviewer的title获取句柄:" + i3.ToString("x2") + " "); IntPtr i1 = FindWindow(null, "TeamViewer"); textBox1.AppendText("火狐浏览器的句柄:" + i.ToString("x2") + " "); textBox1.AppendText("Teamviewer的句柄:" + i1.ToString("x2") + " "); IntPtr i2 = FindWindowEx(i1, IntPtr.Zero, "Button", "远程控制"); StringBuilder sb = new StringBuilder(); GetWindowText(i1, sb, sb.Capacity); textBox1.AppendText("TeamViewer的文本:" + sb.ToString() + " "); textBox1.AppendText("Teamviewer下的子窗体的句柄:" + i2.ToString("x2") + " "); List<WindowInfo> listInfo = GetAllDesktopWindows("MozillaWindowClass"); for (int s = 0; s < listInfo.Count; s++) { textBox1.AppendText("List<WindowInfo>[" + s + "]" + "句柄:" + listInfo[s].hWnd.ToString("x2") + "类名:" + listInfo[s].szClassName + "文本:" + listInfo[s].szWindowName + " "); } WindowInfo vsInfo = GetIntPtrByWindowClName("HwndWrapper[DefaultDomain;;ec23c78e-745d-424c-a8d8-d108ad24c70f]"); textBox1.AppendText("根据vs2013的类名获取qq窗体的信息:" + "句柄:" + vsInfo.hWnd.ToString("x2") + "文本:" + vsInfo.szWindowName + "类名:" + vsInfo.szClassName + " "); WindowInfo mailInfo= GetIntPtrByWindowTitle("Foxmail"); textBox1.AppendText("根据Foxmail的标题获取句柄信息:"+"句柄:"+mailInfo.hWnd.ToString("x2")+"文本:"+mailInfo.szWindowName+"类名:"+mailInfo.szClassName+" "); //这一步将会打开i1的窗口 SetForegroundWindow(i1); ShowWindow(i1, 1); } public struct WindowInfo { public IntPtr hWnd; public string szWindowName; public string szClassName; } /// <summary> /// 根据类名获取窗口信息 /// </summary> /// <param name="className"></param> /// <param name="windowText"></param> /// <returns></returns> public static List<WindowInfo> GetAllDesktopWindows(string className = "", string windowText = "") { List<WindowInfo> wndList = new List<WindowInfo>(); EnumWindows(delegate(IntPtr hWnd, int lParam) { WindowInfo wnd = new WindowInfo(); StringBuilder sb = new StringBuilder(256); wnd.hWnd = hWnd; GetWindowText(hWnd, sb, sb.Capacity); wnd.szWindowName = sb.ToString(); GetClassName(hWnd, sb, sb.Capacity); wnd.szClassName = sb.ToString(); if ((className == "" || wnd.szClassName == className) && (windowText == "" || wnd.szWindowName == windowText)) { wndList.Add(wnd); } return true; }, 0); return wndList; } /// <summary> /// 显示窗体 /// </summary> /// <param name="hWnd">窗体句柄</param> /// <param name="nCmdShow">指定的命令 0:关闭窗口 1:正常大小显示窗口 2:最小化窗口3:最大化窗口</param> /// <returns></returns> [DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); public enum WindowShowStatus { /// <summary> /// 隐藏窗口 /// </summary> SW_HIDE = 0, /// <summary> /// 最大化窗口 /// </summary> SW_MAXIMIZE = 3, /// <summary> /// 最小化窗口 /// </summary> SW_MINIMIZE = 6, /// <summary> /// 用原来的大小和位置显示一个窗口,同时令其进入活动状态 /// </summary> SW_RESTORE = 9, /// <summary> /// 用当前的大小和位置显示一个窗口,同时令其进入活动状态 /// </summary> SW_SHOW = 5, /// <summary> /// 最大化窗口,并将其激活 /// </summary> SW_SHOWMAXIMIZED = 3, /// <summary> /// 最小化窗口,并将其激活 /// </summary> SW_SHOWMINIMIZED = 2, /// <summary> /// 最小化一个窗口,同时不改变活动窗口 /// </summary> SW_SHOWMINNOACTIVE = 7, /// <summary> /// 用当前的大小和位置显示一个窗口,不改变活动窗口 /// </summary> SW_SHOWNA = 8, /// <summary> /// 用最近的大小和位置显示一个窗口,同时不改变活动窗口 /// </summary> SW_SHOWNOACTIVATE = 4, /// <summary> /// 用原来的大小和位置显示一个窗口,同时令其进入活动状态,与SW_RESTORE 相同 /// </summary> SW_SHOWNORMAL = 1, /// <summary> /// 关闭窗体 /// </summary> WM_CLOSE = 0x10 } /// <summary> /// 设置系统的前台窗体 /// </summary> /// <param name="hWnd">窗体句柄</param> /// <returns></returns> [DllImport("user32.dll")] public static extern bool SetForegroundWindow(IntPtr hWnd); [DllImport("user32.dll")] public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpWindowText, int nMaxCount); [DllImport("user32.dll")] public static extern void GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount); [DllImport("user32.dll")] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); /// <summary> /// 获取子窗口句柄 /// </summary> /// <param name="hwndParent">主窗体句柄</param> /// <param name="hwndChildAfter">子窗体句柄</param> /// <param name="lpszClass">搜索的类名</param> /// <param name="lpszWindow"></param> /// <returns></returns> [DllImport("user32.dll")]//,EntryPoint="FindWindowEx" public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, String lpszClass, String lpszWindow); public delegate bool CallBack(IntPtr hwnd, int lParam); [DllImport("user32.dll")] public static extern int EnumWindows(CallBack x, int y); /// <summary> /// 功能:更具窗体类名获取窗体的信息 /// </summary> /// <param name="strWinClassName"></param> /// <returns></returns> public static WindowInfo GetIntPtrByWindowClName(string strWinClassName) { WindowInfo windowInfo = new WindowInfo(); EnumWindows(delegate(IntPtr hWnd, int lParam) { WindowInfo wnd = new WindowInfo(); StringBuilder sb = new StringBuilder(256); wnd.hWnd = hWnd; GetWindowText(hWnd, sb, sb.Capacity); wnd.szWindowName = sb.ToString(); GetClassName(hWnd, sb, sb.Capacity); wnd.szClassName = sb.ToString(); if (wnd.szClassName == strWinClassName) { windowInfo = wnd; } return true; }, 0); return windowInfo; } /// <summary> /// 功能:更具窗体标题获取窗体的信息 /// </summary> /// <param name="strWinClassName"></param> /// <returns></returns> public static WindowInfo GetIntPtrByWindowTitle(string strWinHeadName) { WindowInfo windowInfo = new WindowInfo(); EnumWindows(delegate(IntPtr hWnd, int lParam) { WindowInfo wnd = new WindowInfo(); StringBuilder sb = new StringBuilder(256); wnd.hWnd = hWnd; GetWindowText(hWnd, sb, sb.Capacity); wnd.szWindowName = sb.ToString(); GetClassName(hWnd, sb, sb.Capacity); wnd.szClassName = sb.ToString(); if (wnd.szWindowName == strWinHeadName) { windowInfo = wnd; } return true; }, 0); return windowInfo; } /// <summary> /// 指定句柄的窗口发送消息 /// </summary> /// <param name="hWnd">接收消息窗体的句柄</param> /// <param name="Msg">消息标示符</param> /// <param name="wParam">消息</param> /// <param name="lParam"></param> /// <returns></returns> [DllImport("user32.dll")] public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, string lParam); } }