c#调用winapi为外部程序控件赋值

来源:互联网 发布:mhx配装器 java怎么用 编辑:程序博客网 时间:2024/06/15 03:19
  public partial class Form1 : Form    {        [DllImport("user32.dll")]        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);        //[DllImport("User32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Unicode)]        //private static extern int SendMessage(IntPtr hwnd, uint wMsg, int wParam, int lParam);        [DllImport("user32.dll", EntryPoint = "SendMessageA")]        private static extern int SendMessage(IntPtr hwnd, uint wMsg, IntPtr wParam, string lParam);        [DllImport("User32.dll ")]        public static extern IntPtr FindWindowEx(IntPtr parent, IntPtr childe, string strclass, string FrmText);        [DllImport("user32.dll", EntryPoint = "GetDesktopWindow", CharSet = CharSet.Auto, SetLastError = true)]        static extern IntPtr GetDesktopWindow();        [DllImport("user32.dll", SetLastError = true)]        static extern IntPtr GetWindow(IntPtr hWnd, GetWindowCmd uCmd);        [DllImport("user32.dll", CharSet = CharSet.Auto)]        static extern int GetWindowText(IntPtr hWnd, StringBuilder lpText, int nCount);        [DllImport("user32.dll")]        public static extern int EnumChildWindows(IntPtr hWndParent, CallBack lpfn, int lParam);        public delegate bool CallBack(IntPtr hwnd, int lParam);        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]        static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);        public Form1()        {            InitializeComponent();        }        private void btnFind_Click(object sender, EventArgs e)        {            //1、获取桌面窗口的句柄            IntPtr desktopPtr = GetDesktopWindow();            //2、获得一个子窗口(这通常是一个顶层窗口,当前活动的窗口)            IntPtr winPtr = GetWindow(desktopPtr, GetWindowCmd.GW_CHILD);            //3、循环取得桌面下的所有子窗口            while (winPtr != IntPtr.Zero)            {                StringBuilder sbs = new StringBuilder(200);                GetWindowText(winPtr, sbs, sbs.Capacity);                if (!String.IsNullOrEmpty(sbs.ToString()))                {                    if (sbs.ToString().IndexOf("自动输入") >= 0)                    {                        StringBuilder sbClassName = new StringBuilder(200);                        GetClassName(winPtr, sbClassName, sbClassName.Capacity);                        IntPtr ptrChild = IntPtr.Zero;                        int i = EnumChildWindows(winPtr, (h, l) =>                        {                            StringBuilder sb = new StringBuilder(200);                            GetWindowText(h, sb, sb.Capacity);                            if (sb.ToString().Contains("自动输入"))                            {                                ptrChild = h;                                return false;                            }                            else                            {                                return true;                            }                        }, 0);                        if (ptrChild != IntPtr.Zero)                        {                            List<IntPtr> listEdit = new List<IntPtr>();                            List<string> listLabel = new List<string>();                            uint WM_SETTEXT = 0x000C;                            IntPtr result = IntPtr.Zero;                            do                            {                                result = FindWindowEx(ptrChild, result, null, null);                                if (result != IntPtr.Zero)                                {                                    StringBuilder sb = new StringBuilder(200);                                    GetWindowText(result, sb, sb.Capacity);                                    GetClassName(result, sbClassName, sbClassName.Capacity);                                    if (sbClassName.ToString().Contains("EDIT"))                                    {                                        listEdit.Add(result);                                    }                                    if (sbClassName.ToString().Contains("STATIC"))                                    {                                        listLabel.Add(sb.ToString());                                    }                                }                            } while (result != IntPtr.Zero);                            for (int j = 0; j < listLabel.Count; j++)                            {                                string str = listLabel[j];                                IntPtr p = listEdit[j];                                SendMessage(p, WM_SETTEXT, IntPtr.Zero, "");                                string tmp = "";                                if (str.Contains("单位名称"))                                {                                    tmp = textBox1.Text;                                }                                else if (str.Contains("识别码"))                                {                                    tmp = textBox2.Text;                                }                                else if (str.Contains("联系电话"))                                {                                    tmp = textBox3.Text;                                }                                else if (str.Contains("账户"))                                {                                    tmp = textBox4.Text;                                }                                SendMessage(p, WM_SETTEXT, IntPtr.Zero, tmp);                            }                        }                    }                }                //4、继续获取下一个子窗口                winPtr = GetWindow(winPtr, GetWindowCmd.GW_HWNDNEXT);            }        }           }    /// <summary>    /// 窗口与要获得句柄的窗口之间的关系。    /// </summary>    enum GetWindowCmd : uint    {         GW_HWNDFIRST = 0,         GW_HWNDLAST = 1,         GW_HWNDNEXT = 2,         GW_HWNDPREV = 3,         GW_OWNER = 4,         GW_CHILD = 5,         GW_ENABLEDPOPUP = 6    }
原创粉丝点击