X-Aim – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 using System;
2 using System.Collections.Generic;
3 using System.Diagnostics;
4 using System.Drawing;
5 using System.Drawing.Imaging;
6 using System.IO;
7 using System.Linq;
8 using System.Reflection;
9 using System.Threading.Tasks;
10 using System.Windows.Forms;
11  
12 namespace X_Aim
13 {
14 public partial class Form1 : Form
15 {
16 private readonly string ACTIVE_CROSSHAIR_FILE_PATH =
17 $"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}/img/activeCrosshair.png";
18  
19 private Point firstPoint;
20  
21 private bool lockedPosition;
22  
23 private bool mouseIsDown;
24  
25 public Form1()
26 {
27 InitializeComponent();
28 }
29  
30 #region Event Handlers
31  
32 private void OnDragDrop(object sender, DragEventArgs e)
33 {
34 if (!TryGetDragDropImage(e, out var draggedImage))
35 {
36 e.Effect = DragDropEffects.None;
37 return;
38 }
39  
40 BackgroundImage = draggedImage;
41  
42 draggedImage.Save(ACTIVE_CROSSHAIR_FILE_PATH, ImageFormat.Png);
43 }
44  
45 private void OnDragEnter(object sender, DragEventArgs e)
46 {
47 e.Effect = DragDropEffects.Copy;
48 }
49  
50 private void OnMouseDown(object sender, MouseEventArgs e)
51 {
52 firstPoint = e.Location;
53 mouseIsDown = true;
54 }
55  
56 private void OnMouseUp(object sender, MouseEventArgs e)
57 {
58 mouseIsDown = false;
59  
60 if (e.Button == MouseButtons.Right)
61 {
62 lockedPosition = !lockedPosition;
63  
64 var resourceImage = lockedPosition ? "X_Aim.img.locked.png" : "X_Aim.img.unlocked.png";
65  
66 #pragma warning disable 4014
67 Task.Run(() =>
68 #pragma warning restore 4014
69 {
70 Image originalImage = null;
71  
72 Invoke((MethodInvoker)delegate { originalImage = BackgroundImage; });
73  
74 Image lockImage;
75 using (var manifestResourceStream =
76 Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceImage))
77 {
78 lockImage = Image.FromStream(manifestResourceStream);
79 }
80  
81 foreach (var i in Enumerable.Range(0, 100).Reverse())
82 {
83 Invoke((MethodInvoker)delegate
84 {
85 BackgroundImage = SetImageOpacity(lockImage, (float)i / 100);
86 Invalidate();
87 Refresh();
88 });
89 }
90  
91 Invoke((MethodInvoker)delegate { BackgroundImage = originalImage; });
92 });
93 }
94 }
95  
96 private void OnMouseMove(object sender, MouseEventArgs e)
97 {
98 if (!mouseIsDown || lockedPosition)
99 {
100 return;
101 }
102  
103 // Get the difference between the two points
104 var xDiff = firstPoint.X - e.Location.X;
105 var yDiff = firstPoint.Y - e.Location.Y;
106  
107 // Set the new point
108 var x = Location.X - xDiff;
109 var y = Location.Y - yDiff;
110 Location = new Point(x, y);
111 }
112  
113 private void quitToolStripMenuItem_Click(object sender, EventArgs e)
114 {
115 Application.Exit();
116 }
117  
118 private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
119 {
120 new About().Show();
121 }
122  
123 private async void OnLoad(object sender, EventArgs e)
124 {
125 // Set window to top-most.
126 Natives.AllowSetForegroundWindow((uint) Process.GetCurrentProcess().Id);
127 Natives.SetForegroundWindow(Handle);
128 Natives.ShowWindow(Handle, Natives.SW_SHOWNORMAL);
129  
130 // If the default crosshair cannot be found, unpack from resources and load the crosshair.
131 if (!File.Exists(ACTIVE_CROSSHAIR_FILE_PATH))
132 {
133 using (var manifestResourceStream =
134 Assembly.GetExecutingAssembly().GetManifestResourceStream("X_Aim.img.defaultCrosshair.png"))
135 {
136 using (var fileStream = new FileStream(ACTIVE_CROSSHAIR_FILE_PATH, FileMode.Create))
137 {
138 await manifestResourceStream.CopyToAsync(fileStream);
139 }
140 }
141 }
142  
143 BackgroundImage = LoadImageFromFile(ACTIVE_CROSSHAIR_FILE_PATH);
144 }
145  
146 #endregion
147  
148 /// <summary>
149 /// method for changing the opacity of an image
150 /// </summary>
151 /// <param name="image">image to set opacity on</param>
152 /// <param name="opacity">percentage of opacity</param>
153 /// <returns></returns>
154 public Image SetImageOpacity(Image image, float opacity)
155 {
156 try
157 {
158 //create a Bitmap the size of the image provided
159 var bmp = new Bitmap(image.Width, image.Height);
160  
161 //create a graphics object from the image
162 using (var gfx = Graphics.FromImage(bmp))
163 {
164 //create a color matrix object
165 var matrix = new ColorMatrix();
166  
167 //set the opacity
168 matrix.Matrix33 = opacity;
169  
170 //create image attributes
171 var attributes = new ImageAttributes();
172  
173 //set the color(opacity) of the image
174 attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
175  
176 //now draw the image
177 gfx.DrawImage(image, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, image.Width, image.Height,
178 GraphicsUnit.Pixel, attributes);
179 }
180  
181 return bmp;
182 }
183 catch (Exception ex)
184 {
185 MessageBox.Show(ex.Message);
186 return null;
187 }
188 }
189  
190 private Image LoadImageFromFile(string file)
191 {
192 using (var fileStream = new FileStream(file, FileMode.Open))
193 {
194 return Image.FromStream(fileStream);
195 }
196 }
197  
198 private bool TryGetDragDropImage(DragEventArgs e, out Image image)
199 {
200 var file = ((IEnumerable<string>) e.Data.GetData(DataFormats.FileDrop)).FirstOrDefault();
201 if (string.IsNullOrEmpty(file))
202 {
203 image = null;
204 return false;
205 }
206  
207 try
208 {
209 image = LoadImageFromFile(file);
210  
211 return true;
212 }
213 catch
214 {
215 image = null;
216 return false;
217 }
218 }
219 }
220 }