Findwindow根据标题得到其它软件窗体 全球看点


(资料图片仅供参考)

上次已经能够打开win10jh.exe这个软件了,这次我们来研究如何找到这个窗体,然后再对其进行操作。

为了能够看到整个过程,我将这个找窗体的过程放在线程中,然后在主线程中监视一下是不是成功了。注意:我把软件名改成了 win10jh.exe,所以软件的标题为 win10jh.

using System;using System.Diagnostics;using System.IO;using System.Runtime.InteropServices;using System.Threading;using System.Windows.Forms;namespace jhwin{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        //1、引用API        [DllImport("user32.dll", EntryPoint = "FindWindow")]        private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);        private void Form1_Load(object sender, EventArgs e)        {            label2.Text = "等待激活软件运行...";            //2、允许在线程中更改控件值            CheckForIllegalCrossThreadCalls = false;            //3、在线程中运行 RunJHexe ,好观察结果            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);            //4、观察 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;                }            }        }    }}

运行结果

IntPtr startwin 这里就得到了当前软件的窗体,然而这个软件的正式操作界面标题并不是win10jh.而是

这个硬抄的话肯定不科学,还是得想办法得到这个标题,弄成参数比较好。下次再说。

关键词: