X-Aim – Rev 2
?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;
namespace X_Aim
{
public partial class Form1 : Form
{
private readonly string ACTIVE_CROSSHAIR_FILE_PATH =
$"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}/img/activeCrosshair.png";
private Point firstPoint;
private bool lockedPosition;
private bool mouseIsDown;
public Form1()
{
InitializeComponent();
}
#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(ACTIVE_CROSSHAIR_FILE_PATH, 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.SW_SHOWNORMAL);
// If the default crosshair cannot be found, unpack from resources and load the crosshair.
if (!File.Exists(ACTIVE_CROSSHAIR_FILE_PATH))
{
using (var manifestResourceStream =
Assembly.GetExecutingAssembly().GetManifestResourceStream("X_Aim.img.defaultCrosshair.png"))
{
FileInfo fileInfo = new FileInfo(ACTIVE_CROSSHAIR_FILE_PATH);
if (!fileInfo.Exists)
Directory.CreateDirectory(fileInfo.Directory.FullName);
using (var fileStream = new FileStream(ACTIVE_CROSSHAIR_FILE_PATH, FileMode.Create))
{
await manifestResourceStream.CopyToAsync(fileStream);
}
}
}
BackgroundImage = LoadImageFromFile(ACTIVE_CROSSHAIR_FILE_PATH);
}
#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();
//set the opacity
matrix.Matrix33 = 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 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();
if (string.IsNullOrEmpty(file))
{
image = null;
return false;
}
try
{
image = LoadImageFromFile(file);
return true;
}
catch
{
image = null;
return false;
}
}
}
}
Generated by GNU Enscript 1.6.5.90.