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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 671 - (hide annotations) (download)
Mon Jun 17 05:29:37 2013 UTC (7 years, 10 months ago) by william
File size: 12595 byte(s)
+ fix when valuechanged event fires

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

  ViewVC Help
Powered by ViewVC 1.1.22