From ce4614c4eedd976a53cf0fcd6a352eb1842e15f6 Mon Sep 17 00:00:00 2001 From: lalakii Date: Wed, 24 Jul 2024 14:24:31 +0800 Subject: [PATCH] =?UTF-8?q?:rocket:=20=E4=BF=AE=E5=A4=8D=E4=B8=80=E4=BA=9B?= =?UTF-8?q?=E8=8E=AB=E5=90=8D=E5=85=B6=E5=A6=99=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MouseClickTool.Designer.cs | 22 ++++++++-------- MouseClickTool.cs | 51 +++++++++++++++++++++----------------- MouseClickTool.csproj | 6 +++-- MouseClickTool.sln | 1 + 4 files changed, 44 insertions(+), 36 deletions(-) diff --git a/MouseClickTool.Designer.cs b/MouseClickTool.Designer.cs index fb9eeca..cb5ac6a 100644 --- a/MouseClickTool.Designer.cs +++ b/MouseClickTool.Designer.cs @@ -39,7 +39,7 @@ private void InitializeComponent() this.label4 = new System.Windows.Forms.Label(); this.btnStart = new System.Windows.Forms.Button(); this.btnClose = new System.Windows.Forms.Label(); - this.btnMin = new System.Windows.Forms.Label(); + this.btnHide = new System.Windows.Forms.Label(); this.groupBox1.SuspendLayout(); this.groupBox3.SuspendLayout(); this.SuspendLayout(); @@ -166,15 +166,15 @@ private void InitializeComponent() this.btnClose.TabIndex = 3; this.btnClose.Text = "×"; // - // btnMin + // btnHide // - this.btnMin.AutoSize = true; - this.btnMin.Font = new System.Drawing.Font("宋体", 24F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); - this.btnMin.Location = new System.Drawing.Point(270, 16); - this.btnMin.Name = "btnMin"; - this.btnMin.Size = new System.Drawing.Size(37, 40); - this.btnMin.TabIndex = 5; - this.btnMin.Text = "-"; + this.btnHide.AutoSize = true; + this.btnHide.Font = new System.Drawing.Font("宋体", 24F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); + this.btnHide.Location = new System.Drawing.Point(270, 16); + this.btnHide.Name = "btnHide"; + this.btnHide.Size = new System.Drawing.Size(37, 40); + this.btnHide.TabIndex = 5; + this.btnHide.Text = "-"; // // MouseClickTool // @@ -183,7 +183,7 @@ private void InitializeComponent() this.BackColor = System.Drawing.SystemColors.Window; this.ClientSize = new System.Drawing.Size(358, 243); this.Controls.Add(this.groupBox1); - this.Controls.Add(this.btnMin); + this.Controls.Add(this.btnHide); this.Controls.Add(this.btnClose); this.Controls.Add(this.groupBox3); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; @@ -210,7 +210,7 @@ private void InitializeComponent() private System.Windows.Forms.Button btnStart; private System.Windows.Forms.ComboBox clickType; private System.Windows.Forms.Label btnClose; - private System.Windows.Forms.Label btnMin; + private System.Windows.Forms.Label btnHide; private System.Windows.Forms.ComboBox hotkeys; private System.Windows.Forms.Label label3; } diff --git a/MouseClickTool.cs b/MouseClickTool.cs index 1d38385..383cb39 100644 --- a/MouseClickTool.cs +++ b/MouseClickTool.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics; using System.Drawing; using System.IO; using System.Reflection; @@ -6,8 +7,8 @@ using System.Threading.Tasks; using System.Windows.Forms; -[assembly: AssemblyVersion("1.7.0.2")] -[assembly: AssemblyFileVersion("1.7.0.2")] +[assembly: AssemblyVersion("1.7.0.3")] +[assembly: AssemblyFileVersion("1.7.0.3")] [assembly: AssemblyTitle("MouseClickTool")] [assembly: AssemblyProduct("MouseClickTool")] [assembly: AssemblyCopyright("Copyright (C) 2024 lalaki.cn")] @@ -22,14 +23,12 @@ public partial class MouseClickTool : Form private bool running = false; private const int waitSeconds = 3; private const int hotkeyId = 0x233; + private TaskCompletionSource source; private readonly string[] config = { "F1", "1000", "0" }; [DllImport("user32.dll")] private static extern bool SetProcessDPIAware(); - [DllImport("shell32.dll")] - private static extern int ShellExecute(int hwnd, string lpOperation, string lpFile, string lpParameters, string lpDirectory, int nShowCmd); - //参考:https://stackoverflow.com/questions/5094398/how-to-programmatically-mouse-move-click-right-click-and-keypress-etc-in-winfo [DllImport("user32.dll")] private static extern uint SendInput(uint nInputs, ref Input pInputs, int cbSize); @@ -88,7 +87,7 @@ private void UpdateBtnStartText() btnStart.Text = (running ? "停止" : "开始") + "(" + config[0] + ")"; } - private MouseClickTool() + public MouseClickTool() { InitializeComponent(); var configFile = Path.Combine(Path.GetTempPath(), "lalaki_mouse_click_tool.ini"); @@ -119,7 +118,7 @@ private MouseClickTool() delayVal.Text = config[1]; int.TryParse(config[2], out int clickTypeIndex); clickType.SelectedIndex = clickTypeIndex; - btnUrl.Click += (__, _) => ShellExecute(0, "open", btnUrl.Text, "", "", 1); + btnUrl.Click += (__, _) => Process.Start(btnUrl.Text); btnClose.Click += (__, _) => { Hide(); @@ -127,14 +126,15 @@ private MouseClickTool() Application.Exit(); }; FormClosing += (__, _) => File.WriteAllLines(configFile, config); - btnMin.Click += (__, _) => WindowState = FormWindowState.Minimized; + btnHide.Click += (__, _) => WindowState = FormWindowState.Minimized; btnClose.MouseHover += (__, _) => btnClose.ForeColor = Color.IndianRed; - btnClose.MouseLeave += (__, _) => btnClose.ForeColor = Control.DefaultForeColor; - btnMin.MouseHover += (__, _) => btnMin.ForeColor = Color.DodgerBlue; - btnMin.MouseLeave += (__, _) => btnMin.ForeColor = Control.DefaultForeColor; + btnHide.MouseHover += (__, _) => btnHide.ForeColor = Color.MediumPurple; + EventHandler leave = delegate (object o, EventArgs __) { ((Control)o).ForeColor = DefaultForeColor; }; + btnHide.MouseLeave += leave; + btnClose.MouseLeave += leave; Resize += (_, __) => WindowState = WindowState == FormWindowState.Maximized ? FormWindowState.Normal : WindowState; MouseUp += (_, __) => Cursor = Cursors.Default; - MouseDown += (_, e) => Tag = (e.Button == MouseButtons.Left) ? (object)e.Location : null; + MouseDown += (_, e) => Tag = (e.Button == MouseButtons.Left) ? e.Location : null; MouseMove += (_, e) => { if (e.Button == MouseButtons.Left && Tag is Point offset) @@ -164,14 +164,11 @@ private MouseClickTool() btnStart.Enabled = clickType.Enabled = false; Task.Run(async () => { - await Task.Run(async () => + for (int i = 1; i < waitSeconds; i++) { - for (int i = 1; i < waitSeconds; i++) - { - btnStart.Text = string.Format("{0}", waitSeconds - i); - await Task.Delay(1000); - } - }); + btnStart.Text = string.Format("{0}", waitSeconds - i); + await Task.Delay(1000); + } btnStart.Enabled = true; UpdateBtnStartText(); var downFlag = MouseEventFlag.MOUSEEVENTF_LEFTDOWN; @@ -182,6 +179,7 @@ await Task.Run(async () => upFlag = MouseEventFlag.MOUSEEVENTF_RIGHTUP; } var size = Marshal.SizeOf(input); + source = new TaskCompletionSource(); while (running) { await Task.Run(async () => @@ -192,7 +190,7 @@ await Task.Run(async () => SendInput(1, ref input, size); if (delay != 0) { - await Task.Delay(delay); + await Task.WhenAny(Task.Delay(delay), source.Task); } }); } @@ -200,9 +198,9 @@ await Task.Run(async () => { await Task.Delay(1000); } - clickType.Enabled = true; - delayVal.ReadOnly = false; UpdateBtnStartText(); + delayVal.ReadOnly = false; + btnStart.Enabled = clickType.Enabled = true; }); } else @@ -212,7 +210,14 @@ await Task.Run(async () => } else { - running = false; + btnStart.Enabled = running = false; + try + { + source.SetCanceled(); + } + catch + { + } } }; } diff --git a/MouseClickTool.csproj b/MouseClickTool.csproj index c602b78..1e59090 100644 --- a/MouseClickTool.csproj +++ b/MouseClickTool.csproj @@ -2,6 +2,7 @@ + preview {64FA4132-AC70-4006-9495-F974EE2DD808} WinExe def @@ -28,9 +29,9 @@ true - x64 + x86 obj\ - 4 + 5 Off none 512 @@ -42,6 +43,7 @@ AllRules.ruleset none False + IDE0039;S2486;S108 diff --git a/MouseClickTool.sln b/MouseClickTool.sln index c16b561..794b2f5 100644 --- a/MouseClickTool.sln +++ b/MouseClickTool.sln @@ -11,6 +11,7 @@ Global EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {64FA4132-AC70-4006-9495-F974EE2DD808}.Default|Windows.ActiveCfg = Default|x64 + {64FA4132-AC70-4006-9495-F974EE2DD808}.Default|Windows.Build.0 = Default|x64 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE