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