X-Aim – Rev 3
?pathlinks?
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
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 _activeCrosshairFilePath =
$"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}/img/activeCrosshair.png";
private Point _firstPoint;
private bool _lockedPosition;
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
private void OnDragDrop(object sender, DragEventArgs e)
{
if (!TryGetDragDropImage(e, out var draggedImage))
{
e.Effect = DragDropEffects.None;
return;
}
BackgroundImage = draggedImage;
draggedImage.Save(_activeCrosshairFilePath, ImageFormat.Png);
}
private void OnDragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
private void OnMouseDown(object sender, MouseEventArgs e)
{
_firstPoint = e.Location;
_mouseIsDown = true;
}
private void OnMouseUp(object sender, MouseEventArgs e)
{
_mouseIsDown = false;
if (e.Button == MouseButtons.Right)
{
_lockedPosition = !_lockedPosition;
var resourceImage = _lockedPosition ? "X_Aim.img.locked.png" : "X_Aim.img.unlocked.png";
#pragma warning disable 4014
Task.Run(() =>
#pragma warning restore 4014
{
Image originalImage = null;
Invoke((MethodInvoker) delegate { originalImage = BackgroundImage; });
Image lockImage;
using (var manifestResourceStream =
Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceImage))
{
lockImage = Image.FromStream(manifestResourceStream);
}
foreach (var i in Enumerable.Range(0, 100).Reverse())
{
Invoke((MethodInvoker) delegate
{
BackgroundImage = SetImageOpacity(lockImage, (float) i / 100);
Invalidate();
Refresh();
});
}
Invoke((MethodInvoker) delegate { BackgroundImage = originalImage; });
});
}
}
private void OnMouseMove(object sender, MouseEventArgs e)
{
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;
// Set the new point
var x = Location.X - xDiff;
var y = Location.Y - yDiff;
Location = new Point(x, y);
}
private void quitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
new About().Show();
}
private async void OnLoad(object sender, EventArgs e)
{
// Set window to top-most.
Natives.AllowSetForegroundWindow((uint) Process.GetCurrentProcess().Id);
Natives.SetForegroundWindow(Handle);
Natives.ShowWindow(Handle, Natives.SwShownormal);
// If the default crosshair cannot be found, unpack from resources and load the crosshair.
if (!File.Exists(_activeCrosshairFilePath))
{
using (var manifestResourceStream =
Assembly.GetExecutingAssembly().GetManifestResourceStream("X_Aim.img.defaultCrosshair.png"))
{
var fileInfo = new FileInfo(_activeCrosshairFilePath);
if (!fileInfo.Exists)
{
Directory.CreateDirectory(fileInfo.Directory.FullName);
}
using (var fileStream = new FileStream(_activeCrosshairFilePath, FileMode.Create))
{
await manifestResourceStream.CopyToAsync(fileStream);
}
}
}
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>
/// method for changing the opacity of an image
/// </summary>
/// <param name="image">image to set opacity on</param>
/// <param name="opacity">percentage of opacity</param>
/// <returns></returns>
public Image SetImageOpacity(Image image, float opacity)
{
try
{
//create a Bitmap the size of the image provided
var bmp = new Bitmap(image.Width, image.Height);
//create a graphics object from the image
using (var gfx = Graphics.FromImage(bmp))
{
//create a color matrix object
var matrix = new ColorMatrix {Matrix33 = opacity};
//set the opacity
//create image attributes
var attributes = new ImageAttributes();
//set the color(opacity) of the image
attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
//now draw the image
gfx.DrawImage(image, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, image.Width, image.Height,
GraphicsUnit.Pixel, attributes);
}
return bmp;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return null;
}
}
private bool TryGetDragDropImage(DragEventArgs e, out Image image)
{
var file = ((IEnumerable<string>) e.Data.GetData(DataFormats.FileDrop)).FirstOrDefault();
if (string.IsNullOrEmpty(file))
{
image = null;
return false;
}
try
{
using (var fileStream = new FileStream(file, FileMode.Open))
{
image = Image.FromStream(fileStream);
}
return true;
}
catch
{
image = null;
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);
}
}
}
}
Generated by GNU Enscript 1.6.5.90.