Skip to content

Commit

Permalink
🚀 修复一些莫名其妙的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
lalakii committed Jul 24, 2024
1 parent 59a58e6 commit ce4614c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 36 deletions.
22 changes: 11 additions & 11 deletions MouseClickTool.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 28 additions & 23 deletions MouseClickTool.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
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")]
Expand All @@ -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<int> 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);
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -119,22 +118,23 @@ 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();
running = false;
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)
Expand Down Expand Up @@ -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;
Expand All @@ -182,6 +179,7 @@ await Task.Run(async () =>
upFlag = MouseEventFlag.MOUSEEVENTF_RIGHTUP;
}
var size = Marshal.SizeOf(input);
source = new TaskCompletionSource<int>();
while (running)
{
await Task.Run(async () =>
Expand All @@ -192,17 +190,17 @@ await Task.Run(async () =>
SendInput(1, ref input, size);
if (delay != 0)
{
await Task.Delay(delay);
await Task.WhenAny(Task.Delay(delay), source.Task);
}
});
}
if (delay == 0)
{
await Task.Delay(1000);
}
clickType.Enabled = true;
delayVal.ReadOnly = false;
UpdateBtnStartText();
delayVal.ReadOnly = false;
btnStart.Enabled = clickType.Enabled = true;
});
}
else
Expand All @@ -212,7 +210,14 @@ await Task.Run(async () =>
}
else
{
running = false;
btnStart.Enabled = running = false;
try
{
source.SetCanceled();
}
catch
{
}
}
};
}
Expand Down
6 changes: 4 additions & 2 deletions MouseClickTool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<LangVersion>preview</LangVersion>
<ProjectGuid>{64FA4132-AC70-4006-9495-F974EE2DD808}</ProjectGuid>
<OutputType>WinExe</OutputType>
<RootNamespace>def</RootNamespace>
Expand All @@ -28,9 +29,9 @@
<NoWin32Manifest>true</NoWin32Manifest>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Default|x64' ">
<PlatformTarget>x64</PlatformTarget>
<PlatformTarget>x86</PlatformTarget>
<OutputPath>obj\</OutputPath>
<WarningLevel>4</WarningLevel>
<WarningLevel>5</WarningLevel>
<GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
<ErrorReport>none</ErrorReport>
<FileAlignment>512</FileAlignment>
Expand All @@ -42,6 +43,7 @@
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
<DebugType>none</DebugType>
<UseWinFormsOutOfProcDesigner>False</UseWinFormsOutOfProcDesigner>
<NoWarn>IDE0039;S2486;S108</NoWarn>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
Expand Down
1 change: 1 addition & 0 deletions MouseClickTool.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ce4614c

Please sign in to comment.