zoukankan      html  css  js  c++  java
  • Windows API 查找窗体,发送Windows消息

    最近项目中需要做Windows消息截获操作,在网上找了一些资料。

        public class WindowsAPI
        {
            /// <summary> 
            /// 回调函数代理 
            /// </summary> 
            public delegate bool CallBack(int hwnd, int lParam);
            public const int WM_GETTEXT = 0x000D;
            public const int WM_SETTEXT = 0x000C;
            /// <summary>
            /// 鼠标click事件
            /// </summary>
            public const int WM_CLICK = 0x00F5;
    
            public const int WM_KEYUP = 0x0101;
    
            #region
            [DllImport("User32.dll", EntryPoint = "FindWindow")]
            public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    
            [DllImport("user32.dll", EntryPoint = "FindWindowEx", SetLastError = true)]
            public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
    
            [DllImport("User32.dll", EntryPoint = "SendMessage")]
            public static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);
    
    
            [DllImport("user32.dll")]
            public static extern int GetLastActivePopup(int hWnd);
            [DllImport("user32.dll")]
            public static extern int AnyPopup();
            [DllImport("user32.dll")]
            public static extern int GetWindowText(int hWnd, StringBuilder lpString, int nMaxCount);
            [DllImport("user32.dll")]
            public static extern int EnumThreadWindows(int dwThreadId, CallBack lpfn, int lParam);
            [DllImport("user32.dll")]
            public static extern int EnumWindows(CallBack lpfn, int lParam);
            [DllImport("user32.dll")]
            public static extern int EnumChildWindows(int hWndParent, CallBack lpfn, int lParam);
    
            /// <summary>
            /// 获取活动窗体句柄
            /// </summary>
            /// <returns></returns>
            [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
            public static extern IntPtr GetForegroundWindow();
    
            [DllImport("user32.dll", EntryPoint = "SendMessage")]
            public static extern int SendMessage(IntPtr hwnd, int wMsg, uint wParam, uint lParam);
    
    
            [DllImport("user32.dll", CharSet = CharSet.Unicode)]
            public static extern IntPtr PostMessage(IntPtr hwnd, int wMsg, uint wParam, uint lParam);
    
            #endregion
    
            /// <summary>
            /// 获取枚举描述
            /// </summary>
            /// <param name="enumValue">枚举值</param>
            /// <returns>描述信息</returns>
            public static string GetEnumDescription(Enum enumValue)
            {
                string str = enumValue.ToString();
                System.Reflection.FieldInfo field = enumValue.GetType().GetField(str);
                object[] objs = field.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false);
                if (objs == null || objs.Length == 0) return str;
                System.ComponentModel.DescriptionAttribute da = (System.ComponentModel.DescriptionAttribute)objs[0];
                return da.Description;
    
            }
        }

    至于发送的消息可以通过“Spy++”工具抓取,然后模拟发送。

                var pHand = WindowsAPI.FindWindow(null, windowsName);//查找进程窗体
    //查找子窗体
    var tabControl = WindowsAPI.FindWindowEx(pHand, (IntPtr)0, "SysTabControl32", "");        //多页选择框,其中第三个参数是第几页,如0,1,2,3,4...
    //4912会点击按钮,并切换到对应页。4876只会点按钮,不会切换页
    var r = WindowsAPI.SendMessage(tabControl, 4912, 1, 0);//4912//4876 //发送点击事件 int clickPrintDoneOk = WindowsAPI.SendMessage(childHandPrintDoneOk, WindowsAPI.WM_CLICK, 0, 0);
  • 相关阅读:
    [SQL] 外卖系统数据库设计
    [PHP] 遗传算法求函数最大值一般实现
    PHP CURL根据详细地址获取腾讯地图经纬度
    PHP 递归读取无限级分类
    PHP 仿网易云的评论盖楼
    JavaScript清除空格、换行,把双引号转换成单引号
    百度地图小Demo---获取当前地址以及拖拽显示地址
    基于Modbus的C#串口调试开发
    二叉树的中序遍历
    二叉树的前序遍历
  • 原文地址:https://www.cnblogs.com/LittleJin/p/5799252.html
Copyright © 2011-2022 走看看