鼠标模拟点击窗口
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace shubiaodj
{
public class Win32API
{
/// <summary>
/// 该函数将创建指定窗口的线程设置到前台,并且激活该窗口。键盘输入转向该窗口,并为用户改各种可视的记号。系统给创建前台窗口的线程分配的权限稍高于其他线程。
/// </summary>
/// <param name="hwnd"></param>
/// <returns></returns>
[DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern bool SetForegroundWindow(IntPtr hwnd);
/// <summary>
/// 检索指定坐标点的像素的RGB颜色值
/// </summary>
/// <param name="hdc">设备环境句柄</param>
/// <param name="nXPos">指定要检查的像素点的逻辑X轴坐标</param>
/// <param name="nYPos">指定要检查的像素点的逻辑Y轴坐标</param>
/// <returns></returns>
[DllImport("Gdi32.dll")]
public static extern uint GetPixel(IntPtr hdc, int nXPos, int nYPos);
/// <summary>
/// 返回指定窗口的边框矩形的尺寸。该尺寸以相对于屏幕坐标左上角的屏幕坐标给出
/// </summary>
/// <param name="hWnd">窗口句柄</param>
/// <param name="rect">指向一个RECT结构的指针,该结构接收窗口的左上角和右下角的屏幕坐标</param>
/// <returns></returns>
[DllImport("user32.dll")]
public static extern IntPtr GetWindowRect(IntPtr hWnd, ref Rectangle rect);
/// <summary>
/// 函数释放设备上下文环境(DC)供其他应用程序使用。函数的效果与设备上下文环境类型有关。它只释放公用的和设备上下文环境,对于类或私有的则无效。
/// </summary>
/// <param name="hwnd">指向要释放的设备上下文环境所在的窗口的句柄。</param>
/// <param name="hdc">指向要释放的设备上下文环境的句柄。</param>
/// <returns></returns>
[DllImport("user32.dll")]
public static extern Int32 ReleaseDC(IntPtr hwnd, IntPtr hdc);
const int MOUSEEVENTF_MOVE = 0x0001; //移动鼠标
public const int MOUSEEVENTF_LEFTDOWN = 0x0002; //模拟鼠标左键按下
public const int MOUSEEVENTF_LEFTUP = 0x0004; //模拟鼠标左键抬起
const int MOUSEEVENTF_RIGHTDOWN = 0x0008; //模拟鼠标右键按下
const int MOUSEEVENTF_RIGHTUP = 0x0010; //模拟鼠标右键抬起
const int MOUSEEVENTF_MIDDLEDOWN = 0x0020; //模拟鼠标中键按下
const int MOUSEEVENTF_MIDDLEUP = 0x0040; //模拟鼠标中键抬起
const int MOUSEEVENTF_ABSOLUTE = 0x8000; //标示是否采用绝对坐标
/// <summary>
/// 搜所窗口句柄
/// </summary>
/// <param name="lpClassName">null</param>
/// <param name="lpWindowName">窗口名称</param>
/// <returns></returns>
[DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
/// <summary>
/// 取得屏幕
/// </summary>
/// <param name="hwnd"></param>
/// <returns></returns>
[DllImport("user32.dll")]
public static extern IntPtr GetDC(IntPtr hwnd);
/// <summary>
/// 模拟鼠标点击
/// </summary>
/// <param name="dwFlags">下表中标志之一或它们的组合</param>
/// <param name="dx">根据MOUSEEVENTF_ABSOLUTE标志,指定x,y方向的绝对位置或相对位置 </param>
/// <param name="dy">根据MOUSEEVENTF_ABSOLUTE标志,指定x,y方向的绝对位置或相对位置 </param>
/// <param name="cButtons">没有使</param>
/// <param name="dwExtraInfo">没有使</param>
/// <returns></returns>
[DllImport("user32.dll")]
public static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace shubiaodj
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private IntPtr wndPane;
private Rectangle rectPane = new Rectangle();
private int x;
private int y;
private bool startt=false;
private void Form1_Load(object sender, EventArgs e)
{
if (System.IO.File.Exists(Application.StartupPath + "\\data.xml"))
{
DataSet ds = new DataSet();
ds.ReadXml(Application.StartupPath + "\\data.xml");
foreach (DataRow ei in ds.Tables[0].Rows)
{
int i = dataGridView1.Rows.Add();
dataGridView1.Rows[i].Cells["ckmc"].Value = ei["ckmc"];
dataGridView1.Rows[i].Cells["xzb"].Value = ei["xzb"];
dataGridView1.Rows[i].Cells["yzb"].Value = ei["yzb"];
dataGridView1.Rows[i].Cells["ms"].Value = ei["ms"];
}
}
}
private void KS()
{
int countt = dataGridView1.Rows.Count;
int i = 0;
while (startt)
{
DataGridViewRow eo = dataGridView1.Rows[i];
x = Convert.ToInt32(Convert.ToInt32(eo.Cells["xzb"].Value) + rectPane.Left);
y = Convert.ToInt32(Convert.ToInt32(eo.Cells["yzb"].Value) + rectPane.Top);
Win32API.SetForegroundWindow(wndPane);
System.Drawing.Point a = new System.Drawing.Point(x, y);
System.Windows.Forms.Cursor.Position = a;
Win32API.mouse_event(Win32API.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
Win32API.mouse_event(Win32API.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
//msg.Text = "点击成功";
i++;
if (i >= countt)
{
i = 0;
System.Threading.Thread.Sleep(Convert.ToInt32(dataGridView1.Rows[0].Cells["ms"].Value) * 1000);
}
else
{
System.Threading.Thread.Sleep(Convert.ToInt32(dataGridView1.Rows[i].Cells["ms"].Value) * 1000);
}
}
}
private void button1_Click(object sender, EventArgs e)
{
wndPane = Win32API.FindWindow(null, pan.Text);
if (wndPane == IntPtr.Zero)
{
msg.Text = "游戏没有启动";
return;
}
Win32API.GetWindowRect(wndPane, ref rectPane);
if (dataGridView1.Rows.Count<=0)
{
msg.Text = "没有可以操作的数据!!!";
}
System.Threading.Thread m = new Thread(KS);
m.Start();
startt = true;
panel1.Enabled = false;
}
private void timer1_Tick(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
startt = false;
panel1.Enabled = true;
}
private void button4_Click(object sender, EventArgs e)
{
DataTable dtwrite = new DataTable();
dtwrite.Columns.Add("ckmc");
dtwrite.Columns.Add("xzb");
dtwrite.Columns.Add("yzb");
dtwrite.Columns.Add("ms");
foreach (DataGridViewRow ei in this.dataGridView1.Rows)
{
DataRow eo = dtwrite.NewRow();
eo["ckmc"] = ei.Cells["ckmc"].Value;
eo["xzb"] = ei.Cells["xzb"].Value;
eo["yzb"] = ei.Cells["yzb"].Value;
eo["ms"] = ei.Cells["ms"].Value;
dtwrite.Rows.Add(eo);
}
DataSet ds = new DataSet();
ds.Tables.Add(dtwrite);
ds.WriteXml(Application.StartupPath + "\\data.xml");
ds = null;
}
private void button3_Click(object sender, EventArgs e)
{
int i = dataGridView1.Rows.Add();
dataGridView1.Rows[i].Cells["ckmc"].Value = pan.Text;
dataGridView1.Rows[i].Cells["xzb"].Value = xp.Value;
dataGridView1.Rows[i].Cells["yzb"].Value = yp.Value;
dataGridView1.Rows[i].Cells["ms"].Value = maio.Value;
}
private void button5_Click(object sender, EventArgs e)
{
dataGridView1.Rows.Remove(dataGridView1.SelectedRows[0]);
}
}
}