Skip to content

Commit

Permalink
Merge branch 'dev-build' of https://github.com/RyanLua/FluentAutoClicker
Browse files Browse the repository at this point in the history
 into dev-build
  • Loading branch information
RyanLua committed Dec 8, 2024
2 parents dbcff6c + 52466a6 commit b96a72f
Show file tree
Hide file tree
Showing 8 changed files with 295 additions and 307 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ csharp_style_prefer_readonly_struct_member = true
csharp_prefer_braces = true
csharp_prefer_simple_using_statement = true
csharp_prefer_system_threading_lock = true
csharp_style_namespace_declarations = block_scoped
csharp_style_namespace_declarations = file_scoped
csharp_style_prefer_method_group_conversion = true
csharp_style_prefer_primary_constructors = true
csharp_style_prefer_top_level_statements = true
Expand Down
26 changes: 0 additions & 26 deletions .vsconfig
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,6 @@
"components": [
"Microsoft.VisualStudio.Component.CoreEditor",
"Microsoft.VisualStudio.Workload.CoreEditor",
"Microsoft.Net.Component.4.8.SDK",
"Microsoft.Net.Component.4.7.2.TargetingPack",
"Microsoft.Net.ComponentGroup.DevelopmentPrerequisites",
"Microsoft.VisualStudio.Component.Roslyn.Compiler",
"Microsoft.Component.MSBuild",
"Microsoft.VisualStudio.Component.Roslyn.LanguageServices",
"Microsoft.VisualStudio.Component.TextTemplating",
"Microsoft.VisualStudio.Component.NuGet",
"Microsoft.VisualStudio.Component.SQL.CLR",
"Microsoft.Component.ClickOnce",
"Microsoft.VisualStudio.Component.ManagedDesktop.Core",
"Microsoft.NetCore.Component.Runtime.9.0",
"Microsoft.NetCore.Component.Runtime.8.0",
"Microsoft.NetCore.Component.SDK",
"Microsoft.VisualStudio.Component.AppInsights.Tools",
"Microsoft.VisualStudio.Component.DiagnosticTools",
"Microsoft.NetCore.Component.Runtime.6.0",
"Microsoft.VisualStudio.ComponentGroup.WindowsAppSDK.Cs",
"Microsoft.ComponentGroup.Blend",
"Microsoft.VisualStudio.Component.Windows10SDK.19041",
"Microsoft.Component.NetFX.Native",
"Microsoft.VisualStudio.Component.Graphics",
"Microsoft.VisualStudio.Component.VC.Tools.ARM64EC",
"Microsoft.VisualStudio.Component.WindowsAppSdkSupport.CSharp",
"Microsoft.VisualStudio.ComponentGroup.WindowsAppDevelopment.Prerequisites",
"Microsoft.VisualStudio.ComponentGroup.UWP.NetCoreAndStandard",
"Microsoft.VisualStudio.Workload.Universal"
],
"extensions": []
Expand Down
43 changes: 21 additions & 22 deletions FluentAutoClicker/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,31 @@
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.

namespace FluentAutoClicker
namespace FluentAutoClicker;

/// <summary>
/// Provides application-specific behavior to supplement the default Application class.
/// </summary>
public partial class App : Application
{
/// <summary>
/// Provides application-specific behavior to supplement the default Application class.
/// Initializes the singleton application object. This is the first line of authored code
/// executed, and as such is the logical equivalent of main() or WinMain().
/// </summary>
public partial class App : Application
public App()
{
/// <summary>
/// Initializes the singleton application object. This is the first line of authored code
/// executed, and as such is the logical equivalent of main() or WinMain().
/// </summary>
public App()
{
this.InitializeComponent();
}

/// <summary>
/// Invoked when the application is launched.
/// </summary>
/// <param name="args">Details about the launch request and process.</param>
protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
{
Window = new MainWindow();
Window.Activate();
}
InitializeComponent();
}

public static Window Window { get; private set; }
/// <summary>
/// Invoked when the application is launched.
/// </summary>
/// <param name="args">Details about the launch request and process.</param>
protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
{
Window = new MainWindow();
Window.Activate();
}

public static Window Window { get; private set; }
}
8 changes: 2 additions & 6 deletions FluentAutoClicker/FluentAutoClicker.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<PublishProfile>win-$(Platform).pubxml</PublishProfile>
<UseWinUI>true</UseWinUI>
<EnableMsixTooling>true</EnableMsixTooling>
<DefineConstants>DISABLE_XAML_GENERATED_MAIN</DefineConstants>
<Authors>Ryan Luu</Authors>
<Copyright>Copyright (C) 2024 Ryan Luu</Copyright>
<ApplicationIcon>Assets\WindowIcon.ico</ApplicationIcon>
Expand Down Expand Up @@ -53,11 +54,6 @@
<WebView2EnableCsWinRTProjection>False</WebView2EnableCsWinRTProjection>
</PropertyGroup>

<!-- Turn off XAML's generated Program code use our single-instanced app code (Program.cs) -->
<PropertyGroup>
<DefineConstants>DISABLE_XAML_GENERATED_MAIN</DefineConstants>
</PropertyGroup>

<!-- Publish Properties -->
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x86'">
<PublishAot>False</PublishAot>
Expand Down Expand Up @@ -89,4 +85,4 @@
<PublishReadyToRun>True</PublishReadyToRun>
<PublishTrimmed>True</PublishTrimmed>
</PropertyGroup>
</Project>
</Project>
15 changes: 9 additions & 6 deletions FluentAutoClicker/Helpers/AutoClicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public static void StopAutoClicker()

private static async void AutoClickerThread(int ClickInterval, int RepeatAmount, int MouseButton, int ClickOffset)
{
var clickCount = 0;
var random = new Random();
int clickCount = 0;
Random random = new();
while (IsAutoClickerRunning)
{
if (clickCount >= RepeatAmount && RepeatAmount != 0)
Expand All @@ -87,7 +87,10 @@ private static async void AutoClickerThread(int ClickInterval, int RepeatAmount,
break;
}

if (RepeatAmount > 0) clickCount++;
if (RepeatAmount > 0)
{
clickCount++;
}

int randomClickOffset = random.Next(0, ClickOffset);
await Task.Delay(ClickInterval + randomClickOffset);
Expand All @@ -96,10 +99,10 @@ private static async void AutoClickerThread(int ClickInterval, int RepeatAmount,

private static void MouseEvent(int dx, int dy, uint dwFlags, uint dwData, uint time, nint dwExtraInfo)
{
var inputs = new Input[2];
Input[] inputs = new Input[2];
inputs[0] = MouseInput(dx, dy, dwData, dwFlags, time, dwExtraInfo);
inputs[1] = MouseInput(dx, dy, dwData, dwFlags, time, dwExtraInfo);
SendInput((uint)inputs.Length, inputs, Marshal.SizeOf<Input>());
_ = SendInput((uint)inputs.Length, inputs, Marshal.SizeOf<Input>());
}

private static Input MouseInput(int dx, int dy, uint mouseData, uint dwFlags, uint time, nint dwExtraInfo)
Expand Down Expand Up @@ -147,4 +150,4 @@ private enum MouseEventF : uint
MiddleDown = 0x0020,
MiddleUp = 0x0040
}
}
}
61 changes: 47 additions & 14 deletions FluentAutoClicker/Helpers/WindowMessageHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,44 @@ public WindowMessageHook(Window window) : this(GetHandle(window)) { }
public WindowMessageHook(nint hWnd)
{
if (hWnd == 0)
{
throw new ArgumentException(null, nameof(hWnd));
}

_hWnd = hWnd;
_hooks.AddOrUpdate(hWnd, this, (k, o) =>
_ = _hooks.AddOrUpdate(hWnd, this, (k, o) =>
{
if (Equals(o)) return o;
if (Equals(o))
{
return o;
}

o.Dispose();
return this;
});
if (!SetWindowSubclass(hWnd, _proc, 0, 0))
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}

protected virtual void OnMessage(object sender, MessageEventArgs e)
{
Message?.Invoke(sender, e);
}

protected virtual void OnMessage(object sender, MessageEventArgs e) => Message?.Invoke(sender, e);
protected virtual void Dispose(bool disposing)
{
if (!disposing) return;
var hWnd = Interlocked.Exchange(ref _hWnd, IntPtr.Zero);
if (!disposing)
{
return;
}

nint hWnd = Interlocked.Exchange(ref _hWnd, IntPtr.Zero);
if (hWnd != IntPtr.Zero)
{
RemoveWindowSubclass(hWnd, _proc, 0);
_hooks.Remove(hWnd, out _);
_ = RemoveWindowSubclass(hWnd, _proc, 0);
_ = _hooks.Remove(hWnd, out _);
}
}

Expand All @@ -84,20 +100,37 @@ private static nint GetHandle(Window window)

private static nint SubclassProc(nint hWnd, uint uMsg, nint wParam, nint lParam, nint uIdSubclass, uint dwRefData)
{
if (_hooks.TryGetValue(hWnd, out var hook))
if (_hooks.TryGetValue(hWnd, out WindowMessageHook hook))
{
var e = new MessageEventArgs(hWnd, uMsg, wParam, lParam);
MessageEventArgs e = new(hWnd, uMsg, wParam, lParam);
hook.OnMessage(hook, e);
if (e.Result.HasValue)
{
return e.Result.Value;
}
}
return DefSubclassProc(hWnd, uMsg, wParam, lParam);
}

public override int GetHashCode() => _hWnd.GetHashCode();
public override string ToString() => _hWnd.ToString();
public override bool Equals(object obj) => Equals(obj as WindowMessageHook);
public virtual bool Equals(WindowMessageHook other) => other != null && _hWnd.Equals(other._hWnd);
public override int GetHashCode()
{
return _hWnd.GetHashCode();
}

public override string ToString()
{
return _hWnd.ToString();
}

public override bool Equals(object obj)
{
return Equals(obj as WindowMessageHook);
}

public virtual bool Equals(WindowMessageHook other)
{
return other != null && _hWnd.Equals(other._hWnd);
}
}

public class MessageEventArgs : EventArgs
Expand All @@ -115,4 +148,4 @@ public MessageEventArgs(nint hWnd, uint uMsg, nint wParam, nint lParam)
public nint WParam { get; }
public nint LParam { get; }
public virtual nint? Result { get; set; }
}
}
Loading

0 comments on commit b96a72f

Please sign in to comment.