X-Aim – Blame information for rev 2

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 {
2 office 136 FileInfo fileInfo = new FileInfo(ACTIVE_CROSSHAIR_FILE_PATH);
137  
138 if (!fileInfo.Exists)
139 Directory.CreateDirectory(fileInfo.Directory.FullName);
140  
1 office 141 using (var fileStream = new FileStream(ACTIVE_CROSSHAIR_FILE_PATH, FileMode.Create))
142 {
143 await manifestResourceStream.CopyToAsync(fileStream);
144 }
145 }
146 }
147  
148 BackgroundImage = LoadImageFromFile(ACTIVE_CROSSHAIR_FILE_PATH);
149 }
150  
151 #endregion
152  
153 /// <summary>
154 /// method for changing the opacity of an image
155 /// </summary>
156 /// <param name="image">image to set opacity on</param>
157 /// <param name="opacity">percentage of opacity</param>
158 /// <returns></returns>
159 public Image SetImageOpacity(Image image, float opacity)
160 {
161 try
162 {
163 //create a Bitmap the size of the image provided
164 var bmp = new Bitmap(image.Width, image.Height);
165  
166 //create a graphics object from the image
167 using (var gfx = Graphics.FromImage(bmp))
168 {
169 //create a color matrix object
170 var matrix = new ColorMatrix();
171  
172 //set the opacity
173 matrix.Matrix33 = opacity;
174  
175 //create image attributes
176 var attributes = new ImageAttributes();
177  
178 //set the color(opacity) of the image
179 attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
180  
181 //now draw the image
182 gfx.DrawImage(image, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, image.Width, image.Height,
183 GraphicsUnit.Pixel, attributes);
184 }
185  
186 return bmp;
187 }
188 catch (Exception ex)
189 {
190 MessageBox.Show(ex.Message);
191 return null;
192 }
193 }
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  
203 private bool TryGetDragDropImage(DragEventArgs e, out Image image)
204 {
205 var file = ((IEnumerable<string>) e.Data.GetData(DataFormats.FileDrop)).FirstOrDefault();
206 if (string.IsNullOrEmpty(file))
207 {
208 image = null;
209 return false;
210 }
211  
212 try
213 {
214 image = LoadImageFromFile(file);
215  
216 return true;
217 }
218 catch
219 {
220 image = null;
221 return false;
222 }
223 }
224 }
225 }