Jacklovely

导航

 

2026年5月19日14:01:33

因为工作需要,要在内网一个网页页面一直点击一个按钮,长时间的点击对手和鼠标都是一种折磨,遂想要写一个小软件自动点击。

因为内网电脑不能安装乱七八糟的软件,就把按键精灵等商用软件排除了,只能自己写。开始尝试用VBA,但是后来发现似乎不能后台运行,切换出excel后就失效了。又准备用VB,但是windows的键盘钩子和API函数实在理解不了,翻了很多零几年的书也没找到头绪。又转头用了python,用的时候python已经是3.14版本了,好不容易写完有用tkinter加上了界面,在自己的win10电脑运行起来,考到内网的win7系统,直接报俩dll丢失的错误,心态当场就崩了。查询才知道win7已经不支持python3.8之后的版本了,那我用的pyautogui这些库还得跟着降级啊?遂决定放弃python。然后专门研究了一天windows系统下开发软件推荐的语言,有rust,golang,c#,aardio,autohotkey,当然推荐最多的还是python,但是已经吃了python版本的苦不想再尝试了,推荐python的理由无一例外都是库多,AI写得好,简单。但是再好也不适合win系统下运行,而且打包体积巨大。C#我之前用过,好是好,毕竟微软亲儿子,但是我不想写个小东西还要再安装十几个G的VS,而且C#写的东西还要考虑.net framework的版本,win7和win10并不兼容。最后选择了delphi7版本,在豆包的帮助下,一上午好歹是做完了,生成的exe文件大小才400K,成功在各台win7、win10内网电脑运行。

Unit1.pas

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    cboSeconds: TComboBox;
    Timer1: TTimer;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    FixX, FixY: Integer;
    procedure WMHOTKEY(var Msg: TMessage); message WM_HOTKEY;
  public
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

const
  ID_F7 = 1;
  ID_F8 = 2;

procedure DoClick(X,Y: Integer);
begin
  SetCursorPos(X, Y);
  mouse_event(MOUSEEVENTF_LEFTDOWN, 0,0,0,0);
  Sleep(15);
  mouse_event(MOUSEEVENTF_LEFTUP, 0,0,0,0);
end;

procedure TForm1.FormCreate(Sender: TObject);
var i: Integer;
begin
  SetWindowPos(Handle, HWND_TOPMOST, 0,0,0,0, SWP_NOMOVE or SWP_NOSIZE);

  cboSeconds.Clear;
  for i := 1 to 10 do cboSeconds.Items.Add(IntToStr(i));
  
  // 默认选中 2秒
  cboSeconds.ItemIndex := 1;

  RegisterHotKey(Handle, ID_F7, 0, VK_F7);
  RegisterHotKey(Handle, ID_F8, 0, VK_F8);

  Timer1.Enabled := False;
  Label4.Caption := '状态:已暂停';
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  UnregisterHotKey(Handle, ID_F7);
  UnregisterHotKey(Handle, ID_F8);
  SetWindowPos(Handle, HWND_NOTOPMOST,0,0,0,0, SWP_NOMOVE or SWP_NOSIZE);
end;

procedure TForm1.WMHOTKEY(var Msg: TMessage);
var pt: TPoint; sec: Integer;
begin
  if Msg.WParam = ID_F7 then
  begin
    GetCursorPos(pt);
    FixX := pt.X;
    FixY := pt.Y;

    TryStrToInt(cboSeconds.Text, sec);
    Timer1.Interval := sec * 1000;
    Timer1.Enabled := True;

    Label4.Caption := '状态:正在运行,间隔 ' + IntToStr(sec) + '';
  end;

  if Msg.WParam = ID_F8 then
  begin
    Timer1.Enabled := False;
    Label4.Caption := '状态:已暂停';
  end;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  DoClick(FixX, FixY);
end;

end.

 

Unit1.dfm

object Form1: TForm1
  Left = 1
  Top = 173
  Width = 336
  Height = 189
  Caption = '定点自动点击器'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  KeyPreview = True
  OldCreateOrder = False
  OnCreate = FormCreate
  OnDestroy = FormDestroy
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 30
    Top = 25
    Width = 84
    Height = 13
    Caption = '点击间隔秒数:'
  end
  object Label2: TLabel
    Left = 50
    Top = 70
    Width = 50
    Height = 16
    Caption = 'F7 运行'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clBlue
    Font.Height = -13
    Font.Name = 'Tahoma'
    Font.Style = [fsBold]
    ParentFont = False
  end
  object Label3: TLabel
    Left = 180
    Top = 70
    Width = 50
    Height = 16
    Caption = 'F8 暂停'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clRed
    Font.Height = -13
    Font.Name = 'Tahoma'
    Font.Style = [fsBold]
    ParentFont = False
  end
  object Label4: TLabel
    Left = 30
    Top = 110
    Width = 96
    Height = 16
    Caption = '状态:已暂停'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clBlack
    Font.Height = -13
    Font.Name = 'Tahoma'
    Font.Style = [fsBold]
    ParentFont = False
  end
  object cboSeconds: TComboBox
    Left = 105
    Top = 22
    Width = 150
    Height = 21
    Style = csDropDownList
    ItemHeight = 13
    TabOrder = 0
  end
  object Timer1: TTimer
    Enabled = False
    OnTimer = Timer1Timer
    Left = 40
    Top = 10
  end
end

界面:

image

 

posted on 2026-05-19 14:30  Jacklovely  阅读(18)  评论(0)    收藏  举报