Skip to content

Commit

Permalink
Merge pull request #5 from smith-j-travis/multi-instance
Browse files Browse the repository at this point in the history
Multi instance
  • Loading branch information
smith-j-travis authored Jan 17, 2019
2 parents 463acf4 + 59dc071 commit 54fbec7
Show file tree
Hide file tree
Showing 13 changed files with 413 additions and 45 deletions.
Binary file modified AutoClicker.exe
Binary file not shown.
14 changes: 14 additions & 0 deletions AutoClicker/AutoClicker.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,31 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Controls\ValueCheckBox.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Extensions.cs" />
<Compile Include="Main.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Main.Designer.cs">
<DependentUpon>Main.cs</DependentUpon>
</Compile>
<Compile Include="MultipleInstances.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="MultipleInstances.Designer.cs">
<DependentUpon>MultipleInstances.cs</DependentUpon>
</Compile>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Win32Api.cs" />
<EmbeddedResource Include="Main.resx">
<DependentUpon>Main.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="MultipleInstances.resx">
<DependentUpon>MultipleInstances.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
Expand Down
9 changes: 9 additions & 0 deletions AutoClicker/Controls/ValueCheckBox.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Windows.Forms;

namespace AutoClicker.Controls
{
public class ValueCheckBox : CheckBox
{
public string Value { get; set; }
}
}
25 changes: 25 additions & 0 deletions AutoClicker/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace AutoClicker
{
public static class Extensions
{
public static IEnumerable<T> AllControls<T>(this Control startingPoint) where T : Control
{
var hit = startingPoint is T;

if (hit)
yield return (T) startingPoint;

foreach (var child in startingPoint.Controls.Cast<Control>())
{
foreach (var item in AllControls<T>(child))
{
yield return item;
}
}
}
}
}
4 changes: 2 additions & 2 deletions AutoClicker/Main.Designer.cs

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

68 changes: 34 additions & 34 deletions AutoClicker/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,64 +3,64 @@
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;

namespace AutoClicker
{
public partial class Main : Form
{
[DllImport("user32.dll")]
public static extern IntPtr PostMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);

[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);

[DllImport("user32.dll")]
internal static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); //ShowWindow needs an IntPtr

private const int WmLbuttonDown = 0x201;
private const int WmRbuttonDown = 0x0204;

private bool _stop;

public Main()
{
InitializeComponent();
}


private void btn_action_Click(object sender, EventArgs e)
private void Btn_action_Click(object sender, EventArgs e)
{
var mcProcess = Process.GetProcessesByName("javaw").FirstOrDefault();
var mcProcesses = Process.GetProcessesByName("javaw").Where(b => b.MainWindowTitle.Contains("Minecraft")).ToList();

var mainHandle = this.Handle;
int delay;

if (!int.TryParse(this.txtDelay.Text, out delay))
if (!int.TryParse(this.txtDelay.Text, out int delay))
{
MessageBox.Show(@"The delay must be an integer!", @"Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBox.Show(@"The delay must be an integer! Resetting to default.", @"Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.txtDelay.Text = @"300";
return;
}

if (mcProcess == null || !mcProcess.MainWindowTitle.Contains("Minecraft"))
if (!mcProcesses.Any())
{
MessageBox.Show(@"Minecraft not running!", @"Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}

var buttonCode = rdio_RightClick.Checked ? WmRbuttonDown : WmLbuttonDown;
if (mcProcesses.Count > 1)
{
var instancesForm = new MultipleInstances(mcProcesses);

if (instancesForm.ShowDialog() != DialogResult.OK)
return;

mcProcesses = instancesForm.SelectedInstances.Select(Process.GetProcessById).ToList();
}

var buttonCode = rdio_RightClick.Checked ? Win32Api.WmRbuttonDown : Win32Api.WmLbuttonDown;

this._stop = false;
this.lblstart_time.Text = DateTime.Now.ToString("MMMM dd HH:mm tt");

var thread = new BackgroundWorker();
thread.DoWork += delegate { StartClick(mcProcess, mainHandle, (uint)buttonCode, delay, this.chkHold.Checked); };
thread.RunWorkerAsync();
foreach (var mcProcess in mcProcesses)
{
var thread = new BackgroundWorker();
thread.DoWork += delegate { StartClick(mcProcess, mainHandle, (uint)buttonCode, delay, this.chkHold.Checked); };
thread.RunWorkerAsync();

Thread.Sleep(200);
FocusToggle(mcProcess.MainWindowHandle);
FocusToggle(this.Handle);
Thread.Sleep(200);
FocusToggle(mcProcess.MainWindowHandle);
FocusToggle(this.Handle);
}
}

private void StartClick(Process mcProcess, IntPtr mainWindowHandle, uint buttonCode, int delay, bool miningMode)
Expand All @@ -86,7 +86,7 @@ private void StartClick(Process mcProcess, IntPtr mainWindowHandle, uint buttonC

var millisecondsPassed = -1;
if (miningMode)
PostMessage(handle, (uint)buttonCode, (IntPtr)0x0001, IntPtr.Zero); // send left button down
Win32Api.PostMessage(handle, (uint)buttonCode, (IntPtr)0x0001, IntPtr.Zero); // send left button down

while (!this._stop)
{
Expand All @@ -95,8 +95,8 @@ private void StartClick(Process mcProcess, IntPtr mainWindowHandle, uint buttonC
millisecondsPassed = 0;
if (!miningMode)
{
PostMessage(handle, buttonCode, IntPtr.Zero, IntPtr.Zero);
PostMessage(handle, buttonCode + 1, IntPtr.Zero, IntPtr.Zero);
Win32Api.PostMessage(handle, buttonCode, IntPtr.Zero, IntPtr.Zero);
Win32Api.PostMessage(handle, buttonCode + 1, IntPtr.Zero, IntPtr.Zero);
}
}

Expand All @@ -106,14 +106,14 @@ private void StartClick(Process mcProcess, IntPtr mainWindowHandle, uint buttonC
millisecondsPassed += 5;
}

PostMessage(handle, buttonCode, IntPtr.Zero, IntPtr.Zero);
PostMessage(handle, buttonCode + 1, IntPtr.Zero, IntPtr.Zero);
Win32Api.PostMessage(handle, buttonCode, IntPtr.Zero, IntPtr.Zero);
Win32Api.PostMessage(handle, buttonCode + 1, IntPtr.Zero, IntPtr.Zero);

SetControlPropertyThreadSafe(this.btn_start, "Text", @"START!");
SetControlPropertyThreadSafe(this.btn_start, "Enabled", true);
}

private void btn_stop_Click(object sender, EventArgs e)
private void Btn_stop_Click(object sender, EventArgs e)
{
this._stop = true;
this.btn_stop.Enabled = false;
Expand All @@ -122,7 +122,7 @@ private void btn_stop_Click(object sender, EventArgs e)
private static void FocusToggle(IntPtr hwnd)
{
Thread.Sleep(200);
SetForegroundWindow(hwnd);
Win32Api.SetForegroundWindow(hwnd);
}

private delegate void SetControlPropertyThreadSafeDelegate(Control control, string propertyName, object propertyValue);
Expand Down
101 changes: 101 additions & 0 deletions AutoClicker/MultipleInstances.Designer.cs

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

Loading

0 comments on commit 54fbec7

Please sign in to comment.