1、首先我们需要创建一个窗体,然后将控件拖入:

2、就是事件,每个按钮的事件如下:
button1:获取弹框信息的按钮
-MouseDown 鼠标按下事件
-MouseUp 鼠标弹起事件
-MouseMove 鼠标移动事件
button2:修改句柄标题的按钮
-Click 单击事件


3、创建这些事件,并填入代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace GetWin
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 获取鼠标位置
/// </summary>
/// <param name="lpPoint">The lp point.</param>
/// <returns></returns>
[DllImport("user32.dll")]
public static extern int GetCursorPos(ref Point lpPoint); //获取鼠标坐标,该坐标是光标所在的屏幕坐标位置
/// <summary>
/// 根据位置获取窗体句柄
/// </summary>
/// <param name="xPoint">The x point.</param>
/// <param name="yPoint">The y point.</param>
/// <returns></returns>
[DllImport("user32.dll")]
public static extern int WindowFromPoint(int xPoint, int yPoint); //指定坐标处窗体句柄
/// <summary>
///获取窗体标题
/// </summary>
/// <param name="hwnd">The HWND.</param>
/// <param name="lpString">The lp string.</param>
/// <param name="nMaxCount">The n maximum count.</param>
/// <returns></returns>
[DllImport("user32.dll")]
public static extern int GetWindowText(int hwnd, StringBuilder lpString, int nMaxCount);//获取窗体标题名称
/// <summary>
///获取句柄类名
/// </summary>
/// <param name="hwnd">The HWND.</param>
/// <param name="lpstring">The lpstring.</param>
/// <param name="nMaxCount">The n maximum count.</param>
/// <returns></returns>
[DllImport("user32.dll")]
public static extern int GetClassName(int hwnd, StringBuilder lpstring, int nMaxCount); //获取窗体类名称
/// <summary>
///修改句柄信息
/// </summary>
/// <param name="hwnd">The HWND.</param>
/// <param name="wMsg">The w MSG.</param>
/// <param name="wParam">The w parameter.</param>
/// <param name="lParam">The l parameter.</param>
/// <returns></returns>
[DllImport("user32.dll", EntryPoint = "SendMessageA")]
public static extern int SendMessage(int hwnd, int wMsg, int wParam, string lParam);//这个SendMessage是个好东西,可以实现很多功能,在这里是显示更改窗体的标题。
/// <summary>
/// 获取PID
/// </summary>
/// <param name="handle">The handle.</param>
/// <param name="processId">The process identifier.</param>
/// <returns></returns>
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern int GetWindowThreadProcessId(IntPtr handle, out uint processId);
/// <summary>
/// windowapi 找到指定窗体的句柄函数
/// </summary>
/// <param name="lpClassName">窗体类名</param>
/// <param name="lpWindowName">窗体标题名</param>
/// <returns>返回窗体句柄(IntPtr)</returns>
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
bool isMouseUp = false;//判断鼠标左右键是否被按下状态
Point pi = new Point();//鼠标的坐标
int hwnd;//窗体句柄
/// <summary>
///修改标题
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
private void button2_Click(object sender, EventArgs e)
{
SendMessage(hwnd, 12, 0, textBox1.Text);//利用api操作窗体标题,标题为textBox1.Text里面的text。
}
/// <summary>
///按下鼠标左键,开始获取
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param>
private void button1_MouseDown(object sender, MouseEventArgs e)
{
isMouseUp = true;//鼠标左右键被按下
Cursor = Cursors.Cross; //改变鼠标样式为十字架
}
/// <summary>
///左键弹起,不再获取
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param>
private void button1_MouseUp(object sender, MouseEventArgs e)
{
isMouseUp = false;//鼠标左右键被弹起
Cursor = Cursors.Default;//改变鼠标样式为默认
}
/// <summary>
///鼠标移动 如果按下则开始获取
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param>
private void button1_MouseMove(object sender, MouseEventArgs e)
{
//当鼠标移动时发生
if (isMouseUp)//左键是否被按下
{
GetCursorPos(ref pi); //获取鼠标坐标值
label5.Text = "坐标:X=" + pi.X + " | Y=" + pi.Y; //label1显示鼠标坐标值的x值与y值
hwnd = WindowFromPoint(pi.X, pi.Y);//获取坐标值下的窗体句柄
label6.Text = "句柄:" + hwnd;//lable2显示句柄,貌似SPY++是十六进制的,这个是十进制的
StringBuilder titlename = new StringBuilder(256);
GetWindowText(hwnd, titlename, 256);//name得到窗体的标题
label7.Text = "标题:" + titlename.ToString();//label3显示标题
StringBuilder classname = new StringBuilder(256);
GetClassName(hwnd, classname, 256);//name得到class的名称(这个我也不知道叫什么)
label8.Text = "名称:" + classname.ToString();//显示class的名称
IntPtr intp = FindWindow(classname.ToString(), titlename.ToString());
label9.Text = "路径:" + GetProcessPath(intp);
Color col = GetPixelColor(pi.X, pi.Y);
string colstr = ColorTranslator.ToHtml(col);
label10.ForeColor = col;
label10.Text = "颜色:" + colstr;
}
}
//声明获取屏幕指定坐标点的颜色 所需的画板
private static Bitmap cache = new Bitmap(1, 1);
private static Graphics tempGraphics = Graphics.FromImage(cache);
/// <summary>
///获取屏幕指定坐标点的颜色
/// </summary>
/// <param name="x">The x.</param>
/// <param name="y">The y.</param>
/// <returns></returns>
public Color GetPixelColor(int x, int y)
{
tempGraphics.CopyFromScreen(x, y, 0, 0, new Size(1, 1));
return cache.GetPixel(0, 0);
}
/// <summary>
/// 截图
/// </summary>
public void GetFromScreen()
{
Bitmap cache1 = new Bitmap(500, 500);
Graphics tempGraphics1 = Graphics.FromImage(cache1);
tempGraphics1.CopyFromScreen(500, 500, 0, 0, new Size(500, 500));
cache1.Save("Screen.png", ImageFormat.Png);
}
/// <summary>
///根据句柄获取路径
/// </summary>
/// <param name="hwnd">The HWND.</param>
/// <returns></returns>
public static string GetProcessPath(IntPtr hwnd)
{
if ((int)hwnd == 0)
{
return "";
}
try
{
uint pid = 0;
GetWindowThreadProcessId(hwnd, out pid);
Process proc = Process.GetProcessById((int)pid);
return proc.MainModule.FileName;
}
catch
{
return "";
}
}
}
}4、启动看效果

点击开始获取按钮,并按住鼠标左键,移动到弹框的title上,并松开左键,举个例子:

看到路径了吗,可以直接去这个位置,将这个软件删除就不会有弹框了。
然后我们来尝试修改这个弹框的标题:

哈哈,成功了,但是这个修复只是当前这次启动可以改,弹框重启之后,又会变回来,主要就是想表达的是,拿到句柄,我们就能操作他了。
川公网安备 51010702003150号
留下您的脚步
最近评论