C#使用user32.dll对目标窗口、子窗口进行读写操作类

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

public class User32RWWindowClass
    {
        private IntPtr HwndMain;

        private List<IntPtr> ChildHwnd;

        public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr parameter);

        public delegate bool CallBack(int hwnd, int lParam);
        private int source;
        private int target;
        [DllImport("USER32.DLL")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll")]
        private static extern bool IsWindowVisible(int hWnd);

        [DllImport("USER32.DLL")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);

        [DllImport("USER32.DLL")]
        private static extern int GetWindowText(int hwnd, StringBuilder lpString, int cch);

        [DllImport("USER32.DLL")]
        private static extern int GetWindowTextLength(int hWnd);

        [DllImport("USER32.DLL")]
        public static extern bool ShowWindow(int hWnd, int nCmdShow);

        [DllImport("USER32.DLL")]
        public static extern int EnumWindows(CallBack x, int y);

        [DllImport("User32.dll ")]
        public static extern IntPtr FindWindowEx(IntPtr parent, IntPtr childe, string strclass, string FrmText);
        [DllImport("user32.dll")]
        public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool EnumChildWindows(IntPtr hwndParent, EnumWindowsProc lpEnumFunc, IntPtr lParam);

        [DllImport("user32.dll")]
        public static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);
        [DllImport("user32.dll")]
        public static extern int SendMessage(int hWnd, int wMsg, int wParam, string lParam);

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

        public static bool EnumWindow(IntPtr handle, IntPtr pointer)
        {
            List<IntPtr> list = GCHandle.FromIntPtr(pointer).Target as List<IntPtr>;
            if (list == null)
            {
                return false;
            }
            list.Add(handle);
            return true;
        }

        public static List<IntPtr> GetChildWindows(IntPtr parent)
        {
            List<IntPtr> list = new List<IntPtr>();
            GCHandle value = GCHandle.Alloc(list);
            try
            {
                EnumWindowsProc lpEnumFunc = new EnumWindowsProc(EnumWindow);
                EnumChildWindows(parent, lpEnumFunc, GCHandle.ToIntPtr(value));
            }
            finally
            {
                if (value.IsAllocated)
                {
                    value.Free();
                }
            }
            return list;
        }
        public bool RE_CallBack(int hwnd, int lParam)
        {

            if (IsWindowVisible(hwnd))
            {
                int cTxtLen = GetWindowTextLength(hwnd) + 1;
                StringBuilder text = new StringBuilder(cTxtLen);
                GetWindowText(hwnd, text, cTxtLen);
                string cTitle = text.ToString();

                if (cTitle.Contains("源窗体Title"))
                {
                    source = hwnd;
                }
                if (cTitle.Contains("目标窗体Title"))
                {
                    target = hwnd;
                }

            }
            return true;
        }

        public void SendMessage(string st)
        {
            EnumWindows(RE_CallBack, 0);
            if (target != 0)
            {
                SetForegroundWindow(new IntPtr(target));
                SendKeys.SendWait("123456" + "{ENTER}");
                SetForegroundWindow(new IntPtr(source));
            }

        }
        publice string GetTagetValue()
        {
            StringBuilder stringBuilder = new StringBuilder(250);
            HwndMain = FindWindow("目标窗体Title", null);
            ChildHwnd = GetChildWindows(HwndMain);
            SendMessage(ChildHwnd[0], 13, 100, stringBuilder);
            if (string.IsNullOrEmpty(stringBuilder.ToString()))
            {
                return "";
            }
            return stringBuilder.ToString();
        }
    }

猜你喜欢

转载自blog.csdn.net/qq_33790894/article/details/131723451