1 |
william |
88 |
using System; |
2 |
|
|
using System.Collections.Generic; |
3 |
|
|
using System.ComponentModel; |
4 |
|
|
using System.Drawing; |
5 |
|
|
using System.Data; |
6 |
|
|
using System.Text; |
7 |
|
|
using System.Windows.Forms; |
8 |
|
|
using System.Runtime.InteropServices; |
9 |
|
|
|
10 |
|
|
namespace System.Windows.Forms |
11 |
|
|
{ |
12 |
william |
658 |
|
13 |
william |
287 |
[DefaultEvent("ValueChanged")] |
14 |
william |
88 |
public partial class MaskedHexBox : UserControl |
15 |
|
|
{ |
16 |
william |
671 |
ulong old_value = 0; |
17 |
william |
88 |
public MaskedHexBox() |
18 |
|
|
{ |
19 |
|
|
InitializeComponent(); |
20 |
|
|
SetStyle( |
21 |
|
|
ControlStyles.AllPaintingInWmPaint | |
22 |
|
|
ControlStyles.OptimizedDoubleBuffer | |
23 |
|
|
ControlStyles.UserPaint | |
24 |
|
|
ControlStyles.ContainerControl, |
25 |
|
|
true); |
26 |
|
|
this.isAddressMask = true; |
27 |
|
|
txtMaskedHexBox.KeyDown += new KeyEventHandler(txtMaskedHexBox_KeyDown); |
28 |
|
|
txtMaskedHexBox.TextAlign = HorizontalAlignment.Right; |
29 |
william |
287 |
ValueChanged = null; |
30 |
william |
673 |
txtMaskedHexBox.InsertKeyMode = InsertKeyMode.Overwrite; |
31 |
william |
88 |
} |
32 |
william |
287 |
[Browsable(true)] |
33 |
|
|
[Category("Property Changed")] |
34 |
|
|
[Description("Raised when the Value displayed changes.")] |
35 |
|
|
public event EventHandler<ValueChangedEventArgs> ValueChanged; |
36 |
william |
88 |
|
37 |
|
|
private void MaskedHexBox_KeyDown(object sender, KeyEventArgs e) |
38 |
|
|
{ |
39 |
|
|
} |
40 |
|
|
|
41 |
|
|
private void MaskedHexBox_KeyPress(object sender, KeyPressEventArgs e) |
42 |
|
|
{ |
43 |
|
|
} |
44 |
|
|
|
45 |
|
|
void txtMaskedHexBox_KeyDown(object sender, KeyEventArgs e) |
46 |
|
|
{ |
47 |
|
|
|
48 |
|
|
if (e.KeyCode > Keys.F && e.KeyCode <= Keys.Z && !e.Control && !e.Alt) |
49 |
|
|
{ |
50 |
|
|
e.SuppressKeyPress = true; |
51 |
william |
671 |
} |
52 |
|
|
if (!e.SuppressKeyPress) |
53 |
|
|
{ |
54 |
|
|
old_value = this.Value; |
55 |
|
|
} |
56 |
william |
88 |
} |
57 |
|
|
|
58 |
|
|
new public Font Font |
59 |
|
|
{ |
60 |
|
|
get { return base.Font; } |
61 |
|
|
set |
62 |
|
|
{ |
63 |
|
|
base.Font = value; |
64 |
william |
658 |
|
65 |
|
|
Font t = value; |
66 |
|
|
|
67 |
|
|
txtMaskedHexBox.Font = new Font(t.FontFamily, t.SizeInPoints + 4, t.Style); |
68 |
william |
88 |
btnCopy.Font = value; |
69 |
|
|
btnPaste.Font = value; |
70 |
|
|
} |
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
private string Mask |
74 |
|
|
{ |
75 |
|
|
get { return txtMaskedHexBox.Mask; } |
76 |
|
|
set { txtMaskedHexBox.Mask = value; } |
77 |
|
|
} |
78 |
|
|
|
79 |
|
|
private bool _ReadOnly; |
80 |
|
|
public virtual bool ReadOnly |
81 |
|
|
{ |
82 |
|
|
get { return _ReadOnly; } |
83 |
|
|
set { _ReadOnly = value; if (_ReadOnly) { txtMaskedHexBox.Enabled = false; } else { txtMaskedHexBox.Enabled = true; } } |
84 |
|
|
} |
85 |
|
|
public override string Text |
86 |
|
|
{ |
87 |
william |
671 |
get |
88 |
|
|
{ |
89 |
|
|
return txtMaskedHexBox.Text.PadRight(this.TextLength + 2, '0'); |
90 |
|
|
} |
91 |
|
|
set |
92 |
|
|
{ |
93 |
|
|
old_value = this.Value; |
94 |
|
|
txtMaskedHexBox.Text = value; |
95 |
|
|
DoResize(); |
96 |
|
|
ulong new_value = this.Value; |
97 |
|
|
if ((new_value != old_value) && ValueChanged != null) |
98 |
|
|
ValueChanged(this, new ValueChangedEventArgs(old_value, new_value)); |
99 |
|
|
} |
100 |
william |
88 |
} |
101 |
william |
575 |
public ulong Value |
102 |
william |
88 |
{ |
103 |
william |
575 |
get { return this.ToUInt64(); } |
104 |
william |
88 |
set |
105 |
|
|
{ |
106 |
william |
671 |
|
107 |
william |
88 |
switch (this.TextLength) |
108 |
|
|
{ |
109 |
|
|
case 0: |
110 |
|
|
case 1: |
111 |
|
|
case 2: this.Text = string.Format("0x{0:x2}", value); break; |
112 |
|
|
case 3: |
113 |
|
|
case 4: this.Text = string.Format("0x{0:x4}", value); break; |
114 |
|
|
case 5: |
115 |
|
|
case 6: |
116 |
|
|
case 7: |
117 |
|
|
case 8: this.Text = string.Format("0x{0:x8}", value); break; |
118 |
|
|
default: this.Text = string.Format("0x{0:x" + this.TextLength + "}", value); break; |
119 |
william |
671 |
} |
120 |
william |
88 |
} |
121 |
|
|
} |
122 |
|
|
private bool _isAddressMask; |
123 |
|
|
public bool isAddressMask |
124 |
|
|
{ |
125 |
|
|
get { return _isAddressMask; } |
126 |
|
|
set |
127 |
|
|
{ |
128 |
|
|
_isAddressMask = value; |
129 |
|
|
if (value) |
130 |
|
|
{ |
131 |
|
|
this.CreateTypeSize<uint>(); |
132 |
|
|
} |
133 |
|
|
} |
134 |
|
|
} |
135 |
|
|
|
136 |
|
|
public void CreateTypeSize<T>() where T : IConvertible |
137 |
|
|
{ |
138 |
|
|
Type type = typeof(T); |
139 |
william |
229 |
int size = Marshal.SizeOf(type) * 2; |
140 |
|
|
this.MaxLength = size; |
141 |
|
|
this.TextLength = this.MaxLength; |
142 |
william |
88 |
} |
143 |
|
|
|
144 |
|
|
private int _MaxLength; |
145 |
|
|
public int MaxLength |
146 |
|
|
{ |
147 |
|
|
get { return _MaxLength; } |
148 |
|
|
set { _MaxLength = value; } |
149 |
|
|
} |
150 |
|
|
private int _TextLength; |
151 |
|
|
public int TextLength |
152 |
|
|
{ |
153 |
|
|
get { return _TextLength; } |
154 |
|
|
set |
155 |
|
|
{ |
156 |
|
|
StringBuilder mask = new StringBuilder(); |
157 |
|
|
|
158 |
|
|
mask.Append("\\0x"); |
159 |
|
|
|
160 |
|
|
if (this.MaxLength <= sizeof(uint) * 2) |
161 |
|
|
{ |
162 |
|
|
|
163 |
|
|
switch (value) |
164 |
|
|
{ |
165 |
|
|
case 2: _TextLength = 2; mask.Append(this.CreateMask(_TextLength)); break; |
166 |
|
|
case 4: _TextLength = 4; mask.Append(this.CreateMask(_TextLength)); break; |
167 |
william |
582 |
case 8: goto default; |
168 |
|
|
case 16: _TextLength = 16; mask.Append(this.CreateMask(_TextLength)); break; |
169 |
william |
88 |
default: _TextLength = 8; mask.Append(this.CreateMask(_TextLength)); break; |
170 |
|
|
} |
171 |
|
|
} |
172 |
|
|
else |
173 |
|
|
{ |
174 |
|
|
switch (value) |
175 |
|
|
{ |
176 |
|
|
case 2: _TextLength = 2; mask.Append(this.CreateMask(_TextLength)); break; |
177 |
|
|
case 4: _TextLength = 4; mask.Append(this.CreateMask(_TextLength)); break; |
178 |
|
|
case 8: _TextLength = 8; mask.Append(this.CreateMask(_TextLength)); break; |
179 |
william |
582 |
case 16: _TextLength = 16; mask.Append(this.CreateMask(_TextLength)); break; |
180 |
william |
88 |
default: _TextLength = value; mask.Append(this.CreateMask(_TextLength)); break; |
181 |
|
|
} |
182 |
|
|
} |
183 |
|
|
this.Mask = mask.ToString(); |
184 |
william |
662 |
DoResize(); |
185 |
william |
88 |
} |
186 |
|
|
} |
187 |
|
|
private string CreateMask(int length) |
188 |
|
|
{ |
189 |
|
|
StringBuilder mask = new StringBuilder(); |
190 |
|
|
for (int i = 0; i < length; i++) |
191 |
|
|
{ |
192 |
|
|
mask.Append("C"); |
193 |
|
|
} |
194 |
|
|
return mask.ToString(); |
195 |
|
|
} |
196 |
|
|
|
197 |
|
|
private void btnCopy_Click(object sender, EventArgs e) { Clipboard.SetData(DataFormats.Text, this.Text); } |
198 |
william |
593 |
private void btnPaste_Click(object sender, EventArgs e) |
199 |
|
|
{ |
200 |
|
|
if (this.ReadOnly) return; |
201 |
|
|
var rawText = (string)Clipboard.GetData(DataFormats.Text); |
202 |
|
|
if (rawText.StartsWith("0x")) { rawText = rawText.Remove(0,2); } |
203 |
|
|
if (rawText.StartsWith("x")) { rawText = rawText.Remove(0, 1); } |
204 |
william |
88 |
|
205 |
william |
593 |
rawText = rawText.PadLeft(this.TextLength, '0'); |
206 |
|
|
rawText = string.Format("0x{0}", rawText); |
207 |
|
|
this.Text = rawText; |
208 |
|
|
} |
209 |
|
|
|
210 |
william |
88 |
private void txtMaskedHexBox_MaskInputRejected(object sender, MaskInputRejectedEventArgs e) |
211 |
|
|
{ |
212 |
|
|
|
213 |
|
|
} |
214 |
|
|
|
215 |
william |
662 |
private void MaskedHexBox_Load(object sender, EventArgs e) |
216 |
|
|
{ |
217 |
|
|
DoResize(); |
218 |
|
|
} |
219 |
william |
88 |
|
220 |
william |
662 |
protected override void OnResize(EventArgs e) |
221 |
|
|
{ |
222 |
|
|
base.OnResize(e); |
223 |
|
|
DoResize(); |
224 |
|
|
} |
225 |
|
|
|
226 |
|
|
private void DoResize() |
227 |
|
|
{ |
228 |
|
|
string t = string.Empty; |
229 |
|
|
switch (this.TextLength) |
230 |
|
|
{ |
231 |
|
|
case 2: |
232 |
|
|
t = string.Format("0x{0:x2}", this.Value); |
233 |
|
|
break; |
234 |
|
|
case 4: |
235 |
|
|
t = string.Format("0x{0:x4}", this.Value); |
236 |
|
|
break; |
237 |
|
|
case 8: goto default; |
238 |
|
|
case 16: |
239 |
|
|
t = string.Format("0x{0:x16}", this.Value); |
240 |
|
|
break; |
241 |
|
|
default: |
242 |
|
|
t = string.Format("0x{0:x8}", this.Value); |
243 |
|
|
break; |
244 |
|
|
} |
245 |
|
|
|
246 |
|
|
|
247 |
|
|
|
248 |
|
|
using (Graphics g = this.CreateGraphics()) |
249 |
|
|
{ |
250 |
|
|
var size = g.MeasureString(t, this.Font).ToSize(); |
251 |
|
|
int width = size.Width; // the minimum size of the textbox |
252 |
|
|
double growth = (double)size.Width / 0.5; |
253 |
|
|
|
254 |
|
|
switch (this.TextLength) |
255 |
|
|
{ |
256 |
|
|
case 2: this.Width = (int)growth + 25; break; |
257 |
|
|
case 4: this.Width = (int)growth + 15; break; |
258 |
|
|
case 8: goto default; |
259 |
|
|
case 16: this.Width = (int)growth - 70; break; |
260 |
|
|
default: |
261 |
|
|
this.Width = (int)growth - 15; break; |
262 |
|
|
} |
263 |
|
|
|
264 |
|
|
|
265 |
|
|
} |
266 |
|
|
} |
267 |
william |
671 |
|
268 |
|
|
private void txtMaskedHexBox_TextChanged(object sender, EventArgs e) |
269 |
|
|
{ |
270 |
|
|
ulong new_value = this.Value; |
271 |
|
|
if ((new_value != old_value) && ValueChanged != null) |
272 |
|
|
ValueChanged(this, new ValueChangedEventArgs(old_value, new_value)); |
273 |
|
|
} |
274 |
william |
88 |
} |
275 |
|
|
|
276 |
|
|
#region MaskedHexBox Extensions |
277 |
|
|
public static class MaskedHexBoxExtensions |
278 |
|
|
{ |
279 |
|
|
private static string GetSafeConversionString(this MaskedHexBox val) |
280 |
|
|
{ |
281 |
|
|
StringBuilder builder = new StringBuilder(); |
282 |
|
|
|
283 |
|
|
if (val.Text == "0x") |
284 |
|
|
{ |
285 |
|
|
builder.Append("0x"); |
286 |
|
|
for (int i = 0; i < val.TextLength; i++) |
287 |
|
|
{ |
288 |
|
|
builder.Append("0"); |
289 |
|
|
} |
290 |
|
|
} |
291 |
|
|
else |
292 |
|
|
builder.Append(val.Text.Replace(" ", "")); |
293 |
|
|
return builder.ToString(); |
294 |
|
|
} |
295 |
|
|
public static byte ToByte(this MaskedHexBox val) |
296 |
|
|
{ |
297 |
|
|
try { return Convert.ToByte(val.GetSafeConversionString(), 16); } |
298 |
|
|
catch { return 0; } |
299 |
|
|
} |
300 |
|
|
public static sbyte ToSByte(this MaskedHexBox val) |
301 |
|
|
{ |
302 |
|
|
try |
303 |
|
|
{ |
304 |
|
|
return Convert.ToSByte(val.GetSafeConversionString(), 16); |
305 |
|
|
} |
306 |
|
|
catch { return 0; } |
307 |
|
|
} |
308 |
|
|
public static Int16 ToInt16(this MaskedHexBox val) |
309 |
|
|
{ |
310 |
|
|
try |
311 |
|
|
{ |
312 |
|
|
return Convert.ToInt16(val.GetSafeConversionString(), 16); |
313 |
|
|
} |
314 |
|
|
catch { return 0; } |
315 |
|
|
} |
316 |
|
|
public static Int32 ToInt32(this MaskedHexBox val) |
317 |
|
|
{ |
318 |
|
|
try |
319 |
|
|
{ |
320 |
|
|
return Convert.ToInt32(val.GetSafeConversionString(), 16); |
321 |
|
|
} |
322 |
|
|
catch { return 0; } |
323 |
|
|
} |
324 |
|
|
public static Int64 ToInt64(this MaskedHexBox val) |
325 |
|
|
{ |
326 |
|
|
try |
327 |
|
|
{ |
328 |
|
|
return Convert.ToInt64(val.GetSafeConversionString(), 16); |
329 |
|
|
} |
330 |
|
|
catch { return 0; } |
331 |
|
|
} |
332 |
|
|
public static UInt16 ToUInt16(this MaskedHexBox val) |
333 |
|
|
{ |
334 |
|
|
try |
335 |
|
|
{ |
336 |
|
|
return Convert.ToUInt16(val.GetSafeConversionString(), 16); |
337 |
|
|
} |
338 |
|
|
catch { return 0; } |
339 |
|
|
} |
340 |
|
|
public static UInt32 ToUInt32(this MaskedHexBox val) |
341 |
|
|
{ |
342 |
|
|
try |
343 |
|
|
{ |
344 |
|
|
return Convert.ToUInt32(val.GetSafeConversionString(), 16); |
345 |
|
|
} |
346 |
|
|
catch { return 0; } |
347 |
|
|
} |
348 |
|
|
public static UInt64 ToUInt64(this MaskedHexBox val) |
349 |
|
|
{ |
350 |
|
|
try |
351 |
|
|
{ |
352 |
|
|
return Convert.ToUInt64(val.GetSafeConversionString(), 16); |
353 |
|
|
} |
354 |
|
|
catch { return 0; } |
355 |
|
|
} |
356 |
|
|
public static Single ToSingle(this MaskedHexBox val) |
357 |
|
|
{ |
358 |
|
|
try |
359 |
|
|
{ |
360 |
|
|
return Convert.ToSingle(val.ToUInt32()); |
361 |
|
|
} |
362 |
|
|
catch { return 0; } |
363 |
|
|
} |
364 |
|
|
public static double ToDouble(this MaskedHexBox val) |
365 |
|
|
{ |
366 |
|
|
try |
367 |
|
|
{ |
368 |
|
|
return Convert.ToDouble(val.ToUInt32()); |
369 |
|
|
} |
370 |
|
|
catch { return 0; } |
371 |
|
|
} |
372 |
|
|
public static decimal ToDecimal(this MaskedHexBox val) |
373 |
|
|
{ |
374 |
|
|
try |
375 |
|
|
{ |
376 |
|
|
return Convert.ToDecimal(val.ToUInt32()); |
377 |
|
|
} |
378 |
|
|
catch { return 0; } |
379 |
|
|
} |
380 |
|
|
//public static string ToString(this MaskedHexBox val) |
381 |
|
|
//{ |
382 |
|
|
// return Convert.ToString(val.ToUInt32(), 16); |
383 |
|
|
//} |
384 |
|
|
} |
385 |
|
|
#endregion |
386 |
|
|
} |