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();
        }
    }

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值