C# 发送消息给其它窗口的子窗体和读取子窗体标题或者内容

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 发送文本到句柄框
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //根据窗口类型或者窗口标题获取窗口句柄
        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
        //根据父窗口句柄、子窗口句柄、窗口类型、窗口标题 获取句柄
        [DllImport("User32.dll", EntryPoint = "FindWindowEx")]
        private extern static IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

        /// 向指定句柄的窗口发送消息
        ///
        /// 窗口句柄
        /// 消息类型
        /// 消息1
        /// 消息2
        // 遍历窗口的所有子窗口,通过CallBack回调
        [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);

        // 判断窗口是否可见
        [DllImport("user32.dll")]
        public static extern bool IsWindowVisible(IntPtr hWnd);

        // 获取窗口文本长度
        [DllImport("user32.dll")]
        public static extern int GetWindowTextLength(IntPtr hWnd);

        // 获取窗口文本,文本会塞入StringBuilder中,需要指明字符串最大长度nMaxCount
        [DllImport("User32.dll", EntryPoint = "GetWindowText")]
        private static extern int GetWindowText(IntPtr hwnd, StringBuilder text, int nMaxCount);


        //函数返回桌面窗口的句柄
        [DllImport("user32.dll", EntryPoint = "GetDesktopWindow", CharSet = CharSet.Auto, SetLastError = true)]
        static extern IntPtr GetDesktopWindow();

        [DllImport("user32.dll", EntryPoint = "SendMessageA")]
        public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, StringBuilder lParam);
        [DllImport("user32.dll", EntryPoint = "SendMessage")]
        public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        // 给窗口发送消息,事件返回的数据通过Byte[] 数组获得
        [DllImport("user32.dll", EntryPoint = "SendMessage")]
        public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, Byte[] lParam);

        [DllImport("user32.dll", EntryPoint = "PostMessage")]
        public static extern int PostMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);

        //将指定句柄的窗口置顶
        [DllImport("User32.dll", EntryPoint = "SetForegroundWindow")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);

        public static int WM_COMMAND = 0x111; //为指定句柄的窗口发送点击事件
        const int WM_SETTEXT = 0x000C; //为指定句柄的窗口设置文本信息
        const int WM_CLICK = 0X00F5;//为指定句柄的窗口发送点击事件
        public const int WM_CHAR = 0x0102;
        const int WM_GETTEXT = 0x000D;//获取窗口标题内容
        const int WM_GETTEXTLENGTH = 0x000E;
        const int WM_CLOSE = 0x10;

       // 这段声明是另一种读取窗口文本的方式,发现只能读取顶层窗体的标题。

        [DllImport("user32.dll")]
        public static extern int EnumChildWindows(int hWndParent, CallBack lpfn, int lParam);
        [DllImport("user32.dll")]
        static extern int GetWindowText(int hWnd, StringBuilder text, int count);
        [DllImport("user32.dll", EntryPoint = "GetDlgCtrlID")]
        private static extern int GetDlgCtrlID(IntPtr hDlg);


        private void button1_Click(object sender, EventArgs e)
        {
            string a = textBox1.Text;

            IntPtr hWnd = new IntPtr(0);
            hWnd = FindWindow(null, a);
            if (hWnd != IntPtr.Zero)
            {
                //MessageBox.Show("发现窗口");
                IntPtr zWnd = FindWindowEx(hWnd, IntPtr.Zero, "Edit", null);
   
                SendMessage(zWnd, WM_CHAR, (int)'3', 0);
                SendMessage(zWnd, WM_CHAR, (int)'0', 0);
                SendMessage(zWnd, WM_CHAR, (int)'0', 0);
                SendMessage(zWnd, WM_CHAR, (int)'1', 0);
                SendMessage(zWnd, WM_CHAR, (int)'3', 0);
                SendMessage(zWnd, WM_CHAR, (int)'8', 0);              
                //Console.ReadLine();
            }
            else
                MessageBox.Show("没找到窗口");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string b = textBox1.Text;
            IntPtr hWnd = new IntPtr(0);
            hWnd = FindWindow(null, b);
            if (hWnd != IntPtr.Zero)
            {
                //MessageBox.Show("发现窗口");
                IntPtr zWnd = FindWindowEx(hWnd, IntPtr.Zero, "Edit", null);
            
                if (zWnd!=IntPtr.Zero)
                {
                    const int buffer_size = 256;
                    StringBuilder buffer = new StringBuilder(buffer_size);
                    SendMessage(zWnd, WM_GETTEXT, buffer_size, buffer);
                   //int length = GetWindowTextLength(zWnd);
                   //StringBuilder s = new StringBuilder(length + 1);
                   //GetWindowText(zWnd, s, s.Capacity);
                   
                   MessageBox.Show(buffer.ToString());
                   textBox2.Text = buffer.ToString();
                }                
            }
        }
    }
}

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