/[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 662 - (show annotations) (download)
Mon Jun 17 02:54:52 2013 UTC (8 years, 5 months ago) by william
File size: 12029 byte(s)
+ fix resize of MaskedHexControl

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

  ViewVC Help
Powered by ViewVC 1.1.22