/[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 229 - (show annotations) (download)
Sat Jun 2 18:31:40 2012 UTC (8 years, 6 months ago) by william
File size: 9524 byte(s)

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 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 int size = Marshal.SizeOf(type) * 2;
114 this.MaxLength = size;
115 this.TextLength = this.MaxLength;
116 }
117
118 private int _MaxLength;
119 public int MaxLength
120 {
121 get { return _MaxLength; }
122 set { _MaxLength = value; }
123 }
124 private int _TextLength;
125 public int TextLength
126 {
127 get { return _TextLength; }
128 set
129 {
130 StringBuilder mask = new StringBuilder();
131
132 mask.Append("\\0x");
133
134 if (this.MaxLength <= sizeof(uint) * 2)
135 {
136
137 switch (value)
138 {
139 case 0:
140 case 1:
141 case 2: _TextLength = 2; mask.Append(this.CreateMask(_TextLength)); break;
142 case 3:
143 case 4: _TextLength = 4; mask.Append(this.CreateMask(_TextLength)); break;
144 case 5:
145 case 6:
146 case 7:
147 case 8:
148 default: _TextLength = 8; mask.Append(this.CreateMask(_TextLength)); break;
149 }
150 }
151 else
152 {
153 switch (value)
154 {
155 case 0:
156 case 1:
157 case 2: _TextLength = 2; mask.Append(this.CreateMask(_TextLength)); break;
158 case 3:
159 case 4: _TextLength = 4; mask.Append(this.CreateMask(_TextLength)); break;
160 case 5:
161 case 6:
162 case 7:
163 case 8: _TextLength = 8; mask.Append(this.CreateMask(_TextLength)); break;
164 default: _TextLength = value; mask.Append(this.CreateMask(_TextLength)); break;
165 }
166 }
167 this.Mask = mask.ToString();
168 }
169 }
170 private string CreateMask(int length)
171 {
172 StringBuilder mask = new StringBuilder();
173 for (int i = 0; i < length; i++)
174 {
175 mask.Append("C");
176 }
177 return mask.ToString();
178 }
179
180 private void btnCopy_Click(object sender, EventArgs e) { Clipboard.SetData(DataFormats.Text, this.Text); }
181 private void btnPaste_Click(object sender, EventArgs e) { if (this.ReadOnly) return; this.Text = (string)Clipboard.GetData(DataFormats.Text); }
182
183 private void txtMaskedHexBox_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
184 {
185
186 }
187
188
189 }
190
191 #region MaskedHexBox Extensions
192 public static class MaskedHexBoxExtensions
193 {
194 private static string GetSafeConversionString(this MaskedHexBox val)
195 {
196 StringBuilder builder = new StringBuilder();
197
198 if (val.Text == "0x")
199 {
200 builder.Append("0x");
201 for (int i = 0; i < val.TextLength; i++)
202 {
203 builder.Append("0");
204 }
205 }
206 else
207 builder.Append(val.Text.Replace(" ", ""));
208 return builder.ToString();
209 }
210 public static byte ToByte(this MaskedHexBox val)
211 {
212 try { return Convert.ToByte(val.GetSafeConversionString(), 16); }
213 catch { return 0; }
214 }
215 public static sbyte ToSByte(this MaskedHexBox val)
216 {
217 try
218 {
219 return Convert.ToSByte(val.GetSafeConversionString(), 16);
220 }
221 catch { return 0; }
222 }
223 public static Int16 ToInt16(this MaskedHexBox val)
224 {
225 try
226 {
227 return Convert.ToInt16(val.GetSafeConversionString(), 16);
228 }
229 catch { return 0; }
230 }
231 public static Int32 ToInt32(this MaskedHexBox val)
232 {
233 try
234 {
235 return Convert.ToInt32(val.GetSafeConversionString(), 16);
236 }
237 catch { return 0; }
238 }
239 public static Int64 ToInt64(this MaskedHexBox val)
240 {
241 try
242 {
243 return Convert.ToInt64(val.GetSafeConversionString(), 16);
244 }
245 catch { return 0; }
246 }
247 public static UInt16 ToUInt16(this MaskedHexBox val)
248 {
249 try
250 {
251 return Convert.ToUInt16(val.GetSafeConversionString(), 16);
252 }
253 catch { return 0; }
254 }
255 public static UInt32 ToUInt32(this MaskedHexBox val)
256 {
257 try
258 {
259 return Convert.ToUInt32(val.GetSafeConversionString(), 16);
260 }
261 catch { return 0; }
262 }
263 public static UInt64 ToUInt64(this MaskedHexBox val)
264 {
265 try
266 {
267 return Convert.ToUInt64(val.GetSafeConversionString(), 16);
268 }
269 catch { return 0; }
270 }
271 public static Single ToSingle(this MaskedHexBox val)
272 {
273 try
274 {
275 return Convert.ToSingle(val.ToUInt32());
276 }
277 catch { return 0; }
278 }
279 public static double ToDouble(this MaskedHexBox val)
280 {
281 try
282 {
283 return Convert.ToDouble(val.ToUInt32());
284 }
285 catch { return 0; }
286 }
287 public static decimal ToDecimal(this MaskedHexBox val)
288 {
289 try
290 {
291 return Convert.ToDecimal(val.ToUInt32());
292 }
293 catch { return 0; }
294 }
295 //public static string ToString(this MaskedHexBox val)
296 //{
297 // return Convert.ToString(val.ToUInt32(), 16);
298 //}
299 }
300 #endregion
301 }

  ViewVC Help
Powered by ViewVC 1.1.22