.net 调用 winapi获取窗口句柄和内容
2021-05-30 07:21
                         标签:cli   use   repo   builder   窗体   line   city   无效   com      .net 调用 winapi获取窗口句柄和内容 标签:cli   use   repo   builder   窗体   line   city   无效   com    原文地址:https://www.cnblogs.com/yesok/p/11062008.htmlusing 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
    { 
        //定义一个符合WINAPI返回值和参数的委托
        public delegate bool CallBack(IntPtr hwnd, int lParam);
        //声明符合上述委托的函数(定义一个函数指针)
        private static CallBack myCallBack;
        public Form1()
        {
            myCallBack = new CallBack(Report);
        InitializeComponent();
            GetHandle("C#");
            this.textBox1.Multiline = true;
            this.textBox1.Dock = DockStyle.Fill;
        }
        private void GetHandle(string windcaption)
        {
            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);
                //修改窗口标题
                SetWindowText((int)mainHandle, "C#");
                StringBuilder s = new StringBuilder(512);
                //获取控件标题
                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("\r\n");
        }
        //根据窗体句柄,输出窗体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;
        }
        /// 
下一篇:Windows常用软件
文章标题:.net 调用 winapi获取窗口句柄和内容
文章链接:http://soscw.com/index.php/essay/89452.html