现在我们要得到激活程序的标题
这个当然可以用spy++,但是如果每次都用那个东西应该挺麻烦,干脆我自己做一个功能来随时得到其它软件的标题,这样会方便得多。我想要的结果如下:
这里我贴出所有代码
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
本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828
© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号