得到其它软件窗体的标题和类名

现在我们要得到激活程序的标题

这个当然可以用spy++,但是如果每次都用那个东西应该挺麻烦,干脆我自己做一个功能来随时得到其它软件的标题,这样会方便得多。我想要的结果如下:


1、界面搞成如下情况:timer1间隔弄成5000,也就是5秒钟运行一次

2、代码功能为:点击按钮,timer1开始收集用户鼠标所在位置的软件窗体的名称和类等附带信息,再点一次,timer1停止收集这些信息。

这里我贴出所有代码

using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace jhwin
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        
        [DllImport("user32.dll", EntryPoint = "FindWindow")]
        private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);

        //引用API,这里多引用了得到类名称的API,反正也是闲着
        //根据坐标得到指定位置句柄
        [DllImport("user32")]
        private static extern IntPtr WindowFromPoint(
            Point Point  //坐标
        );

        //得到窗体名称
        [DllImport("user32", SetLastError = true)]
        public static extern int GetWindowText(
            IntPtr hWnd,//窗口句柄
            StringBuilder lpString,//标题
            int nMaxCount //最大值
        );

        //获取类的名字
        [DllImport("user32.dll")]
        private static extern int GetClassName(
            IntPtr hWnd,//句柄
            StringBuilder lpString, //类名
            int nMaxCount //最大值
            );

        private void Form1_Load(object sender, EventArgs e)
        {
            label2.Text = "等待激活软件运行...";

            CheckForIllegalCrossThreadCalls = false;

            Thread t = new Thread(RunJHexe);
            t.IsBackground = true;
            t.Start();
        }

        //运行激活程序
        void RunJHexe()
        {
            //临时文件夹位置
            var tempPath = Path.Combine(Path.GetTempPath(), "win10jh.exe");
            //将文件写到临时文件夹位置
            File.WriteAllBytes(tempPath, Resource1.win10jh);
            //建立文件信息
            var info = new ProcessStartInfo(tempPath);
            //这里选false的话,需要提升本调试程序权限
            info.UseShellExecute = true;
            //启动程序
            Process.Start(info);

            //观察 win10jh.exe 是否已经运行了。
            FindJHWin();
        }
        void FindJHWin()
        {
            bool OK;

            OK = false;
            while (!OK)
            {
                //主窗口标题 为 win10jh.exe 
                IntPtr startwin = FindWindow(null, "win10jh.exe");
                if (startwin != IntPtr.Zero)
                {
                    label2.Text += "已运行!";
                    OK = true;
                }
            }
        }

        //////////// 上面是上次的内容---------------------------------------------
        
        //点击按钮,控制timer
        private void button1_Click(object sender, EventArgs e)
        {
            if (!timer1.Enabled)
            {
                timer1.Enabled = true;
                timer1.Start();
            }
            else
            {
                timer1.Enabled = false;
                timer1.Stop();
            }
        }

        //得到标题
        private void timer1_Tick(object sender, EventArgs e)
        {
            GetTitle();
        }

        //得到鼠标位置软件标题
        void GetTitle()
        {
            //鼠标位置
            int x = Cursor.Position.X;
            int y = Cursor.Position.Y;
            Point p = new Point(x, y);

            //得到鼠标位窗口句柄
            IntPtr formHandle = WindowFromPoint(p);

            //根据句柄得到窗口的标题
            StringBuilder title = new StringBuilder(256);
            GetWindowText(formHandle, title, title.Capacity);

            //根据句柄得到窗口的类名,这个有时会用到,本次没有用到
            StringBuilder className = new StringBuilder(256);
            GetClassName(formHandle, className, className.Capacity);//得到窗口的句柄

            //标题
            this.textBox1.Text += title.ToString() + "r
";
            //类名
            this.textBox1.Text += className.ToString() + "r
"; ;
        }
    }
}

运行结果如下:

展开阅读全文

页面更新:2024-02-05

标签:窗体   标题   句柄   最大值   软件   坐标   窗口   名称   位置   程序

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号

Top