X-Aim – Diff between revs 2 and 3

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