/[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 658 - (show annotations) (download)
Mon Jun 17 02:21:26 2013 UTC (6 years, 8 months ago) by william
File size: 10541 byte(s)
+ update maskedHexBox using a tablelayoutpanel to auto-size the contents more efficiently

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

  ViewVC Help
Powered by ViewVC 1.1.22