1 |
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 |
|
13 |
[DefaultEvent("ValueChanged")] |
14 |
public partial class MaskedHexBox : UserControl |
15 |
{ |
16 |
const int ReferenceWidthPixels = 114; |
17 |
const double ReferenceWidthPercent = 0.50; |
18 |
|
19 |
public MaskedHexBox() |
20 |
{ |
21 |
InitializeComponent(); |
22 |
SetStyle( |
23 |
ControlStyles.AllPaintingInWmPaint | |
24 |
ControlStyles.OptimizedDoubleBuffer | |
25 |
ControlStyles.UserPaint | |
26 |
ControlStyles.ContainerControl, |
27 |
true); |
28 |
this.isAddressMask = true; |
29 |
txtMaskedHexBox.KeyDown += new KeyEventHandler(txtMaskedHexBox_KeyDown); |
30 |
txtMaskedHexBox.TextAlign = HorizontalAlignment.Right; |
31 |
ValueChanged = null; |
32 |
} |
33 |
[Browsable(true)] |
34 |
[Category("Property Changed")] |
35 |
[Description("Raised when the Value displayed changes.")] |
36 |
public event EventHandler<ValueChangedEventArgs> ValueChanged; |
37 |
|
38 |
private void MaskedHexBox_KeyDown(object sender, KeyEventArgs e) |
39 |
{ |
40 |
} |
41 |
|
42 |
private void MaskedHexBox_KeyPress(object sender, KeyPressEventArgs e) |
43 |
{ |
44 |
} |
45 |
|
46 |
void txtMaskedHexBox_KeyDown(object sender, KeyEventArgs e) |
47 |
{ |
48 |
|
49 |
if (e.KeyCode > Keys.F && e.KeyCode <= Keys.Z && !e.Control && !e.Alt) |
50 |
{ |
51 |
e.SuppressKeyPress = true; |
52 |
} |
53 |
} |
54 |
|
55 |
new public Font Font |
56 |
{ |
57 |
get { return base.Font; } |
58 |
set |
59 |
{ |
60 |
base.Font = value; |
61 |
|
62 |
Font t = value; |
63 |
|
64 |
txtMaskedHexBox.Font = new Font(t.FontFamily, t.SizeInPoints + 4, t.Style); |
65 |
btnCopy.Font = value; |
66 |
btnPaste.Font = value; |
67 |
} |
68 |
} |
69 |
|
70 |
private string Mask |
71 |
{ |
72 |
get { return txtMaskedHexBox.Mask; } |
73 |
set { txtMaskedHexBox.Mask = value; } |
74 |
} |
75 |
|
76 |
private bool _ReadOnly; |
77 |
public virtual bool ReadOnly |
78 |
{ |
79 |
get { return _ReadOnly; } |
80 |
set { _ReadOnly = value; if (_ReadOnly) { txtMaskedHexBox.Enabled = false; } else { txtMaskedHexBox.Enabled = true; } } |
81 |
} |
82 |
public override string Text |
83 |
{ |
84 |
get { return txtMaskedHexBox.Text.PadRight(this.TextLength + 2, '0'); } |
85 |
set { txtMaskedHexBox.Text = value; } |
86 |
} |
87 |
public ulong Value |
88 |
{ |
89 |
get { return this.ToUInt64(); } |
90 |
set |
91 |
{ |
92 |
ulong old_value = this.Value; |
93 |
switch (this.TextLength) |
94 |
{ |
95 |
case 0: |
96 |
case 1: |
97 |
case 2: this.Text = string.Format("0x{0:x2}", value); break; |
98 |
case 3: |
99 |
case 4: this.Text = string.Format("0x{0:x4}", value); break; |
100 |
case 5: |
101 |
case 6: |
102 |
case 7: |
103 |
case 8: this.Text = string.Format("0x{0:x8}", value); break; |
104 |
default: this.Text = string.Format("0x{0:x" + this.TextLength + "}", value); break; |
105 |
} |
106 |
ulong new_value = this.Value; |
107 |
if ((new_value!= old_value) && ValueChanged != null) |
108 |
ValueChanged(this, new ValueChangedEventArgs(old_value, new_value)); |
109 |
} |
110 |
} |
111 |
private bool _isAddressMask; |
112 |
public bool isAddressMask |
113 |
{ |
114 |
get { return _isAddressMask; } |
115 |
set |
116 |
{ |
117 |
_isAddressMask = value; |
118 |
if (value) |
119 |
{ |
120 |
this.CreateTypeSize<uint>(); |
121 |
} |
122 |
} |
123 |
} |
124 |
|
125 |
public void CreateTypeSize<T>() where T : IConvertible |
126 |
{ |
127 |
Type type = typeof(T); |
128 |
int size = Marshal.SizeOf(type) * 2; |
129 |
this.MaxLength = size; |
130 |
this.TextLength = this.MaxLength; |
131 |
} |
132 |
|
133 |
private int _MaxLength; |
134 |
public int MaxLength |
135 |
{ |
136 |
get { return _MaxLength; } |
137 |
set { _MaxLength = value; } |
138 |
} |
139 |
private int _TextLength; |
140 |
public int TextLength |
141 |
{ |
142 |
get { return _TextLength; } |
143 |
set |
144 |
{ |
145 |
StringBuilder mask = new StringBuilder(); |
146 |
|
147 |
mask.Append("\\0x"); |
148 |
|
149 |
if (this.MaxLength <= sizeof(uint) * 2) |
150 |
{ |
151 |
|
152 |
switch (value) |
153 |
{ |
154 |
case 2: _TextLength = 2; mask.Append(this.CreateMask(_TextLength)); break; |
155 |
case 4: _TextLength = 4; mask.Append(this.CreateMask(_TextLength)); break; |
156 |
case 8: goto default; |
157 |
case 16: _TextLength = 16; mask.Append(this.CreateMask(_TextLength)); break; |
158 |
default: _TextLength = 8; mask.Append(this.CreateMask(_TextLength)); break; |
159 |
} |
160 |
} |
161 |
else |
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 |
case 8: _TextLength = 8; mask.Append(this.CreateMask(_TextLength)); break; |
168 |
case 16: _TextLength = 16; mask.Append(this.CreateMask(_TextLength)); break; |
169 |
default: _TextLength = value; mask.Append(this.CreateMask(_TextLength)); break; |
170 |
} |
171 |
} |
172 |
this.Mask = mask.ToString(); |
173 |
} |
174 |
} |
175 |
private string CreateMask(int length) |
176 |
{ |
177 |
StringBuilder mask = new StringBuilder(); |
178 |
for (int i = 0; i < length; i++) |
179 |
{ |
180 |
mask.Append("C"); |
181 |
} |
182 |
return mask.ToString(); |
183 |
} |
184 |
|
185 |
private void btnCopy_Click(object sender, EventArgs e) { Clipboard.SetData(DataFormats.Text, this.Text); } |
186 |
private void btnPaste_Click(object sender, EventArgs e) |
187 |
{ |
188 |
if (this.ReadOnly) return; |
189 |
var rawText = (string)Clipboard.GetData(DataFormats.Text); |
190 |
if (rawText.StartsWith("0x")) { rawText = rawText.Remove(0,2); } |
191 |
if (rawText.StartsWith("x")) { rawText = rawText.Remove(0, 1); } |
192 |
|
193 |
rawText = rawText.PadLeft(this.TextLength, '0'); |
194 |
rawText = string.Format("0x{0}", rawText); |
195 |
this.Text = rawText; |
196 |
} |
197 |
|
198 |
private void txtMaskedHexBox_MaskInputRejected(object sender, MaskInputRejectedEventArgs e) |
199 |
{ |
200 |
|
201 |
} |
202 |
|
203 |
|
204 |
} |
205 |
|
206 |
#region MaskedHexBox Extensions |
207 |
public static class MaskedHexBoxExtensions |
208 |
{ |
209 |
private static string GetSafeConversionString(this MaskedHexBox val) |
210 |
{ |
211 |
StringBuilder builder = new StringBuilder(); |
212 |
|
213 |
if (val.Text == "0x") |
214 |
{ |
215 |
builder.Append("0x"); |
216 |
for (int i = 0; i < val.TextLength; i++) |
217 |
{ |
218 |
builder.Append("0"); |
219 |
} |
220 |
} |
221 |
else |
222 |
builder.Append(val.Text.Replace(" ", "")); |
223 |
return builder.ToString(); |
224 |
} |
225 |
public static byte ToByte(this MaskedHexBox val) |
226 |
{ |
227 |
try { return Convert.ToByte(val.GetSafeConversionString(), 16); } |
228 |
catch { return 0; } |
229 |
} |
230 |
public static sbyte ToSByte(this MaskedHexBox val) |
231 |
{ |
232 |
try |
233 |
{ |
234 |
return Convert.ToSByte(val.GetSafeConversionString(), 16); |
235 |
} |
236 |
catch { return 0; } |
237 |
} |
238 |
public static Int16 ToInt16(this MaskedHexBox val) |
239 |
{ |
240 |
try |
241 |
{ |
242 |
return Convert.ToInt16(val.GetSafeConversionString(), 16); |
243 |
} |
244 |
catch { return 0; } |
245 |
} |
246 |
public static Int32 ToInt32(this MaskedHexBox val) |
247 |
{ |
248 |
try |
249 |
{ |
250 |
return Convert.ToInt32(val.GetSafeConversionString(), 16); |
251 |
} |
252 |
catch { return 0; } |
253 |
} |
254 |
public static Int64 ToInt64(this MaskedHexBox val) |
255 |
{ |
256 |
try |
257 |
{ |
258 |
return Convert.ToInt64(val.GetSafeConversionString(), 16); |
259 |
} |
260 |
catch { return 0; } |
261 |
} |
262 |
public static UInt16 ToUInt16(this MaskedHexBox val) |
263 |
{ |
264 |
try |
265 |
{ |
266 |
return Convert.ToUInt16(val.GetSafeConversionString(), 16); |
267 |
} |
268 |
catch { return 0; } |
269 |
} |
270 |
public static UInt32 ToUInt32(this MaskedHexBox val) |
271 |
{ |
272 |
try |
273 |
{ |
274 |
return Convert.ToUInt32(val.GetSafeConversionString(), 16); |
275 |
} |
276 |
catch { return 0; } |
277 |
} |
278 |
public static UInt64 ToUInt64(this MaskedHexBox val) |
279 |
{ |
280 |
try |
281 |
{ |
282 |
return Convert.ToUInt64(val.GetSafeConversionString(), 16); |
283 |
} |
284 |
catch { return 0; } |
285 |
} |
286 |
public static Single ToSingle(this MaskedHexBox val) |
287 |
{ |
288 |
try |
289 |
{ |
290 |
return Convert.ToSingle(val.ToUInt32()); |
291 |
} |
292 |
catch { return 0; } |
293 |
} |
294 |
public static double ToDouble(this MaskedHexBox val) |
295 |
{ |
296 |
try |
297 |
{ |
298 |
return Convert.ToDouble(val.ToUInt32()); |
299 |
} |
300 |
catch { return 0; } |
301 |
} |
302 |
public static decimal ToDecimal(this MaskedHexBox val) |
303 |
{ |
304 |
try |
305 |
{ |
306 |
return Convert.ToDecimal(val.ToUInt32()); |
307 |
} |
308 |
catch { return 0; } |
309 |
} |
310 |
//public static string ToString(this MaskedHexBox val) |
311 |
//{ |
312 |
// return Convert.ToString(val.ToUInt32(), 16); |
313 |
//} |
314 |
} |
315 |
#endregion |
316 |
} |