往期内容回顾
前面已经写过两篇相关的博客了:1.C#制作网站挂机程序:
(https://blog.youkuaiyun.com/stwzx/article/details/121042382)
2.Python构建自动在线刷视频—一个只能做不能说的项目:
(https://blog.youkuaiyun.com/stwzx/article/details/121519322)
3.【C#制作挂机程序V2.0—刷鼠标单击类在线视频程序】下载地址:
(https://download.youkuaiyun.com/download/stwzx/74089052)
第一篇文章主要在教大家怎样用C#来做一个挂机程序,代码也相对简朴,主要是完成了一个简单的功能,可以处理网页中的弹出的alert对话框。第二篇文章采用Python语言,完全控制浏览器,并能抓取网页中的Tag、id、name或者CSS等标记并进行键盘、鼠标输入,应该是比较完美的程序。但今天为什么又再次回到C#开发的老路上走自动化键盘、鼠标单击事件的挂机程序呢?Python不香吗?代码君不禁呵呵了,Python的坑不可谓不多啊,反爬技术直接让Python驱动浏览器bye bye了。
还是直接上干货吧
一、程序界面
程序还挺简洁的,不过不要小看它,功能可不小!
二、功能说明
1.程序已经集成了Microsoft Spy++的功能
2.通过拖动工具自动获取窗体的Title
3.拖动工具自动获取鼠标准备单击点的坐标
4.通过时间设置单击事件后等待时间(刷视频必备)
5.浏览器应用程序不再局限于FireFox
6.适应性增加,只要是通过鼠标单击完成的操作都可以交给它。
7.对窗体程序的自动化单击也适用
强调一下,拖动工具得到坐标和窗体标题的做法,在C#开发中的实现,有可能仅此一家哦。因为本码农搜遍百度都没找到,有此功能的都是C++开发的。所以这个创意在C#应用完全是本人独有。
三、程序使用步骤
核心使用功能展示:
1.程序运行后单击【获取浏览器标题:】,将出现工具如下:
2.拖动放大镜工具到对应的程序窗体,将自动获取到窗体标题
这个功能主要目的还是为了获取到应用程序的句柄,有了句柄可以把窗体设置为最上层显示。(不被其它窗体遮盖)
3.单击【获取坐标】按钮,将出现工具如下:
4.拖动放大镜工具到需要自动单击的第一个点,松开,将弹出如下窗口:
坐标不用设置,就是刚才鼠标松开的位置,停顿时间是单击后停多少时间,进行下一个操作的时间。
5.循环3和4的操作,可以得到一系列的坐标点和相应停顿时间
6.全部设置完成后,直接单击【Start】按钮启动自动化操作。
7.注意,这个自动化操作将周而复始的进行,可以直接关闭这个程序来关闭这个自动化操作。也可以单击【保存坐标】会把坐标点和时间保存到文件中,重新启动时会自动加载已经存在的坐标点和时间。
三、程序关键代码
1.API引用及成员变量
#region API及成员变量
/// <summary>
/// 根据坐标获取窗口句柄
/// </summary>
/// <param name="point">坐标</param>
/// <returns></returns>
[DllImport("user32.dll")]
private static extern IntPtr WindowFromPoint(Point point);
public delegate bool EnumChildWindow(IntPtr WindowHandle, string num);
/// <summary>
/// 传递消息给记事本
/// </summary>
/// <param name="hWnd"></param>
/// <param name="Msg"></param>
/// <param name="wParam"></param>
/// <param name="lParam"></param>
/// <returns></returns>
[DllImport("User32.DLL")]
public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, string lParam);
[DllImport("user32.dll", EntryPoint = "keybd_event", SetLastError = true)]
public static extern void keybd_event(Keys bVk, byte bScan, uint dwFlags, uint dwExtraInfo);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(int hWnd);
[DllImport("User32.dll")]
public static extern int EnumChildWindows(IntPtr WinHandle, EnumChildWindow ecw, string name);
[DllImport("User32.dll")]
public static extern int GetWindowText(IntPtr WinHandle, StringBuilder Title, int size);
[DllImport("user32.dll")]
public static extern int GetClassName(IntPtr WinHandle, StringBuilder Type, int size);
[DllImport("user32")]
private static extern int GetWindowThreadProcessId(IntPtr handle, out int pid);
[DllImport("user32")]
public static extern IntPtr SetActiveWindow(IntPtr hWnd);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);
[DllImport("user32.dll", EntryPoint = "FindWindow")]
private static extern IntPtr FindWindow(string IpClassName, string IpWindowName);
//查找窗体控件
public int iSecond = 30;
public delegate bool CallBack(int hwnd, int lParam);
public RECT rectMain = new RECT();
private string typeName;
private IntPtr mainHwnd;
public IntPtr ip;
private string BSType = "Chrome_WidgetWin_1";
bool Flag = false;
int X;
int Y;
int times;
private IntPtr mainWindowHandle;
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int X; //最左坐标
public int Y; //最上坐标
public int Height; //最右坐标
public int Width; //最下坐标
}
/// <summary>
/// 查找句柄
/// </summary>
/// <param name="hwndParent"></param>
/// <param name="hwndChildAfter"></param>
/// <param name="lpszClass"></param>
/// <param name="lpszWindow"></param>
/// <returns></returns>
[DllImport("User32.DLL")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("User32")]
public extern static void mouse_event(int dwFlags, int dx, int dy, int dwData, IntPtr dwExtraInfo);
[DllImport("user32.dll")]
static extern bool SetCursorPos(int X, int Y);
public const uint WM_SETTEXT = 0x000C;
public System.Diagnostics.Process Proc;
public System.Windows.Forms.Timer myTimer;
public List<Opt> optList = new List<Opt>();
public Queue<Opt> optQueue = new Queue<Opt>();
#endregion
2.移动鼠标代码
public void MoveTo(int x1, int y1, int x2, int y2)
{
float k = (float)(y2 - y1) / (float)(x2 - x1);
float b = y2 - k * x2;
for (int x = x2; x != x1; x = x + Math.Sign(x1 - x2))
{
//MoveTo(x1,y1,x,(k*x+b));
SetCursorPos(x, (int)(k * x + b));
Thread.Sleep(3);
}
}
代码如下(示例):
3.Start事件代码
private void btnStart_Click(object sender, EventArgs e)
{
foreach (string strLine in richTextBox1.Lines)
{
string[] strInt = strLine.Split(new string[] { "," }, StringSplitOptions.None);
if (strInt.Length <= 2)//小于2个参数:一个Point由X,Y组成
{
continue;
}
int x = int.Parse(strInt[0]);
int y = int.Parse(strInt[1]);
int times = int.Parse(strInt[2]);
optList.Add(new Opt(x, y, times));
}
foreach (Opt opt in optList)
{
optQueue.Enqueue(opt);
}
IntPtr hnd = FindWindow(null, txtTitle.Text);
IntPtr ip = hnd;
lblMessage.Text = ip.ToString();
iSecond = 1;
//myTimer.Interval = 1000 * iSecond;
myTimer.Interval = 1000 * 60 * iSecond;
myTimer.Enabled = false;
FindWindowClass.TopMostWindow.SetTopomost(ip);
SetForegroundWindow((int)ip);
if (optQueue.Count > 0)
{
Opt opt = optQueue.Dequeue();
X = opt.x;
Y = opt.y;
times = opt.Times;
System.Timers.Timer t = new System.Timers.Timer();//实例化
t.Elapsed += new System.Timers.ElapsedEventHandler(CallBack2);
t.AutoReset = false;
t.Interval = 1000 * times;
t.Enabled = true;
}
}
4.定时器事件代码
private void CallBack2(object sender, EventArgs e)
{
MoveTo(X, Y, MousePosition.X, MousePosition.Y);
mouse_event((int)(MouseEventFlags.LeftDown | MouseEventFlags.Absolute), X, Y, 0, IntPtr.Zero);
//Thread.Sleep(200);
mouse_event((int)(MouseEventFlags.LeftUp | MouseEventFlags.Absolute), X, Y, 0, IntPtr.Zero);
if (optQueue.Count > 0)
{
Opt opt = optQueue.Dequeue();
X = opt.x;
Y = opt.y;
times = opt.Times;
System.Timers.Timer t = new System.Timers.Timer();//实例化
t.Elapsed += new System.Timers.ElapsedEventHandler(CallBack2);
t.AutoReset = false;
t.Interval = 1000 * times;
t.Enabled = true;
}
else
{
foreach (Opt opt1 in optList)
{
optQueue.Enqueue(opt1);
}
Opt opt = optQueue.Dequeue();
X = opt.x;
Y = opt.y;
times = opt.Times;
System.Timers.Timer t = new System.Timers.Timer();//实例化
t.Elapsed += new System.Timers.ElapsedEventHandler(CallBack2);
t.AutoReset = false;
t.Interval = 1000 * times;
t.Enabled = true;
}
}
5.启动浏览器事件代码
private void btnStartBrowser_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtFile.Text)) return;
try
{
// 浏览器程序启动线程
Proc = new System.Diagnostics.Process();
Proc.StartInfo.FileName = txtFile.Text;
Proc.StartInfo.Arguments = txtNetAddr.Text; //浏览器打开URL参数
Proc.StartInfo.UseShellExecute = false;
Proc.StartInfo.RedirectStandardInput = true;
Proc.StartInfo.RedirectStandardOutput = true;
Proc.Start();
}
catch
{
Proc = null;
}
}
总结
挂机程序的开发本身是存在着局限性的。往往只能针对某一个或某一类应用程序。但只要你注重思考和挖掘,不管什么样的应用程序,总能找到对应的点开发出相应的挂机程序。要想用一个挂机程序一招吃遍天下是不可行的。