/[RomCheater]/trunk/Win32/libWin32/Win32/UI/MaskedHexBox.cs
ViewVC logotype

Contents of /trunk/Win32/libWin32/Win32/UI/MaskedHexBox.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 903 - (show annotations) (download)
Wed Sep 17 12:44:22 2014 UTC (7 years ago) by william
File size: 13758 byte(s)
+ fix some issues with numeric input controls not properly sizing themselves

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 ulong old_value = 0;
17 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 ValueChanged = null;
30 txtMaskedHexBox.InsertKeyMode = InsertKeyMode.Overwrite;
31 this.AutoSize = true;
32 OnAutoSizeChanged(new EventArgs());
33 //this.TextLength = 8;
34 //CreateTypeSize<ulong>();
35 }
36 [Browsable(true)]
37 [Category("Property Changed")]
38 [Description("Raised when the Value displayed changes.")]
39 public event EventHandler<ValueChangedEventArgs> ValueChanged;
40
41
42
43
44
45 [Browsable(true)]
46 [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
47 [EditorBrowsable(EditorBrowsableState.Always)]
48 public override bool AutoSize { get { return base.AutoSize; } set { base.AutoSize = value; OnAutoSizeChanged(new EventArgs()); } }
49
50 protected override void OnAutoSizeChanged(EventArgs e)
51 {
52 base.OnAutoSizeChanged(e);
53 layout_panel.AutoSize = base.AutoSize;
54 }
55
56 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 }
71 if (!e.SuppressKeyPress)
72 {
73 old_value = this.Value;
74 }
75 }
76
77 new public Font Font
78 {
79 get { return base.Font; }
80 set
81 {
82 base.Font = value;
83
84 txtMaskedHexBox.Font = value;
85 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 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 }
118 public ulong Value
119 {
120 get { return this.ToUInt64(); }
121 set
122 {
123
124 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 }
137 DoResize();
138 }
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 int size = Marshal.SizeOf(type) * 2;
158 this.MaxLength = size;
159 this.TextLength = this.MaxLength;
160 DoResize();
161 }
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 case 8: goto default;
187 case 16: _TextLength = 16; mask.Append(this.CreateMask(_TextLength)); break;
188 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 case 16: _TextLength = 16; mask.Append(this.CreateMask(_TextLength)); break;
199 default: _TextLength = value; mask.Append(this.CreateMask(_TextLength)); break;
200 }
201 }
202 this.Mask = mask.ToString();
203 DoResize();
204 }
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 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
224 rawText = rawText.PadLeft(this.TextLength, '0');
225 rawText = string.Format("0x{0}", rawText);
226 this.Text = rawText;
227 }
228
229 private void txtMaskedHexBox_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
230 {
231
232 }
233
234 private void MaskedHexBox_Load(object sender, EventArgs e)
235 {
236 DoResize();
237 }
238
239 //protected override void OnResize(EventArgs e)
240 //{
241 // base.OnResize(e);
242 // DoResize();
243 //}
244
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 int width = size.Width; // the minimum size of the textbox
271 double growth = (double)size.Width;
272
273 //switch (this.TextLength)
274 //{
275 // case 2: this.Width = (int)growth + 25; break;
276 // case 4: this.Width = (int)growth + 15; break;
277 // case 8: goto default;
278 // case 16: this.Width = (int)growth - 70; break;
279 // default:
280 // this.Width = (int)growth - 15; break;
281 //}
282 switch (this.TextLength)
283 {
284 case 2: width = (int)growth; break;
285 case 4: width = (int)growth; break;
286 case 8: goto default;
287 case 16: width = (int)growth; break;
288 default:
289 width = (int)growth; break;
290 }
291
292 txtMaskedHexBox.Width = width + 5;
293 this.Width = width;
294 }
295 }
296
297 private void txtMaskedHexBox_TextChanged(object sender, EventArgs e)
298 {
299 ulong new_value = this.Value;
300 if ((new_value != old_value) && ValueChanged != null)
301 ValueChanged(this, new ValueChangedEventArgs(old_value, new_value));
302 }
303 }
304
305 #region MaskedHexBox Extensions
306 public static class MaskedHexBoxExtensions
307 {
308 private static string GetSafeConversionString(this MaskedHexBox val)
309 {
310 StringBuilder builder = new StringBuilder();
311
312 if (val.Text == "0x")
313 {
314 builder.Append("0x");
315 for (int i = 0; i < val.TextLength; i++)
316 {
317 builder.Append("0");
318 }
319 }
320 else
321 builder.Append(val.Text.Replace(" ", ""));
322 return builder.ToString();
323 }
324 public static byte ToByte(this MaskedHexBox val)
325 {
326 try { return Convert.ToByte(val.GetSafeConversionString(), 16); }
327 catch { return 0; }
328 }
329 public static sbyte ToSByte(this MaskedHexBox val)
330 {
331 try
332 {
333 return Convert.ToSByte(val.GetSafeConversionString(), 16);
334 }
335 catch { return 0; }
336 }
337 public static Int16 ToInt16(this MaskedHexBox val)
338 {
339 try
340 {
341 return Convert.ToInt16(val.GetSafeConversionString(), 16);
342 }
343 catch { return 0; }
344 }
345 public static Int32 ToInt32(this MaskedHexBox val)
346 {
347 try
348 {
349 return Convert.ToInt32(val.GetSafeConversionString(), 16);
350 }
351 catch { return 0; }
352 }
353 public static Int64 ToInt64(this MaskedHexBox val)
354 {
355 try
356 {
357 return Convert.ToInt64(val.GetSafeConversionString(), 16);
358 }
359 catch { return 0; }
360 }
361 public static UInt16 ToUInt16(this MaskedHexBox val)
362 {
363 try
364 {
365 return Convert.ToUInt16(val.GetSafeConversionString(), 16);
366 }
367 catch { return 0; }
368 }
369 public static UInt32 ToUInt32(this MaskedHexBox val)
370 {
371 try
372 {
373 return Convert.ToUInt32(val.GetSafeConversionString(), 16);
374 }
375 catch { return 0; }
376 }
377 public static UInt64 ToUInt64(this MaskedHexBox val)
378 {
379 try
380 {
381 return Convert.ToUInt64(val.GetSafeConversionString(), 16);
382 }
383 catch { return 0; }
384 }
385 public static Single ToSingle(this MaskedHexBox val)
386 {
387 try
388 {
389 return Convert.ToSingle(val.ToUInt32());
390 }
391 catch { return 0; }
392 }
393 public static double ToDouble(this MaskedHexBox val)
394 {
395 try
396 {
397 return Convert.ToDouble(val.ToUInt32());
398 }
399 catch { return 0; }
400 }
401 public static decimal ToDecimal(this MaskedHexBox val)
402 {
403 try
404 {
405 return Convert.ToDecimal(val.ToUInt32());
406 }
407 catch { return 0; }
408 }
409 //public static string ToString(this MaskedHexBox val)
410 //{
411 // return Convert.ToString(val.ToUInt32(), 16);
412 //}
413 }
414 #endregion
415 }

  ViewVC Help
Powered by ViewVC 1.1.22