/[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 287 - (hide annotations) (download)
Tue Jun 5 08:52:07 2012 UTC (8 years, 10 months ago) by william
File size: 10048 byte(s)

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

  ViewVC Help
Powered by ViewVC 1.1.22