X-Aim

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 2  →  ?path2? @ 3
/trunk/X-Aim/Form1.cs
@@ -8,23 +8,47 @@
using System.Reflection;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;
using X_Aim.Properties;
 
namespace X_Aim
{
public partial class Form1 : Form
{
private readonly string ACTIVE_CROSSHAIR_FILE_PATH =
private readonly string _activeCrosshairFilePath =
$"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}/img/activeCrosshair.png";
 
private Point firstPoint;
private Point _firstPoint;
 
private bool lockedPosition;
private bool _lockedPosition;
 
private bool mouseIsDown;
private bool _mouseIsDown;
 
public Form1()
{
InitializeComponent();
 
// Show or hide the form.
switch (Settings.Default["Enable"])
{
case true:
Show();
break;
default:
Hide();
break;
}
 
// Set the application to start with windows.
switch (Settings.Default["StartWithWindows"])
{
case true:
AddApplicationToStartup();
break;
default:
RemoveApplicationFromStartup();
break;
}
}
 
#region Event Handlers
@@ -39,7 +63,7 @@
 
BackgroundImage = draggedImage;
 
draggedImage.Save(ACTIVE_CROSSHAIR_FILE_PATH, ImageFormat.Png);
draggedImage.Save(_activeCrosshairFilePath, ImageFormat.Png);
}
 
private void OnDragEnter(object sender, DragEventArgs e)
@@ -49,27 +73,27 @@
 
private void OnMouseDown(object sender, MouseEventArgs e)
{
firstPoint = e.Location;
mouseIsDown = true;
_firstPoint = e.Location;
_mouseIsDown = true;
}
 
private void OnMouseUp(object sender, MouseEventArgs e)
{
mouseIsDown = false;
_mouseIsDown = false;
 
if (e.Button == MouseButtons.Right)
{
lockedPosition = !lockedPosition;
_lockedPosition = !_lockedPosition;
 
var resourceImage = lockedPosition ? "X_Aim.img.locked.png" : "X_Aim.img.unlocked.png";
var resourceImage = _lockedPosition ? "X_Aim.img.locked.png" : "X_Aim.img.unlocked.png";
 
#pragma warning disable 4014
#pragma warning disable 4014
Task.Run(() =>
#pragma warning restore 4014
{
Image originalImage = null;
 
Invoke((MethodInvoker)delegate { originalImage = BackgroundImage; });
Invoke((MethodInvoker) delegate { originalImage = BackgroundImage; });
 
Image lockImage;
using (var manifestResourceStream =
@@ -80,15 +104,15 @@
 
foreach (var i in Enumerable.Range(0, 100).Reverse())
{
Invoke((MethodInvoker)delegate
Invoke((MethodInvoker) delegate
{
BackgroundImage = SetImageOpacity(lockImage, (float)i / 100);
BackgroundImage = SetImageOpacity(lockImage, (float) i / 100);
Invalidate();
Refresh();
});
}
 
Invoke((MethodInvoker)delegate { BackgroundImage = originalImage; });
Invoke((MethodInvoker) delegate { BackgroundImage = originalImage; });
});
}
}
@@ -95,14 +119,14 @@
 
private void OnMouseMove(object sender, MouseEventArgs e)
{
if (!mouseIsDown || lockedPosition)
if (!_mouseIsDown || _lockedPosition)
{
return;
}
 
// Get the difference between the two points
var xDiff = firstPoint.X - e.Location.X;
var yDiff = firstPoint.Y - e.Location.Y;
var xDiff = _firstPoint.X - e.Location.X;
var yDiff = _firstPoint.Y - e.Location.Y;
 
// Set the new point
var x = Location.X - xDiff;
@@ -125,20 +149,22 @@
// Set window to top-most.
Natives.AllowSetForegroundWindow((uint) Process.GetCurrentProcess().Id);
Natives.SetForegroundWindow(Handle);
Natives.ShowWindow(Handle, Natives.SW_SHOWNORMAL);
Natives.ShowWindow(Handle, Natives.SwShownormal);
 
// If the default crosshair cannot be found, unpack from resources and load the crosshair.
if (!File.Exists(ACTIVE_CROSSHAIR_FILE_PATH))
if (!File.Exists(_activeCrosshairFilePath))
{
using (var manifestResourceStream =
Assembly.GetExecutingAssembly().GetManifestResourceStream("X_Aim.img.defaultCrosshair.png"))
{
FileInfo fileInfo = new FileInfo(ACTIVE_CROSSHAIR_FILE_PATH);
var fileInfo = new FileInfo(_activeCrosshairFilePath);
 
if (!fileInfo.Exists)
{
Directory.CreateDirectory(fileInfo.Directory.FullName);
}
 
using (var fileStream = new FileStream(ACTIVE_CROSSHAIR_FILE_PATH, FileMode.Create))
using (var fileStream = new FileStream(_activeCrosshairFilePath, FileMode.Create))
{
await manifestResourceStream.CopyToAsync(fileStream);
}
@@ -145,9 +171,47 @@
}
}
 
BackgroundImage = LoadImageFromFile(ACTIVE_CROSSHAIR_FILE_PATH);
using (var fileStream = new FileStream(_activeCrosshairFilePath, FileMode.Open))
{
BackgroundImage = Image.FromStream(fileStream);
}
}
 
private void OnFormClosing(object sender, FormClosingEventArgs e)
{
Settings.Default.Save();
}
 
private void Enable_OnCheckStateChanged(object sender, EventArgs e)
{
switch (Settings.Default["Enable"])
{
case true:
Show();
break;
default:
Hide();
break;
}
 
Settings.Default.Save();
}
 
private void StartWithWindows_OnCheckStateChanged(object sender, EventArgs e)
{
switch (Settings.Default["StartWithWindows"])
{
case true:
AddApplicationToStartup();
break;
default:
RemoveApplicationFromStartup();
break;
}
 
Settings.Default.Save();
}
 
#endregion
 
/// <summary>
@@ -167,10 +231,9 @@
using (var gfx = Graphics.FromImage(bmp))
{
//create a color matrix object
var matrix = new ColorMatrix();
var matrix = new ColorMatrix {Matrix33 = opacity};
 
//set the opacity
matrix.Matrix33 = opacity;
 
//create image attributes
var attributes = new ImageAttributes();
@@ -192,14 +255,6 @@
}
}
 
private Image LoadImageFromFile(string file)
{
using (var fileStream = new FileStream(file, FileMode.Open))
{
return Image.FromStream(fileStream);
}
}
 
private bool TryGetDragDropImage(DragEventArgs e, out Image image)
{
var file = ((IEnumerable<string>) e.Data.GetData(DataFormats.FileDrop)).FirstOrDefault();
@@ -211,7 +266,10 @@
 
try
{
image = LoadImageFromFile(file);
using (var fileStream = new FileStream(file, FileMode.Open))
{
image = Image.FromStream(fileStream);
}
 
return true;
}
@@ -221,5 +279,21 @@
return false;
}
}
 
public static void AddApplicationToStartup()
{
using (var key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
{
key?.SetValue(Assembly.GetExecutingAssembly().FullName, "\"" + Application.ExecutablePath + "\"");
}
}
 
public static void RemoveApplicationFromStartup()
{
using (var key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
{
key?.DeleteValue(Assembly.GetExecutingAssembly().FullName, false);
}
}
}
}