通过窗口句柄获取控件信息

在日常生产中,经常会遇导和其他软件进行对接的情况。有接口的还好处理,有些软件无法提供接口的有时候只能通过句柄将软件界面上的数据取下来,进行处理。这里就记录一下关于通过句柄取值的方式

首选需要准备一些Windows的API类,如下

public class WindowAPI
    {
        #region User32

        [DllImport("user32.dll")]
        public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
        [DllImport("user32.dll")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);
        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        /// 
        /// 根据父窗体查找子窗体
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);

        /// 
        /// 获取句柄
        /// 
        /// 指定窗口句柄
        /// 0 同级第一个;1同级最后一个,2同级下一个;3同级下一个;4属主窗口;5子窗口
        /// 
        [DllImport("User32.dll")]
        public extern static IntPtr GetWindow(IntPtr hWnd, int wCmd);


        [DllImport("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hWnd, int Msg, int wParam, IntPtr lParam);

        // SendMessage
        // 向窗体发送消息
        // @para1: 窗体句柄
        // @para2: 消息类型
        // @para3: 附加的消息信息
        // @para4: 附加的消息信息
        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        public static extern int SendMessage(
            IntPtr hWnd,
            int Msg,
            IntPtr wParam,
            string lParam);


        [DllImport("user32.dll")]
        public static extern bool SetCursorPos(int X, int Y);
        [DllImport("user32.dll")]
        public static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
        [DllImport("user32.dll")]
        public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);
        [DllImport("user32.dll")]
        public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndlnsertAfter, int X, int Y, int cx, int cy, uint Flags);
        //ShowWindow参数
        public const int SW_SHOWNORMAL = 1;
        public const int SW_RESTORE = 9;
        public const int SW_SHOWNOACTIVATE = 4;
        //SendMessage参数
        public const int WM_KEYDOWN = 0X100;
        public const int WM_KEYUP = 0X101;
        public const int WM_SYSCHAR = 0X106;
        public const int WM_SYSKEYUP = 0X105;
        public const int WM_SYSKEYDOWN = 0X104;//ENTER
        public const int WM_CHAR = 0X102;
        public const int WM_GETTEXT = 0x000D;  // 获得窗体文本 如获得对话框标题
        public const int WM_SETTEXT = 0x000C;  // 设置窗体文本 如设置文本框内容
        public const int WM_CLICK = 0x00F5;  // 发送点击消息如调用该窗体(按钮)的"button1_Click();"
        #endregion

    }

注意

在使用句柄获取数据之前,需要先确认一下被获取的界面是否支持通过句柄进行数据的获取。可以通过SPY++这个软件先去抓取一下,如果可以通过SPY++获取到所需控件,那么就可以通过句柄进行操作,否则将无法操作句柄

接下来就是句柄的获取,直接上代码了

		/// 
        /// 获取自定义型号
        /// 
        /// 
        /// 
        private voidGetZdyxh(out string msg)
        {
            try
            {
                IntPtr _hWnd_ = WindowAPI.FindWindow(null, "AE ProductInfo");//AE ProductInfo是窗体名称
                IntPtr _prohWnd_;
                if (_hWnd_ == IntPtr.Zero)
                {
                    _prohWnd_ = new IntPtr();
                    msg = "未找到工具";
                    return false;
                }
                _prohWnd_ = WindowAPI.FindWindowEx(_hWnd_, IntPtr.Zero, "Static", "自定义型号");//根据类型和名称获取句柄
                _prohWnd_ = WindowAPI.GetWindow(_prohWnd_, 2);//寻找同级下一个

                IntPtr buffer = Marshal.AllocHGlobal(10000);
                WindowAPI.SendMessage(_prohWnd_, 13, 1000, buffer);//将数据复制到buffer中
                msg = Marshal.PtrToStringAnsi(buffer);//这就是控件的值内容
            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }
        }

以上!

你可能感兴趣的:(C#,windows,开发语言)