/[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 582 - (hide annotations) (download)
Thu Jun 6 08:44:41 2013 UTC (7 years, 10 months ago) by william
File size: 9874 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 575 public ulong Value
82 william 88 {
83 william 575 get { return this.ToUInt64(); }
84 william 88 set
85     {
86 william 575 ulong 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 575 ulong new_value = this.Value;
101 william 287 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 2: _TextLength = 2; mask.Append(this.CreateMask(_TextLength)); break;
149     case 4: _TextLength = 4; mask.Append(this.CreateMask(_TextLength)); break;
150 william 582 case 8: goto default;
151     case 16: _TextLength = 16; mask.Append(this.CreateMask(_TextLength)); break;
152 william 88 default: _TextLength = 8; mask.Append(this.CreateMask(_TextLength)); break;
153     }
154     }
155     else
156     {
157     switch (value)
158     {
159     case 2: _TextLength = 2; mask.Append(this.CreateMask(_TextLength)); break;
160     case 4: _TextLength = 4; mask.Append(this.CreateMask(_TextLength)); break;
161     case 8: _TextLength = 8; mask.Append(this.CreateMask(_TextLength)); break;
162 william 582 case 16: _TextLength = 16; mask.Append(this.CreateMask(_TextLength)); break;
163 william 88 default: _TextLength = value; mask.Append(this.CreateMask(_TextLength)); break;
164     }
165     }
166     this.Mask = mask.ToString();
167     }
168     }
169     private string CreateMask(int length)
170     {
171     StringBuilder mask = new StringBuilder();
172     for (int i = 0; i < length; i++)
173     {
174     mask.Append("C");
175     }
176     return mask.ToString();
177     }
178    
179     private void btnCopy_Click(object sender, EventArgs e) { Clipboard.SetData(DataFormats.Text, this.Text); }
180     private void btnPaste_Click(object sender, EventArgs e) { if (this.ReadOnly) return; this.Text = (string)Clipboard.GetData(DataFormats.Text); }
181    
182     private void txtMaskedHexBox_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
183     {
184    
185     }
186    
187    
188     }
189    
190     #region MaskedHexBox Extensions
191     public static class MaskedHexBoxExtensions
192     {
193     private static string GetSafeConversionString(this MaskedHexBox val)
194     {
195     StringBuilder builder = new StringBuilder();
196    
197     if (val.Text == "0x")
198     {
199     builder.Append("0x");
200     for (int i = 0; i < val.TextLength; i++)
201     {
202     builder.Append("0");
203     }
204     }
205     else
206     builder.Append(val.Text.Replace(" ", ""));
207     return builder.ToString();
208     }
209     public static byte ToByte(this MaskedHexBox val)
210     {
211     try { return Convert.ToByte(val.GetSafeConversionString(), 16); }
212     catch { return 0; }
213     }
214     public static sbyte ToSByte(this MaskedHexBox val)
215     {
216     try
217     {
218     return Convert.ToSByte(val.GetSafeConversionString(), 16);
219     }
220     catch { return 0; }
221     }
222     public static Int16 ToInt16(this MaskedHexBox val)
223     {
224     try
225     {
226     return Convert.ToInt16(val.GetSafeConversionString(), 16);
227     }
228     catch { return 0; }
229     }
230     public static Int32 ToInt32(this MaskedHexBox val)
231     {
232     try
233     {
234     return Convert.ToInt32(val.GetSafeConversionString(), 16);
235     }
236     catch { return 0; }
237     }
238     public static Int64 ToInt64(this MaskedHexBox val)
239     {
240     try
241     {
242     return Convert.ToInt64(val.GetSafeConversionString(), 16);
243     }
244     catch { return 0; }
245     }
246     public static UInt16 ToUInt16(this MaskedHexBox val)
247     {
248     try
249     {
250     return Convert.ToUInt16(val.GetSafeConversionString(), 16);
251     }
252     catch { return 0; }
253     }
254     public static UInt32 ToUInt32(this MaskedHexBox val)
255     {
256     try
257     {
258     return Convert.ToUInt32(val.GetSafeConversionString(), 16);
259     }
260     catch { return 0; }
261     }
262     public static UInt64 ToUInt64(this MaskedHexBox val)
263     {
264     try
265     {
266     return Convert.ToUInt64(val.GetSafeConversionString(), 16);
267     }
268     catch { return 0; }
269     }
270     public static Single ToSingle(this MaskedHexBox val)
271     {
272     try
273     {
274     return Convert.ToSingle(val.ToUInt32());
275     }
276     catch { return 0; }
277     }
278     public static double ToDouble(this MaskedHexBox val)
279     {
280     try
281     {
282     return Convert.ToDouble(val.ToUInt32());
283     }
284     catch { return 0; }
285     }
286     public static decimal ToDecimal(this MaskedHexBox val)
287     {
288     try
289     {
290     return Convert.ToDecimal(val.ToUInt32());
291     }
292     catch { return 0; }
293     }
294     //public static string ToString(this MaskedHexBox val)
295     //{
296     // return Convert.ToString(val.ToUInt32(), 16);
297     //}
298     }
299     #endregion
300     }

  ViewVC Help
Powered by ViewVC 1.1.22