/[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 88 - (hide annotations) (download)
Wed May 9 20:52:20 2012 UTC (8 years, 11 months ago) by william
File size: 9495 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     public partial class MaskedHexBox : UserControl
13     {
14     public MaskedHexBox()
15     {
16     InitializeComponent();
17     SetStyle(
18     ControlStyles.AllPaintingInWmPaint |
19     ControlStyles.OptimizedDoubleBuffer |
20     ControlStyles.UserPaint |
21     ControlStyles.ContainerControl,
22     true);
23     this.isAddressMask = true;
24     txtMaskedHexBox.KeyDown += new KeyEventHandler(txtMaskedHexBox_KeyDown);
25     txtMaskedHexBox.TextAlign = HorizontalAlignment.Right;
26    
27     }
28    
29     private void MaskedHexBox_KeyDown(object sender, KeyEventArgs e)
30     {
31     }
32    
33     private void MaskedHexBox_KeyPress(object sender, KeyPressEventArgs e)
34     {
35     }
36    
37     void txtMaskedHexBox_KeyDown(object sender, KeyEventArgs e)
38     {
39    
40     if (e.KeyCode > Keys.F && e.KeyCode <= Keys.Z && !e.Control && !e.Alt)
41     {
42     e.SuppressKeyPress = true;
43     }
44     }
45    
46    
47     new public Font Font
48     {
49     get { return base.Font; }
50     set
51     {
52     base.Font = value;
53     txtMaskedHexBox.Font = value;
54     btnCopy.Font = value;
55     btnPaste.Font = value;
56     }
57     }
58    
59     private string Mask
60     {
61     get { return txtMaskedHexBox.Mask; }
62     set { txtMaskedHexBox.Mask = value; }
63     }
64    
65     private bool _ReadOnly;
66     public virtual bool ReadOnly
67     {
68     get { return _ReadOnly; }
69     set { _ReadOnly = value; if (_ReadOnly) { txtMaskedHexBox.Enabled = false; } else { txtMaskedHexBox.Enabled = true; } }
70     }
71     public override string Text
72     {
73     get { return txtMaskedHexBox.Text.PadRight(this.TextLength + 2, '0'); }
74     set { txtMaskedHexBox.Text = value; }
75     }
76     public ulong Value
77     {
78     get { return this.ToUInt64(); }
79     set
80     {
81     switch (this.TextLength)
82     {
83     case 0:
84     case 1:
85     case 2: this.Text = string.Format("0x{0:x2}", value); break;
86     case 3:
87     case 4: this.Text = string.Format("0x{0:x4}", value); break;
88     case 5:
89     case 6:
90     case 7:
91     case 8: this.Text = string.Format("0x{0:x8}", value); break;
92     default: this.Text = string.Format("0x{0:x" + this.TextLength + "}", value); break;
93     }
94     }
95     }
96     private bool _isAddressMask;
97     public bool isAddressMask
98     {
99     get { return _isAddressMask; }
100     set
101     {
102     _isAddressMask = value;
103     if (value)
104     {
105     this.CreateTypeSize<uint>();
106     }
107     }
108     }
109    
110     public void CreateTypeSize<T>() where T : IConvertible
111     {
112     Type type = typeof(T);
113     this.TextLength = Marshal.SizeOf(type) * 2;
114     this.MaxLength = this.TextLength;
115     }
116    
117     private int _MaxLength;
118     public int MaxLength
119     {
120     get { return _MaxLength; }
121     set { _MaxLength = value; }
122     }
123     private int _TextLength;
124     public int TextLength
125     {
126     get { return _TextLength; }
127     set
128     {
129     StringBuilder mask = new StringBuilder();
130    
131     mask.Append("\\0x");
132    
133     if (this.MaxLength <= sizeof(uint) * 2)
134     {
135    
136     switch (value)
137     {
138     case 0:
139     case 1:
140     case 2: _TextLength = 2; mask.Append(this.CreateMask(_TextLength)); break;
141     case 3:
142     case 4: _TextLength = 4; mask.Append(this.CreateMask(_TextLength)); break;
143     case 5:
144     case 6:
145     case 7:
146     case 8:
147     default: _TextLength = 8; mask.Append(this.CreateMask(_TextLength)); break;
148     }
149     }
150     else
151     {
152     switch (value)
153     {
154     case 0:
155     case 1:
156     case 2: _TextLength = 2; mask.Append(this.CreateMask(_TextLength)); break;
157     case 3:
158     case 4: _TextLength = 4; mask.Append(this.CreateMask(_TextLength)); break;
159     case 5:
160     case 6:
161     case 7:
162     case 8: _TextLength = 8; mask.Append(this.CreateMask(_TextLength)); break;
163     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