X-Aim – Blame information for rev 3

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;
3 office 11 using Microsoft.Win32;
12 using X_Aim.Properties;
1 office 13  
14 namespace X_Aim
15 {
16 public partial class Form1 : Form
17 {
3 office 18 private readonly string _activeCrosshairFilePath =
1 office 19 $"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}/img/activeCrosshair.png";
20  
3 office 21 private Point _firstPoint;
1 office 22  
3 office 23 private bool _lockedPosition;
1 office 24  
3 office 25 private bool _mouseIsDown;
1 office 26  
27 public Form1()
28 {
29 InitializeComponent();
3 office 30  
31 // Show or hide the form.
32 switch (Settings.Default["Enable"])
33 {
34 case true:
35 Show();
36 break;
37 default:
38 Hide();
39 break;
40 }
41  
42 // Set the application to start with windows.
43 switch (Settings.Default["StartWithWindows"])
44 {
45 case true:
46 AddApplicationToStartup();
47 break;
48 default:
49 RemoveApplicationFromStartup();
50 break;
51 }
1 office 52 }
53  
54 #region Event Handlers
55  
56 private void OnDragDrop(object sender, DragEventArgs e)
57 {
58 if (!TryGetDragDropImage(e, out var draggedImage))
59 {
60 e.Effect = DragDropEffects.None;
61 return;
62 }
63  
64 BackgroundImage = draggedImage;
65  
3 office 66 draggedImage.Save(_activeCrosshairFilePath, ImageFormat.Png);
1 office 67 }
68  
69 private void OnDragEnter(object sender, DragEventArgs e)
70 {
71 e.Effect = DragDropEffects.Copy;
72 }
73  
74 private void OnMouseDown(object sender, MouseEventArgs e)
75 {
3 office 76 _firstPoint = e.Location;
77 _mouseIsDown = true;
1 office 78 }
79  
80 private void OnMouseUp(object sender, MouseEventArgs e)
81 {
3 office 82 _mouseIsDown = false;
1 office 83  
84 if (e.Button == MouseButtons.Right)
85 {
3 office 86 _lockedPosition = !_lockedPosition;
1 office 87  
3 office 88 var resourceImage = _lockedPosition ? "X_Aim.img.locked.png" : "X_Aim.img.unlocked.png";
1 office 89  
3 office 90 #pragma warning disable 4014
1 office 91 Task.Run(() =>
92 #pragma warning restore 4014
93 {
94 Image originalImage = null;
95  
3 office 96 Invoke((MethodInvoker) delegate { originalImage = BackgroundImage; });
1 office 97  
98 Image lockImage;
99 using (var manifestResourceStream =
100 Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceImage))
101 {
102 lockImage = Image.FromStream(manifestResourceStream);
103 }
104  
105 foreach (var i in Enumerable.Range(0, 100).Reverse())
106 {
3 office 107 Invoke((MethodInvoker) delegate
1 office 108 {
3 office 109 BackgroundImage = SetImageOpacity(lockImage, (float) i / 100);
1 office 110 Invalidate();
111 Refresh();
112 });
113 }
114  
3 office 115 Invoke((MethodInvoker) delegate { BackgroundImage = originalImage; });
1 office 116 });
117 }
118 }
119  
120 private void OnMouseMove(object sender, MouseEventArgs e)
121 {
3 office 122 if (!_mouseIsDown || _lockedPosition)
1 office 123 {
124 return;
125 }
126  
127 // Get the difference between the two points
3 office 128 var xDiff = _firstPoint.X - e.Location.X;
129 var yDiff = _firstPoint.Y - e.Location.Y;
1 office 130  
131 // Set the new point
132 var x = Location.X - xDiff;
133 var y = Location.Y - yDiff;
134 Location = new Point(x, y);
135 }
136  
137 private void quitToolStripMenuItem_Click(object sender, EventArgs e)
138 {
139 Application.Exit();
140 }
141  
142 private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
143 {
144 new About().Show();
145 }
146  
147 private async void OnLoad(object sender, EventArgs e)
148 {
149 // Set window to top-most.
150 Natives.AllowSetForegroundWindow((uint) Process.GetCurrentProcess().Id);
151 Natives.SetForegroundWindow(Handle);
3 office 152 Natives.ShowWindow(Handle, Natives.SwShownormal);
1 office 153  
154 // If the default crosshair cannot be found, unpack from resources and load the crosshair.
3 office 155 if (!File.Exists(_activeCrosshairFilePath))
1 office 156 {
157 using (var manifestResourceStream =
158 Assembly.GetExecutingAssembly().GetManifestResourceStream("X_Aim.img.defaultCrosshair.png"))
159 {
3 office 160 var fileInfo = new FileInfo(_activeCrosshairFilePath);
2 office 161  
162 if (!fileInfo.Exists)
3 office 163 {
2 office 164 Directory.CreateDirectory(fileInfo.Directory.FullName);
3 office 165 }
2 office 166  
3 office 167 using (var fileStream = new FileStream(_activeCrosshairFilePath, FileMode.Create))
1 office 168 {
169 await manifestResourceStream.CopyToAsync(fileStream);
170 }
171 }
172 }
173  
3 office 174 using (var fileStream = new FileStream(_activeCrosshairFilePath, FileMode.Open))
175 {
176 BackgroundImage = Image.FromStream(fileStream);
177 }
1 office 178 }
179  
3 office 180 private void OnFormClosing(object sender, FormClosingEventArgs e)
181 {
182 Settings.Default.Save();
183 }
184  
185 private void Enable_OnCheckStateChanged(object sender, EventArgs e)
186 {
187 switch (Settings.Default["Enable"])
188 {
189 case true:
190 Show();
191 break;
192 default:
193 Hide();
194 break;
195 }
196  
197 Settings.Default.Save();
198 }
199  
200 private void StartWithWindows_OnCheckStateChanged(object sender, EventArgs e)
201 {
202 switch (Settings.Default["StartWithWindows"])
203 {
204 case true:
205 AddApplicationToStartup();
206 break;
207 default:
208 RemoveApplicationFromStartup();
209 break;
210 }
211  
212 Settings.Default.Save();
213 }
214  
1 office 215 #endregion
216  
217 /// <summary>
218 /// method for changing the opacity of an image
219 /// </summary>
220 /// <param name="image">image to set opacity on</param>
221 /// <param name="opacity">percentage of opacity</param>
222 /// <returns></returns>
223 public Image SetImageOpacity(Image image, float opacity)
224 {
225 try
226 {
227 //create a Bitmap the size of the image provided
228 var bmp = new Bitmap(image.Width, image.Height);
229  
230 //create a graphics object from the image
231 using (var gfx = Graphics.FromImage(bmp))
232 {
233 //create a color matrix object
3 office 234 var matrix = new ColorMatrix {Matrix33 = opacity};
1 office 235  
236 //set the opacity
237  
238 //create image attributes
239 var attributes = new ImageAttributes();
240  
241 //set the color(opacity) of the image
242 attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
243  
244 //now draw the image
245 gfx.DrawImage(image, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, image.Width, image.Height,
246 GraphicsUnit.Pixel, attributes);
247 }
248  
249 return bmp;
250 }
251 catch (Exception ex)
252 {
253 MessageBox.Show(ex.Message);
254 return null;
255 }
256 }
257  
258 private bool TryGetDragDropImage(DragEventArgs e, out Image image)
259 {
260 var file = ((IEnumerable<string>) e.Data.GetData(DataFormats.FileDrop)).FirstOrDefault();
261 if (string.IsNullOrEmpty(file))
262 {
263 image = null;
264 return false;
265 }
266  
267 try
268 {
3 office 269 using (var fileStream = new FileStream(file, FileMode.Open))
270 {
271 image = Image.FromStream(fileStream);
272 }
1 office 273  
274 return true;
275 }
276 catch
277 {
278 image = null;
279 return false;
280 }
281 }
3 office 282  
283 public static void AddApplicationToStartup()
284 {
285 using (var key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
286 {
287 key?.SetValue(Assembly.GetExecutingAssembly().FullName, "\"" + Application.ExecutablePath + "\"");
288 }
289 }
290  
291 public static void RemoveApplicationFromStartup()
292 {
293 using (var key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
294 {
295 key?.DeleteValue(Assembly.GetExecutingAssembly().FullName, false);
296 }
297 }
1 office 298 }
299 }