X-Aim – Diff between revs 2 and 3

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 2 Rev 3
Line 6... Line 6...
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;
Line 11... Line 13...
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 =
Line 17... Line 19...
17 $"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}/img/activeCrosshair.png"; 19 $"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}/img/activeCrosshair.png";
Line 18... Line 20...
18   20  
Line 19... Line 21...
19 private Point firstPoint; 21 private Point _firstPoint;
Line 20... Line 22...
20   22  
21 private bool lockedPosition; 23 private bool _lockedPosition;
22   24  
-   25 private bool _mouseIsDown;
-   26  
-   27 public Form1()
-   28 {
-   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();
23 private bool mouseIsDown; 47 break;
Line 24... Line 48...
24   48 default:
Line 25... Line 49...
25 public Form1() 49 RemoveApplicationFromStartup();
Line 37... Line 61...
37 return; 61 return;
38 } 62 }
Line 39... Line 63...
39   63  
Line 40... Line 64...
40 BackgroundImage = draggedImage; 64 BackgroundImage = draggedImage;
41   65  
Line 42... Line 66...
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)
Line 46... Line 70...
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)
Line 51... Line 75...
51 { 75 {
52 firstPoint = e.Location; 76 _firstPoint = e.Location;
53 mouseIsDown = true; 77 _mouseIsDown = true;
Line 54... Line 78...
54 } 78 }
55   79  
56 private void OnMouseUp(object sender, MouseEventArgs e) 80 private void OnMouseUp(object sender, MouseEventArgs e)
Line 57... Line 81...
57 { 81 {
Line 58... Line 82...
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;
Line 63... Line 87...
63   87  
Line 64... Line 88...
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 {
Line 70... Line 94...
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);
Line 79... Line 103...
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 {
Line 83... Line 107...
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 });
Line 89... Line 113...
89 } 113 }
90   114  
91 Invoke((MethodInvoker)delegate { BackgroundImage = originalImage; }); 115 Invoke((MethodInvoker) delegate { BackgroundImage = originalImage; });
Line 92... Line 116...
92 }); 116 });
93 } 117 }
94 } 118 }
95   119  
Line 123... Line 147...
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);
Line 129... Line 153...
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 {
Line 136... Line 160...
136 FileInfo fileInfo = new FileInfo(ACTIVE_CROSSHAIR_FILE_PATH); 160 var fileInfo = new FileInfo(_activeCrosshairFilePath);
-   161  
137   162 if (!fileInfo.Exists)
-   163 {
Line 138... Line 164...
138 if (!fileInfo.Exists) 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);
Line -... Line 170...
-   170 }
-   171 }
144 } 172 }
-   173  
-   174 using (var fileStream = new FileStream(_activeCrosshairFilePath, FileMode.Open))
-   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();
145 } 209 break;
Line 146... Line 210...
146 } 210 }
Line 147... Line 211...
147   211  
Line 165... Line 229...
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
Line 170... Line 234...
170 var matrix = new ColorMatrix(); 234 var matrix = new ColorMatrix {Matrix33 = opacity};
171   -  
Line 172... Line 235...
172 //set the opacity 235  
173 matrix.Matrix33 = opacity; 236 //set the opacity
Line 174... Line 237...
174   237  
Line 190... Line 253...
190 MessageBox.Show(ex.Message); 253 MessageBox.Show(ex.Message);
191 return null; 254 return null;
192 } 255 }
193 } 256 }
Line 194... Line -...
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))
Line 209... Line 264...
209 return false; 264 return false;
210 } 265 }
Line 211... Line 266...
211   266  
212 try 267 try
-   268 {
-   269 using (var fileStream = new FileStream(file, FileMode.Open))
213 { 270 {
-   271 image = Image.FromStream(fileStream);
Line 214... Line 272...
214 image = LoadImageFromFile(file); 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;
-   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);
222 } 296 }
223 } 297 }
224 } 298 }