• .NET 调用WINAPI 获取其他应用窗体内容


    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 cstest
    {
        public partial class Form1 : Form
        {
            public delegate bool CallBack(IntPtr hwnd, int lParam);
            private static CallBack myCallBack;
            public Form1()
            {
                myCallBack = new CallBack(Report);
            InitializeComponent();
               //获取窗口名称为C#的窗口句柄
                GetHandle("C#");
                this.textBox1.Multiline = true;
                this.textBox1.Dock = DockStyle.Fill;
            }
    
            private void GetHandle(string windcaption)
            {
                //调用WINAPI,根据名称取窗口句柄
                IntPtr mainHandle = FindWindow(null, windcaption);
                if (IntPtr.Zero != mainHandle)
                {
                    AppendText(string.Format("{0}句柄:{1}", windcaption, Convert.ToString((int)mainHandle,16)));
                    //枚举所有子窗体
                    //EnumChildWindows((int)mainHandle, myCallBack, 0);
                    //修改窗口标题为C++
                    SetWindowText((int)mainHandle, "C++");
                    StringBuilder s = new StringBuilder(512);
                //通过WINAPI获取控件标题,该方法不能获取到edit中的值,只能取button或窗体的caption
                    int i = GetWindowText(mainHandle, s, s.Capacity);
                    AppendText(string.Format("句柄{0}的caption:{1}", Convert.ToString((int)mainHandle, 16), s.ToString()));
               //枚举所有子窗体,并将子窗体句柄传给myCallBack
                    EnumChildWindows((int)mainHandle, myCallBack, 0);
                }
            }
    
            //输出调试信息
            private void AppendText(string msg)
            {
                this.textBox1.AppendText(msg);
                this.textBox1.AppendText("
    ");
            }
            //根据句柄,输出句柄对应窗体的caption
            public bool Report(IntPtr hWnd, int lParam)
            {
                StringBuilder s = new StringBuilder(512);
                int i = GetWindowText((IntPtr)hWnd, s, s.Capacity);
                AppendText(string.Format("句柄{0}的caption:{1}", Convert.ToString((int)hWnd, 16), s.ToString()));
                return true;
            }
    
            /// <summary>
            /// 获取窗体的句柄函数
            /// </summary>
            /// <param name="lpClassName">窗口类名</param>
            /// <param name="lpWindowName">窗口标题名</param>
            /// <returns>返回句柄</returns>
            [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
            public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    
            [DllImport("user32.dll")]
            public static extern int EnumWindows(CallBack x, int y);
    
            [DllImport("user32.dll")]
            private static extern IntPtr EnumChildWindows(int hWndParent, CallBack lpEnumFunc, int lParam);
    
            [DllImport("user32.dll")]
            public static extern int SetWindowText(int handle, string title);
    
            [DllImport("user32.dll", EntryPoint = "GetWindowText")]
            public static extern int GetWindowText(IntPtr hwnd, StringBuilder lpString,int cch);
    
            [DllImport("User32.dll", EntryPoint = "SendMessage")]
            private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, StringBuilder lParam);
    
            private void button1_Click(object sender, EventArgs e)
            {
                StringBuilder s = new StringBuilder(512);
                //获取控件标题,对TRichEdit无效
                //int i = GetWindowText((IntPtr)int.Parse(txtHandle.Text), s, s.Capacity);
                //获取控件内容,对TRichEdit有效
                int i = SendMessage((IntPtr)int.Parse(txtHandle.Text), 0x000D, 1000, s);
                AppendText(string.Format("句柄{0}的caption:{1}", txtHandle.Text, s.ToString()));
            }
        }
    }
    
  • 相关阅读:
    FNDLOAD 迁移多语言的数据定义
    Failed to lock the main memory
    fnd_function.execute/app_navigate.execute区别
    EBS Form 高亮当前行/设置行背景色
    Fndload常用命令
    EBS Form中数据提交方式(COMMIT)
    隆回护照/港澳通行证办理流程
    浅析淘宝刷单--我们如何网购
    初识 Bootstrap
    框架、架构、设计模式
  • 原文地址:https://www.cnblogs.com/yesok/p/12892759.html
Copyright © 2020-2023  润新知