initial commit

This commit is contained in:
i2p
2026-08-27 11:22:16 -06:00
commit 96afff7a83
600 changed files with 29291 additions and 0 deletions
@@ -0,0 +1,141 @@
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
// thanks to Mavamaarten~ for coding this
namespace Quasar.Server.Controls
{
internal class DotNetBarTabControl : TabControl
{
public DotNetBarTabControl()
{
SetStyle(
ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw | ControlStyles.UserPaint |
ControlStyles.DoubleBuffer, true);
SizeMode = TabSizeMode.Fixed;
ItemSize = new Size(44, 136);
Alignment = TabAlignment.Left;
SelectedIndex = 0;
}
protected override void OnPaint(PaintEventArgs e)
{
Bitmap b = new Bitmap(Width, Height);
Graphics g = Graphics.FromImage(b);
if (!DesignMode)
SelectedTab.BackColor = SystemColors.Control;
g.Clear(SystemColors.Control);
g.FillRectangle(new SolidBrush(Color.FromArgb(246, 248, 252)),
new Rectangle(0, 0, ItemSize.Height + 4, Height));
g.DrawLine(new Pen(Color.FromArgb(170, 187, 204)), new Point(ItemSize.Height + 3, 0),
new Point(ItemSize.Height + 3, 999));
g.DrawLine(new Pen(Color.FromArgb(170, 187, 204)), new Point(0, Size.Height - 1),
new Point(Width + 3, Size.Height - 1));
for (int i = 0; i <= TabCount - 1; i++)
{
if (i == SelectedIndex)
{
Rectangle x2 = new Rectangle(new Point(GetTabRect(i).Location.X - 2, GetTabRect(i).Location.Y - 2),
new Size(GetTabRect(i).Width + 3, GetTabRect(i).Height - 1));
ColorBlend myBlend = new ColorBlend();
myBlend.Colors = new Color[] { Color.FromArgb(232, 232, 240), Color.FromArgb(232, 232, 240), Color.FromArgb(232, 232, 240) };
myBlend.Positions = new float[] { 0f, 0.5f, 1f };
LinearGradientBrush lgBrush = new LinearGradientBrush(x2, Color.Black, Color.Black, 90f);
lgBrush.InterpolationColors = myBlend;
g.FillRectangle(lgBrush, x2);
g.DrawRectangle(new Pen(Color.FromArgb(170, 187, 204)), x2);
g.SmoothingMode = SmoothingMode.HighQuality;
Point[] p =
{
new Point(ItemSize.Height - 3, GetTabRect(i).Location.Y + 20),
new Point(ItemSize.Height + 4, GetTabRect(i).Location.Y + 14),
new Point(ItemSize.Height + 4, GetTabRect(i).Location.Y + 27)
};
g.FillPolygon(SystemBrushes.Control, p);
g.DrawPolygon(new Pen(Color.FromArgb(170, 187, 204)), p);
if (ImageList != null)
{
try
{
g.DrawImage(ImageList.Images[TabPages[i].ImageIndex],
new Point(x2.Location.X + 8, x2.Location.Y + 6));
g.DrawString(" " + TabPages[i].Text, Font, Brushes.Black, x2, new StringFormat
{
LineAlignment = StringAlignment.Center,
Alignment = StringAlignment.Center
});
}
catch (Exception)
{
g.DrawString(TabPages[i].Text, new Font(Font.FontFamily, Font.Size, FontStyle.Bold),
Brushes.Black, x2, new StringFormat
{
LineAlignment = StringAlignment.Center,
Alignment = StringAlignment.Center
});
}
}
else
{
g.DrawString(TabPages[i].Text, new Font(Font.FontFamily, Font.Size, FontStyle.Bold),
Brushes.Black, x2, new StringFormat
{
LineAlignment = StringAlignment.Center,
Alignment = StringAlignment.Center
});
}
g.DrawLine(new Pen(Color.FromArgb(200, 200, 250)), new Point(x2.Location.X - 1, x2.Location.Y - 1),
new Point(x2.Location.X, x2.Location.Y));
g.DrawLine(new Pen(Color.FromArgb(200, 200, 250)), new Point(x2.Location.X - 1, x2.Bottom - 1),
new Point(x2.Location.X, x2.Bottom));
}
else
{
Rectangle x2 = new Rectangle(new Point(GetTabRect(i).Location.X - 2, GetTabRect(i).Location.Y - 2),
new Size(GetTabRect(i).Width + 3, GetTabRect(i).Height - 1));
g.FillRectangle(new SolidBrush(Color.FromArgb(246, 248, 252)), x2);
g.DrawLine(new Pen(Color.FromArgb(170, 187, 204)), new Point(x2.Right, x2.Top),
new Point(x2.Right, x2.Bottom));
if (ImageList != null)
{
try
{
g.DrawImage(ImageList.Images[TabPages[i].ImageIndex],
new Point(x2.Location.X + 8, x2.Location.Y + 6));
g.DrawString(" " + TabPages[i].Text, Font, Brushes.DimGray, x2, new StringFormat
{
LineAlignment = StringAlignment.Center,
Alignment = StringAlignment.Center
});
}
catch (Exception)
{
g.DrawString(TabPages[i].Text, Font, Brushes.DimGray, x2, new StringFormat
{
LineAlignment = StringAlignment.Center,
Alignment = StringAlignment.Center
});
}
}
else
{
g.DrawString(TabPages[i].Text, Font, Brushes.DimGray, x2, new StringFormat
{
LineAlignment = StringAlignment.Center,
Alignment = StringAlignment.Center
});
}
}
}
e.Graphics.DrawImage(b, new Point(0, 0));
g.Dispose();
b.Dispose();
}
}
}
@@ -0,0 +1,82 @@
using System;
using System.Collections.Generic;
namespace Quasar.Server.Controls.HexEditor
{
public class ByteCollection
{
private List<byte> _bytes;
#region Properties
public int Length
{
get { return _bytes.Count; }
}
#endregion
#region Constructor
public ByteCollection()
{
_bytes = new List<byte>();
}
public ByteCollection(byte[] bytes)
{
_bytes = new List<byte>(bytes);
}
#endregion
#region Methods
public void Add(byte item)
{
_bytes.Add(item);
}
public void Insert(int index, byte item)
{
_bytes.Insert(index, item);
}
public void Remove(byte item)
{
_bytes.Remove(item);
}
public void RemoveAt(int index)
{
_bytes.RemoveAt(index);
}
public void RemoveRange(int startIndex, int count)
{
_bytes.RemoveRange(startIndex, count);
}
public byte GetAt(int index)
{
return _bytes[index];
}
public void SetAt(int index, byte item)
{
_bytes[index] = item;
}
public char GetCharAt(int index)
{
return Convert.ToChar(_bytes[index]);
}
public byte[] ToArray()
{
return _bytes.ToArray();
}
#endregion
}
}
+232
View File
@@ -0,0 +1,232 @@
using System;
using System.Drawing;
using System.Runtime.InteropServices;
namespace Quasar.Server.Controls.HexEditor
{
public class Caret
{
#region Field
/// <summary>
/// Contains the start index
/// where the caret started
/// </summary>
int _startIndex;
/// <summary>
/// Contains the end index
/// where the caret is
/// currently located
/// </summary>
int _endIndex;
/// <summary>
/// Tells if the given caret
/// is active in the controller
/// (control is in focus)
/// </summary>
bool _isCaretActive;
/// <summary>
/// Tells if the caret is
/// currently hidden or
/// not (out of view)
/// </summary>
bool _isCaretHidden;
/// <summary>
/// Holds the actual position
/// of the caret
/// </summary>
Point _location;
private HexEditor _editor;
#endregion
#region Properties
public int SelectionStart
{
get
{
if (_endIndex < _startIndex)
return _endIndex;
return _startIndex;
}
}
public int SelectionLength
{
get
{
if (_endIndex < _startIndex)
return _startIndex - _endIndex;
return _endIndex - _startIndex;
}
}
public bool Focused
{
get { return _isCaretActive; }
}
public int CurrentIndex
{
get { return _endIndex; }
}
public Point Location
{
get { return _location; }
}
#endregion
#region EventHandlers
public event EventHandler SelectionStartChanged;
public event EventHandler SelectionLengthChanged;
#endregion
#region Constructor
public Caret(HexEditor editor)
{
_editor = editor;
_isCaretActive = false;
_startIndex = 0;
_endIndex = 0;
_isCaretHidden = true;
_location = new Point(0, 0);
}
#endregion
#region Methods
#region Caret
private bool Create(IntPtr hWHandler)
{
if (!_isCaretActive)
{
_isCaretActive = true;
return CreateCaret(hWHandler, IntPtr.Zero, 0, (int)_editor.CharSize.Height - 2);
}
return false;
}
private bool Show(IntPtr hWnd)
{
if (_isCaretActive)
{
_isCaretHidden = false;
return ShowCaret(hWnd);
}
return false;
}
public bool Hide(IntPtr hWnd)
{
if (_isCaretActive && !_isCaretHidden)
{
_isCaretHidden = true;
return HideCaret(hWnd);
}
return false;
}
public bool Destroy()
{
if (_isCaretActive)
{
_isCaretActive = false;
DeSelect();
DestroyCaret();
}
return false;
}
#endregion
public void SetStartIndex(int index)
{
_startIndex = index;
_endIndex = _startIndex;
if (SelectionStartChanged != null)
SelectionStartChanged(this, EventArgs.Empty);
if (SelectionLengthChanged != null)
SelectionLengthChanged(this, EventArgs.Empty);
}
public void SetEndIndex(int index)
{
_endIndex = index;
if (SelectionStartChanged != null)
SelectionStartChanged(this, EventArgs.Empty);
if (SelectionLengthChanged != null)
SelectionLengthChanged(this, EventArgs.Empty);
}
public void SetCaretLocation(Point start)
{
Create(_editor.Handle);
_location = start;
SetCaretPos(_location.X, _location.Y);
Show(_editor.Handle);
}
public bool IsSelected(int byteIndex)
{
return (SelectionStart <= byteIndex && byteIndex < (SelectionStart + SelectionLength));
}
private void DeSelect()
{
if (_endIndex < _startIndex)
_startIndex = _endIndex;
else
_endIndex = _startIndex;
if (SelectionStartChanged != null)
SelectionStartChanged(this, EventArgs.Empty);
if (SelectionLengthChanged != null)
SelectionLengthChanged(this, EventArgs.Empty);
}
#endregion
#region Caret import
[DllImport("user32.dll", SetLastError = true)]
static extern bool CreateCaret(IntPtr hWnd, IntPtr hBitmap, int nWidth, int nHeight);
[DllImport("user32.dll", SetLastError = true)]
static extern bool DestroyCaret();
[DllImport("user32.dll", SetLastError = true)]
static extern bool SetCaretPos(int x, int y);
[DllImport("user32.dll", SetLastError = true)]
static extern bool ShowCaret(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true)]
static extern bool HideCaret(IntPtr hWnd);
#endregion
}
}
@@ -0,0 +1,177 @@
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Quasar.Server.Controls.HexEditor
{
public class EditView : IKeyMouseEventHandler
{
#region Fields
/// <summary>
/// Contains the handler for the hex
/// view.
/// </summary>
private HexViewHandler _hexView;
/// <summary>
/// Contains the handler for the
/// string view
/// </summary>
private StringViewHandler _stringView;
private HexEditor _editor;
#endregion
#region Contructor
public EditView(HexEditor editor)
{
_editor = editor;
_hexView = new HexViewHandler(editor);
_stringView = new StringViewHandler(editor);
}
#endregion
#region KeyMouseEvent
#region Key
public void OnKeyPress(KeyPressEventArgs e)
{
if (InHexView(_editor.CaretPosX))
{
_hexView.OnKeyPress(e);
}
else
{
_stringView.OnKeyPress(e);
}
}
public void OnKeyDown(KeyEventArgs e)
{
if (InHexView(_editor.CaretPosX))
{
_hexView.OnKeyDown(e);
}
else
{
_stringView.OnKeyDown(e);
}
}
public void OnKeyUp(KeyEventArgs e)
{ /* ... */ }
#endregion
#region Mouse
public void OnMouseDown(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (InHexView(e.X))
{
_hexView.OnMouseDown(e.X, e.Y);
}
else
{
_stringView.OnMouseDown(e.X, e.Y);
}
}
}
public void OnMouseDragged(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (InHexView(e.X))
{
_hexView.OnMouseDragged(e.X, e.Y);
}
else
{
_stringView.OnMouseDragged(e.X, e.Y);
}
}
}
public void OnMouseUp(MouseEventArgs e)
{ /* ... */ }
public void OnMouseDoubleClick(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (InHexView(e.X))
{
_hexView.OnMouseDoubleClick();
}
else
{
_stringView.OnMouseDoubleClick();
}
}
}
#endregion
#region Focus
public void OnGotFocus(EventArgs e)
{
if (InHexView(_editor.CaretPosX))
_hexView.Focus();
else
_stringView.Focus();
}
#endregion
#endregion
#region UpdateActions
public void SetLowerCase()
{
_hexView.SetLowerCase();
}
public void SetUpperCase()
{
_hexView.SetUpperCase();
}
public void Update(int startPositionX, Rectangle area)
{
_hexView.Update(startPositionX, area);
_stringView.Update(_hexView.MaxWidth, area);
}
#endregion
#region PaintActions
public void Paint(Graphics g, int startIndex, int endIndex)
{
for (int i = 0; (i + startIndex) < endIndex; i++)
{
_hexView.Paint(g, i, startIndex);
_stringView.Paint(g, i, startIndex);
}
}
#endregion
#region Misc
private bool InHexView(int x)
{
return (x < (_hexView.MaxWidth + _editor.EntityMargin - 2));
}
#endregion
}
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,442 @@
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Quasar.Server.Controls.HexEditor
{
public class HexViewHandler
{
#region Fields
bool _isEditing;
/// <summary>
/// Contains info about how to
/// present the hex values
/// (Upper or Lower case)
/// </summary>
string _hexType = "X2";
/// <summary>
/// Contains the boundary for one single
/// hexa value that is visible
/// </summary>
Rectangle _recHexValue;
/// <summary>
/// Contains the format of the hexadecimal
/// strings that are presented
/// </summary>
StringFormat _stringFormat;
private HexEditor _editor;
#endregion
#region Properties
public int MaxWidth
{
get { return _recHexValue.X + (_recHexValue.Width * _editor.BytesPerLine); }
}
#endregion
#region Constructor
public HexViewHandler(HexEditor editor)
{
_editor = editor;
//Set String format for the hex values
_stringFormat = new StringFormat(StringFormat.GenericTypographic);
_stringFormat.Alignment = StringAlignment.Center;
_stringFormat.LineAlignment = StringAlignment.Center;
}
#endregion
#region Method
#region KeyMouseEvents
#region KeyEvents
public void OnKeyPress(KeyPressEventArgs e)
{
if (IsHex(e.KeyChar))
HandleUserInput(e.KeyChar);
}
public void OnKeyDown(KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete || e.KeyCode == Keys.Back)
{
if (_editor.SelectionLength > 0)
{
//Remove the selected bytes
HandleUserRemove();
int index = _editor.CaretIndex;
Point newLocation = GetCaretLocation(index);
_editor.SetCaretStart(index, newLocation);
}
else if (_editor.CaretIndex < _editor.LastVisibleByte && e.KeyCode == Keys.Delete)
{
//Remove the byte after the caret
_editor.RemoveByteAt(_editor.CaretIndex);
Point newLocation = GetCaretLocation(_editor.CaretIndex);
_editor.SetCaretStart(_editor.CaretIndex, newLocation);
}
else if (_editor.CaretIndex > 0 && e.KeyCode == Keys.Back)
{
//Remove byte before the caret
int index = _editor.CaretIndex - 1;
if (_isEditing)
{
//Remove the byte that is being edited
index = _editor.CaretIndex;
}
_editor.RemoveByteAt(index);
Point newLocation = GetCaretLocation(index);
_editor.SetCaretStart(index, newLocation);
}
_isEditing = false;
}
else if (e.KeyCode == Keys.Up && (_editor.CaretIndex - _editor.BytesPerLine) >= 0)
{
int index = _editor.CaretIndex - _editor.BytesPerLine;
//Check ig caret is att the end of the line
if (index % _editor.BytesPerLine == 0 && _editor.CaretPosX >= _recHexValue.X + _recHexValue.Width * _editor.BytesPerLine)
{
Point position = new Point(_editor.CaretPosX, _editor.CaretPosY - _recHexValue.Height);
//check that this is not the last row (nothing above)
if (index == 0)
{
//Last row do not change index and position
position = new Point(_editor.CaretPosX, _editor.CaretPosY);
index = _editor.BytesPerLine;
}
if (e.Shift)
_editor.SetCaretEnd(index, position);
else
_editor.SetCaretStart(index, position);
_isEditing = false;
}
else
{
HandleArrowKeys(index, e.Shift);
}
}
else if (e.KeyCode == Keys.Down && (_editor.CaretIndex - 1) / _editor.BytesPerLine < _editor.HexTableLength / _editor.BytesPerLine)
{
int index = _editor.CaretIndex + _editor.BytesPerLine;
if (index > _editor.HexTableLength)
{
index = _editor.HexTableLength;
HandleArrowKeys(index, e.Shift);
}
else
{
Point position = new Point(_editor.CaretPosX, _editor.CaretPosY + _recHexValue.Height);
if (e.Shift)
_editor.SetCaretEnd(index, position);
else
_editor.SetCaretStart(index, position);
_isEditing = false;
}
}
else if (e.KeyCode == Keys.Left && (_editor.CaretIndex - 1) >= 0)
{
int index = _editor.CaretIndex - 1;
HandleArrowKeys(index, e.Shift);
}
else if (e.KeyCode == Keys.Right && (_editor.CaretIndex + 1) <= _editor.HexTableLength)
{
int index = _editor.CaretIndex + 1;
HandleArrowKeys(index, e.Shift);
}
}
public void HandleArrowKeys(int index, bool isShiftDown)
{
Point position = GetCaretLocation(index);
if (isShiftDown)
_editor.SetCaretEnd(index, position);
else
_editor.SetCaretStart(index, position);
_isEditing = false;
}
#endregion
#region MouseEvent
public void OnMouseDown(int x, int y)
{
int iX = (x - _recHexValue.X) / _recHexValue.Width;
int iY = (y - _recHexValue.Y) / _recHexValue.Height;
//Check that values are good
iX = iX > _editor.BytesPerLine ? _editor.BytesPerLine : iX;
iX = iX < 0 ? 0 : iX;
iY = iY > _editor.MaxBytesV ? _editor.MaxBytesV : iY;
iY = iY < 0 ? 0 : iY;
//Make sure values are withing the given bounds
if ((_editor.LastVisibleByte - _editor.FirstVisibleByte) / _editor.BytesPerLine <= iY)
{
//Check that column is not greater than max
if ((_editor.LastVisibleByte - _editor.FirstVisibleByte) % _editor.BytesPerLine <= iX)
{
iX = (_editor.LastVisibleByte - _editor.FirstVisibleByte) % _editor.BytesPerLine;
}
iY = (_editor.LastVisibleByte - _editor.FirstVisibleByte) / _editor.BytesPerLine;
}
//Get the smallest possible location (do not want to exceed the max)
int index = Math.Min(_editor.LastVisibleByte, _editor.FirstVisibleByte + iX + (iY * _editor.BytesPerLine));
int xPos = (iX * _recHexValue.Width) + _recHexValue.X;
int yPos = (iY * _recHexValue.Height) + _recHexValue.Y;
_editor.SetCaretStart(index, new Point(xPos, yPos));
_isEditing = false;
}
public void OnMouseDragged(int x, int y)
{
int iX = (x - _recHexValue.X) / _recHexValue.Width;
int iY = (y - _recHexValue.Y) / _recHexValue.Height;
//Check that values are good
iX = iX > _editor.BytesPerLine ? _editor.BytesPerLine : iX;
iX = iX < 0 ? 0 : iX;
iY = iY > _editor.MaxBytesV ? _editor.MaxBytesV : iY;
if (_editor.FirstVisibleByte > 0)
{
iY = iY < 0 ? -1 : iY;
}
else
{
iY = iY < 0 ? 0 : iY;
}
//Make sure values are withing the given bounds
if ((_editor.LastVisibleByte - _editor.FirstVisibleByte) / _editor.BytesPerLine <= iY)
{
//Check that column is not greater than max
if ((_editor.LastVisibleByte - _editor.FirstVisibleByte) % _editor.BytesPerLine <= iX)
{
iX = (_editor.LastVisibleByte - _editor.FirstVisibleByte) % _editor.BytesPerLine;
}
iY = (_editor.LastVisibleByte - _editor.FirstVisibleByte) / _editor.BytesPerLine;
}
//Get the smallest possible location (do not want to exceed the max)
int index = Math.Min(_editor.LastVisibleByte, _editor.FirstVisibleByte + iX + (iY * _editor.BytesPerLine));
int xPos = (iX * _recHexValue.Width) + _recHexValue.X;
int yPos = (iY * _recHexValue.Height) + _recHexValue.Y;
_editor.SetCaretEnd(index, new Point(xPos, yPos));
}
public void OnMouseDoubleClick()
{
if (_editor.CaretIndex < _editor.LastVisibleByte)
{
int index = _editor.CaretIndex + 1;
Point newLocation = GetCaretLocation(index);
_editor.SetCaretEnd(index, newLocation);
}
}
#endregion
#endregion
#region PaintMethod
public void Update(int startPositionX, Rectangle area)
{
_recHexValue = new Rectangle(
startPositionX,
area.Y,
(int)(_editor.CharSize.Width * 3),
(int)(_editor.CharSize.Height) - 2
);
_recHexValue.X += _editor.EntityMargin;
}
public void Paint(Graphics g, int index, int startIndex)
{
Point columnAndRow = GetByteColumnAndRow(index);
if (_editor.IsSelected(index + startIndex))
{
PaintByteAsSelected(g, columnAndRow, (index + startIndex));
}
else
{
PaintByte(g, columnAndRow, (index + startIndex));
}
}
private void PaintByteAsSelected(Graphics g, Point point, int index)
{
SolidBrush backBrush = new SolidBrush(_editor.SelectionBackColor);
SolidBrush textBrush = new SolidBrush(_editor.SelectionForeColor);
RectangleF drawSurface = GetBound(point);
string hexValue = _editor.GetByte(index).ToString(_hexType);
g.FillRectangle(backBrush, drawSurface);
g.DrawString(hexValue, _editor.Font, textBrush, drawSurface, _stringFormat);
}
private void PaintByte(Graphics g, Point point, int index)
{
SolidBrush brush = new SolidBrush(_editor.ForeColor);
RectangleF drawSurface = GetBound(point);
string hexValue = _editor.GetByte(index).ToString(_hexType);
g.DrawString(hexValue, _editor.Font, brush, drawSurface, _stringFormat);
}
#endregion
public void SetLowerCase()
{
_hexType = "x2";
}
public void SetUpperCase()
{
_hexType = "X2";
}
public void Focus()
{
int index = _editor.CaretIndex;
Point location = GetCaretLocation(index);
_editor.SetCaretStart(index, location);
}
#endregion
#region Caret
/// <summary>
/// Get the caret current location
/// in the given bound.
/// </summary>
private Point GetCaretLocation(int index)
{
int xPos = _recHexValue.X + (_recHexValue.Width * (index % _editor.BytesPerLine));
int yPos = _recHexValue.Y + (_recHexValue.Height * ((index - (_editor.FirstVisibleByte + index % _editor.BytesPerLine)) / _editor.BytesPerLine));
Point ret = new Point(xPos, yPos);
return ret;
}
#endregion
#region Misc
private void HandleUserRemove()
{
//Calculate where to position the caret after the removal
int index = _editor.SelectionStart;
Point position = GetCaretLocation(index);
//Remove all of the selected bytes
_editor.RemoveSelectedBytes();
//Set the new position of the caret
_editor.SetCaretStart(index, position);
}
private void HandleUserInput(char key)
{
if (!_editor.CaretFocused)
return;
//Perform overwrite
HandleUserRemove();
if (_isEditing)
{
//Editing has already started, should change the second nibble
_isEditing = false;
//Load old bytes to allow change
byte oldByte = _editor.GetByte(_editor.CaretIndex);
//Append the new nibble
oldByte += Convert.ToByte(key.ToString(), 16);
_editor.SetByte(_editor.CaretIndex, oldByte);
//Relocate the caret
int index = _editor.CaretIndex + 1;
Point newLocation = GetCaretLocation(index);
_editor.SetCaretStart(index, newLocation);
}
else
{
//Begin new edit phase
_isEditing = true;
string hexByte = key.ToString() + "0";
byte newByte = Convert.ToByte(hexByte, 16);
if (_editor.HexTable.Length <= 0)
{
_editor.AppendByte(newByte);
}
else
{
_editor.InsertByte(_editor.CaretIndex, newByte);
}
//Relocate the caret to the middle of the hex value (provide illusion of editing the second value)
int xPos = (_recHexValue.X + (_recHexValue.Width * ((_editor.CaretIndex) % _editor.BytesPerLine)) + (_recHexValue.Width / 2));
int yPos = _recHexValue.Y + (_recHexValue.Height * ((_editor.CaretIndex - (_editor.FirstVisibleByte + _editor.CaretIndex % _editor.BytesPerLine)) / _editor.BytesPerLine));
_editor.SetCaretStart(_editor.CaretIndex, new Point(xPos, yPos));
}
}
private Point GetByteColumnAndRow(int index)
{
int column = index % _editor.BytesPerLine;
int row = index / _editor.BytesPerLine;
Point ret = new Point(column, row);
return ret;
}
private RectangleF GetBound(Point point)
{
RectangleF ret = new RectangleF(
_recHexValue.X + (point.X * _recHexValue.Width),
_recHexValue.Y + (point.Y * _recHexValue.Height),
_recHexValue.Width,
_recHexValue.Height
);
return ret;
}
private bool IsHex(char c)
{
return (c >= 'a' && c <= 'f') ||
(c >= 'A' && c <= 'F') ||
Char.IsDigit(c);
}
#endregion
}
}
@@ -0,0 +1,24 @@
using System;
using System.Windows.Forms;
namespace Quasar.Server.Controls.HexEditor
{
public interface IKeyMouseEventHandler
{
void OnKeyPress(KeyPressEventArgs e);
void OnKeyDown(KeyEventArgs e);
void OnKeyUp(KeyEventArgs e);
void OnMouseDown(MouseEventArgs e);
void OnMouseDragged(MouseEventArgs e);
void OnMouseUp(MouseEventArgs e);
void OnMouseDoubleClick(MouseEventArgs e);
void OnGotFocus(EventArgs e);
}
}
@@ -0,0 +1,382 @@
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Quasar.Server.Controls.HexEditor
{
public class StringViewHandler
{
#region Field
/// <summary>
/// Contains the boundary of
/// a single line
/// </summary>
Rectangle _recStringView;
/// <summary>
/// Contains the format of the
/// string to be used in the
/// string view
/// </summary>
StringFormat _stringFormat;
private HexEditor _editor;
#endregion
#region Properties
public int MaxWidth
{
get { return _recStringView.X + _recStringView.Width; }
}
#endregion
#region Constructor
public StringViewHandler(HexEditor editor)
{
_editor = editor;
//Set String format for the values
_stringFormat = new StringFormat(StringFormat.GenericTypographic);
_stringFormat.Alignment = StringAlignment.Center;
_stringFormat.LineAlignment = StringAlignment.Center;
}
#endregion
#region KeyMouseEvents
#region Key
public void OnKeyPress(KeyPressEventArgs e)
{
if (!Char.IsControl(e.KeyChar))
{
HandleUserInput(e.KeyChar);
}
}
public void OnKeyDown(KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete || e.KeyCode == Keys.Back)
{
if (_editor.SelectionLength > 0)
{
//Remove the selected bytes
HandleUserRemove();
int index = _editor.CaretIndex;
Point newLocation = GetCaretLocation(index);
_editor.SetCaretStart(index, newLocation);
}
else if (_editor.CaretIndex < _editor.LastVisibleByte && e.KeyCode == Keys.Delete)
{
//Remove the byte after the caret
_editor.RemoveByteAt(_editor.CaretIndex);
Point newLocation = GetCaretLocation(_editor.CaretIndex);
_editor.SetCaretStart(_editor.CaretIndex, newLocation);
}
else if (_editor.CaretIndex > 0 && e.KeyCode == Keys.Back)
{
//Remove byte before the caret
int index = _editor.CaretIndex - 1;
_editor.RemoveByteAt(index);
Point newLocation = GetCaretLocation(index);
_editor.SetCaretStart(index, newLocation);
}
}
else if (e.KeyCode == Keys.Up && (_editor.CaretIndex - _editor.BytesPerLine) >= 0)
{
int index = _editor.CaretIndex - _editor.BytesPerLine;
//Check ig caret is att the end of the line
if (index % _editor.BytesPerLine == 0 && _editor.CaretPosX >= _recStringView.X + _recStringView.Width)
{
Point position = new Point(_editor.CaretPosX, _editor.CaretPosY - _recStringView.Height);
//check that this is not the last row (nothing above)
if (index == 0)
{
//Last row do not change index and position
position = new Point(_editor.CaretPosX, _editor.CaretPosY);
index = _editor.BytesPerLine;
}
if (e.Shift)
_editor.SetCaretEnd(index, position);
else
_editor.SetCaretStart(index, position);
}
else
{
HandleArrowKeys(index, e.Shift);
}
}
else if (e.KeyCode == Keys.Down && (_editor.CaretIndex - 1) / _editor.BytesPerLine < _editor.HexTableLength / _editor.BytesPerLine)
{
int index = _editor.CaretIndex + _editor.BytesPerLine;
if (index > _editor.HexTableLength)
{
index = _editor.HexTableLength;
HandleArrowKeys(index, e.Shift);
}
else
{
Point position = new Point(_editor.CaretPosX, _editor.CaretPosY + _recStringView.Height);
if (e.Shift)
_editor.SetCaretEnd(index, position);
else
_editor.SetCaretStart(index, position);
}
}
else if (e.KeyCode == Keys.Left && (_editor.CaretIndex - 1) >= 0)
{
int index = _editor.CaretIndex - 1;
HandleArrowKeys(index, e.Shift);
}
else if (e.KeyCode == Keys.Right && (_editor.CaretIndex + 1) <= _editor.LastVisibleByte)
{
int index = _editor.CaretIndex + 1;
HandleArrowKeys(index, e.Shift);
}
}
public void HandleArrowKeys(int index, bool isShiftDown)
{
Point newLocation = GetCaretLocation(index);
if (isShiftDown)
_editor.SetCaretEnd(index, newLocation);
else
_editor.SetCaretStart(index, newLocation);
}
#endregion
#region Mouse
public void OnMouseDown(int x, int y)
{
int iX = (x - _recStringView.X) / (int)_editor.CharSize.Width;
int iY = (y - _recStringView.Y) / _recStringView.Height;
//Check that values are good
iX = iX > _editor.BytesPerLine ? _editor.BytesPerLine : iX;
iX = iX < 0 ? 0 : iX;
iY = iY > _editor.MaxBytesV ? _editor.MaxBytesV : iY;
iY = iY < 0 ? 0 : iY;
//Make sure values are withing the given bounds
if ((_editor.LastVisibleByte - _editor.FirstVisibleByte) / _editor.BytesPerLine <= iY)
{
//Check that column is not greater than max
if ((_editor.LastVisibleByte - _editor.FirstVisibleByte) % _editor.BytesPerLine <= iX)
{
iX = (_editor.LastVisibleByte - _editor.FirstVisibleByte) % _editor.BytesPerLine;
}
iY = (_editor.LastVisibleByte - _editor.FirstVisibleByte) / _editor.BytesPerLine;
}
//Get the smallest possible location (do not want to exceed the max)
int index = Math.Min(_editor.LastVisibleByte, _editor.FirstVisibleByte + iX + iY * _editor.BytesPerLine);
int xPos = (iX * (int)_editor.CharSize.Width) + _recStringView.X;
int yPos = (iY * _recStringView.Height) + _recStringView.Y;
_editor.SetCaretStart(index, new Point(xPos, yPos));
}
public void OnMouseDragged(int x, int y)
{
int iX = (x - _recStringView.X) / (int)_editor.CharSize.Width;
int iY = (y - _recStringView.Y) / _recStringView.Height;
//Check that values are good
iX = iX > _editor.BytesPerLine ? _editor.BytesPerLine : iX;
iX = iX < 0 ? 0 : iX;
iY = iY > _editor.MaxBytesV ? _editor.MaxBytesV : iY;
if (_editor.FirstVisibleByte > 0)
{
iY = iY < 0 ? -1 : iY;
}
else
{
iY = iY < 0 ? 0 : iY;
}
//Make sure values are withing the given bounds
if ((_editor.LastVisibleByte - _editor.FirstVisibleByte) / _editor.BytesPerLine <= iY)
{
//Check that column is not greater than max
if ((_editor.LastVisibleByte - _editor.FirstVisibleByte) % _editor.BytesPerLine <= iX)
{
iX = (_editor.LastVisibleByte - _editor.FirstVisibleByte) % _editor.BytesPerLine;
}
iY = (_editor.LastVisibleByte - _editor.FirstVisibleByte) / _editor.BytesPerLine;
}
//Get the smallest possible location (do not want to exceed the max)
int index = Math.Min(_editor.LastVisibleByte, _editor.FirstVisibleByte + iX + iY * _editor.BytesPerLine);
int xPos = (iX * (int)_editor.CharSize.Width) + _recStringView.X;
int yPos = (iY * _recStringView.Height) + _recStringView.Y;
_editor.SetCaretEnd(index, new Point(xPos, yPos));
}
public void OnMouseDoubleClick()
{
if (_editor.CaretIndex < _editor.LastVisibleByte)
{
int index = _editor.CaretIndex + 1;
Point newLocation = GetCaretLocation(index);
_editor.SetCaretEnd(index, newLocation);
}
}
#endregion
#region Focus
public void Focus()
{
int index = _editor.CaretIndex;
Point location = GetCaretLocation(index);
_editor.SetCaretStart(index, location);
}
#endregion
#endregion
#region Paint
public void Update(int startPositionX, Rectangle area)
{
_recStringView = new Rectangle(
startPositionX,
area.Y,
(int)(_editor.CharSize.Width * _editor.BytesPerLine),
(int)(_editor.CharSize.Height) - 2
);
_recStringView.X += _editor.EntityMargin;
}
public void Paint(Graphics g, int index, int startIndex)
{
Point columnAndRow = GetByteColumnAndRow(index);
if (_editor.IsSelected(index + startIndex))
{
PaintByteAsSelected(g, columnAndRow, (index + startIndex));
}
else
{
PaintByte(g, columnAndRow, (index + startIndex));
}
}
private void PaintByteAsSelected(Graphics g, Point point, int index)
{
SolidBrush backBrush = new SolidBrush(_editor.SelectionBackColor);
SolidBrush textBrush = new SolidBrush(_editor.SelectionForeColor);
RectangleF drawSurface = GetBound(point);
char value = _editor.GetByteAsChar(index);
string strValue = (Char.IsControl(value) ? "." : value.ToString());
g.FillRectangle(backBrush, drawSurface);
g.DrawString(strValue, _editor.Font, textBrush, drawSurface, _stringFormat);
}
private void PaintByte(Graphics g, Point point, int index)
{
SolidBrush brush = new SolidBrush(_editor.ForeColor);
RectangleF drawLocation = GetBound(point);
char value = _editor.GetByteAsChar(index);
string strValue = (Char.IsControl(value) ? "." : value.ToString());
g.DrawString(strValue, _editor.Font, brush, drawLocation, _stringFormat);
}
#endregion
#region Caret
/// <summary>
/// Get the caret current location
/// in the given bound.
/// </summary>
private Point GetCaretLocation(int index)
{
int xPos = _recStringView.X + ((int)_editor.CharSize.Width * (index % _editor.BytesPerLine));
int yPos = _recStringView.Y + ((int)_recStringView.Height * ((index - (_editor.FirstVisibleByte + index % _editor.BytesPerLine)) / _editor.BytesPerLine));
Point ret = new Point(xPos, yPos);
return ret;
}
#endregion
#region Misc
private void HandleUserRemove()
{
//Calculate where to position the caret after the removal
int index = _editor.SelectionStart;
Point position = GetCaretLocation(index);
//Remove all of the selected bytes
_editor.RemoveSelectedBytes();
//Set the new position of the caret
_editor.SetCaretStart(index, position);
}
private void HandleUserInput(char key)
{
if (!_editor.CaretFocused)
return;
HandleUserRemove();
byte newByte = Convert.ToByte(key);
if (_editor.HexTableLength <= 0)
_editor.AppendByte(newByte);
else
_editor.InsertByte(_editor.CaretIndex, newByte);
int index = _editor.CaretIndex + 1;
Point newLocation = GetCaretLocation(index);
_editor.SetCaretStart(index, newLocation);
}
private Point GetByteColumnAndRow(int index)
{
int column = index % _editor.BytesPerLine;
int row = index / _editor.BytesPerLine;
Point ret = new Point(column, row);
return ret;
}
private RectangleF GetBound(Point point)
{
RectangleF ret = new RectangleF(
_recStringView.X + (point.X * (int)_editor.CharSize.Width),
_recStringView.Y + (point.Y * _recStringView.Height),
_editor.CharSize.Width,
_recStringView.Height
);
return ret;
}
#endregion
}
}
+54
View File
@@ -0,0 +1,54 @@
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Quasar.Server.Controls
{
public static class InputBox
{
public static DialogResult Show(string title, string promptText, ref string value)
{
DialogResult dialogResult = DialogResult.Cancel;
using (var form = new Form())
{
Label label = new Label();
TextBox textBox = new TextBox();
Button buttonOk = new Button();
Button buttonCancel = new Button();
form.Text = title;
label.Text = promptText;
textBox.Text = value;
buttonOk.Text = "OK";
buttonCancel.Text = "Cancel";
buttonOk.DialogResult = DialogResult.OK;
buttonCancel.DialogResult = DialogResult.Cancel;
label.SetBounds(9, 20, 372, 13);
textBox.SetBounds(12, 36, 372, 20);
buttonOk.SetBounds(228, 72, 75, 23);
buttonCancel.SetBounds(309, 72, 75, 23);
label.AutoSize = true;
textBox.Anchor = textBox.Anchor | AnchorStyles.Right;
buttonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
form.ClientSize = new Size(396, 107);
form.Controls.AddRange(new Control[] {label, textBox, buttonOk, buttonCancel});
form.ClientSize = new Size(Math.Max(300, label.Right + 10), form.ClientSize.Height);
form.FormBorderStyle = FormBorderStyle.FixedDialog;
form.StartPosition = FormStartPosition.CenterScreen;
form.MinimizeBox = false;
form.MaximizeBox = false;
form.AcceptButton = buttonOk;
form.CancelButton = buttonCancel;
dialogResult = form.ShowDialog();
value = textBox.Text;
}
return dialogResult;
}
}
}
+28
View File
@@ -0,0 +1,28 @@
using System.Drawing;
using System.Windows.Forms;
namespace Quasar.Server.Controls
{
public class Line : Control
{
public enum Alignment
{
Horizontal,
Vertical
}
public Alignment LineAlignment { get; set; }
public Line()
{
this.TabStop = false;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.DrawLine(new Pen(new SolidBrush(Color.LightGray)), new Point(5, 5),
LineAlignment == Alignment.Horizontal ? new Point(500, 5) : new Point(5, 500));
}
}
}
+82
View File
@@ -0,0 +1,82 @@
using Quasar.Common.Helpers;
using Quasar.Server.Helper;
using Quasar.Server.Utilities;
using System;
using System.Windows.Forms;
namespace Quasar.Server.Controls
{
internal class AeroListView : ListView
{
private const uint WM_CHANGEUISTATE = 0x127;
private const short UIS_SET = 1;
private const short UISF_HIDEFOCUS = 0x1;
private readonly IntPtr _removeDots = new IntPtr(NativeMethodsHelper.MakeWin32Long(UIS_SET, UISF_HIDEFOCUS));
public ListViewColumnSorter LvwColumnSorter { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="AeroListView"/> class.
/// </summary>
public AeroListView()
{
SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
this.LvwColumnSorter = new ListViewColumnSorter();
this.ListViewItemSorter = LvwColumnSorter;
this.View = View.Details;
this.FullRowSelect = true;
}
/// <summary>
/// Raises the <see cref="E:HandleCreated" /> event.
/// </summary>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
if (PlatformHelper.RunningOnMono) return;
if (PlatformHelper.VistaOrHigher)
{
// set window theme to explorer
NativeMethods.SetWindowTheme(this.Handle, "explorer", null);
}
if (PlatformHelper.XpOrHigher)
{
// removes the ugly dotted line around focused item
NativeMethods.SendMessage(this.Handle, WM_CHANGEUISTATE, _removeDots, IntPtr.Zero);
}
}
/// <summary>
/// Raises the <see cref="E:ColumnClick" /> event.
/// </summary>
/// <param name="e">The <see cref="ColumnClickEventArgs"/> instance containing the event data.</param>
protected override void OnColumnClick(ColumnClickEventArgs e)
{
base.OnColumnClick(e);
// Determine if clicked column is already the column that is being sorted.
if (e.Column == this.LvwColumnSorter.SortColumn)
{
// Reverse the current sort direction for this column.
this.LvwColumnSorter.Order = (this.LvwColumnSorter.Order == SortOrder.Ascending)
? SortOrder.Descending
: SortOrder.Ascending;
}
else
{
// Set the column number that is to be sorted; default to ascending.
this.LvwColumnSorter.SortColumn = e.Column;
this.LvwColumnSorter.Order = SortOrder.Ascending;
}
// Perform the sort with these new sort options.
if (!this.VirtualMode)
this.Sort();
}
}
}
+189
View File
@@ -0,0 +1,189 @@
using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using Quasar.Server.Utilities;
namespace Quasar.Server.Controls
{
public interface IRapidPictureBox
{
bool Running { get; set; }
Image GetImageSafe { get; set; }
void Start();
void Stop();
void UpdateImage(Bitmap bmp, bool cloneBitmap = false);
}
/// <summary>
/// Custom PictureBox Control designed for rapidly-changing images.
/// </summary>
public class RapidPictureBox : PictureBox, IRapidPictureBox
{
/// <summary>
/// True if the PictureBox is currently streaming images, else False.
/// </summary>
public bool Running { get; set; }
/// <summary>
/// Returns the width of the original screen.
/// </summary>
public int ScreenWidth { get; private set; }
/// <summary>
/// Returns the height of the original screen.
/// </summary>
public int ScreenHeight { get; private set; }
/// <summary>
/// Provides thread-safe access to the Image of this Picturebox.
/// </summary>
public Image GetImageSafe
{
get
{
return Image;
}
set
{
lock (_imageLock)
{
Image = value;
}
}
}
/// <summary>
/// The lock object for the Picturebox's image.
/// </summary>
private readonly object _imageLock = new object();
/// <summary>
/// The Stopwatch for internal FPS measuring.
/// </summary>
private Stopwatch _sWatch;
/// <summary>
/// The internal class for FPS measuring.
/// </summary>
private FrameCounter _frameCounter;
/// <summary>
/// Subscribes an Eventhandler to the FrameUpdated event.
/// </summary>
/// <param name="e">The Eventhandler to set.</param>
public void SetFrameUpdatedEvent(FrameUpdatedEventHandler e)
{
_frameCounter.FrameUpdated += e;
}
/// <summary>
/// Unsubscribes an Eventhandler from the FrameUpdated event.
/// </summary>
/// <param name="e">The Eventhandler to remove.</param>
public void UnsetFrameUpdatedEvent(FrameUpdatedEventHandler e)
{
_frameCounter.FrameUpdated -= e;
}
/// <summary>
/// Starts the internal FPS measuring.
/// </summary>
public void Start()
{
_frameCounter = new FrameCounter();
_sWatch = Stopwatch.StartNew();
Running = true;
}
/// <summary>
/// Stops the internal FPS measuring.
/// </summary>
public void Stop()
{
_sWatch?.Stop();
Running = false;
}
/// <summary>
/// Updates the Image of this Picturebox.
/// </summary>
/// <param name="bmp">The new bitmap to use.</param>
/// <param name="cloneBitmap">If True the bitmap will be cloned, else it uses the original bitmap.</param>
public void UpdateImage(Bitmap bmp, bool cloneBitmap)
{
try
{
CountFps();
if ((ScreenWidth != bmp.Width) && (ScreenHeight != bmp.Height))
UpdateScreenSize(bmp.Width, bmp.Height);
lock (_imageLock)
{
// get old image to dispose it correctly
var oldImage = GetImageSafe;
SuspendLayout();
GetImageSafe = cloneBitmap ? new Bitmap(bmp, Width, Height) /*resize bitmap*/ : bmp;
ResumeLayout();
oldImage?.Dispose();
}
}
catch (InvalidOperationException)
{
}
catch (Exception)
{
}
}
/// <summary>
/// Constructor, sets Picturebox double-buffered and initializes the Framecounter.
/// </summary>
public RapidPictureBox()
{
this.SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
}
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x02000000; // Turn on WS_EX_COMPOSITED
return cp;
}
}
protected override void OnPaint(PaintEventArgs pe)
{
lock (_imageLock)
{
if (GetImageSafe != null)
{
pe.Graphics.DrawImage(GetImageSafe, Location);
}
}
}
private void UpdateScreenSize(int newWidth, int newHeight)
{
ScreenWidth = newWidth;
ScreenHeight = newHeight;
}
private void CountFps()
{
var deltaTime = (float)_sWatch.Elapsed.TotalSeconds;
_sWatch = Stopwatch.StartNew();
_frameCounter.Update(deltaTime);
}
}
}
@@ -0,0 +1,13 @@
using System.Windows.Forms;
namespace Quasar.Server.Controls
{
public class RegistryTreeView : TreeView
{
public RegistryTreeView()
{
//Enable double buffering and ignore WM_ERASEBKGND to reduce flicker
SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
}
}
}
@@ -0,0 +1,72 @@
using System.Windows.Forms;
using Quasar.Common.Models;
using Quasar.Server.Extensions;
using Quasar.Server.Registry;
namespace Quasar.Server.Controls
{
public class RegistryValueLstItem : ListViewItem
{
private string _type { get; set; }
private string _data { get; set; }
public string RegName {
get { return this.Name; }
set
{
this.Name = value;
this.Text = RegValueHelper.GetName(value);
}
}
public string Type {
get { return _type; }
set
{
_type = value;
if (this.SubItems.Count < 2)
this.SubItems.Add(_type);
else
this.SubItems[1].Text = _type;
this.ImageIndex = GetRegistryValueImgIndex(_type);
}
}
public string Data {
get { return _data; }
set
{
_data = value;
if (this.SubItems.Count < 3)
this.SubItems.Add(_data);
else
this.SubItems[2].Text = _data;
}
}
public RegistryValueLstItem(RegValueData value)
{
RegName = value.Name;
Type = value.Kind.RegistryTypeToString();
Data = RegValueHelper.RegistryValueToString(value);
}
private int GetRegistryValueImgIndex(string type)
{
switch (type)
{
case "REG_MULTI_SZ":
case "REG_SZ":
case "REG_EXPAND_SZ":
return 0;
case "REG_BINARY":
case "REG_DWORD":
case "REG_QWORD":
default:
return 1;
}
}
}
}
+36
View File
@@ -0,0 +1,36 @@
namespace Quasar.Server.Controls
{
partial class WordTextBox
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion
}
}
+171
View File
@@ -0,0 +1,171 @@
using System;
using System.Globalization;
using System.Windows.Forms;
using Quasar.Server.Enums;
namespace Quasar.Server.Controls
{
public partial class WordTextBox : TextBox
{
private bool isHexNumber;
private WordType type;
public override int MaxLength
{
get
{
return base.MaxLength;
}
set { }
}
public bool IsHexNumber
{
get { return isHexNumber; }
set
{
if (isHexNumber == value)
return;
if(value)
{
if (Type == WordType.DWORD)
Text = UIntValue.ToString("x");
else
Text = ULongValue.ToString("x");
}
else
{
if (Type == WordType.DWORD)
Text = UIntValue.ToString();
else
Text = ULongValue.ToString();
}
isHexNumber = value;
UpdateMaxLength();
}
}
public WordType Type
{
get { return type; }
set
{
if (type == value)
return;
type = value;
UpdateMaxLength();
}
}
public uint UIntValue
{
get
{
try
{
if (String.IsNullOrEmpty(Text))
return 0;
else if (IsHexNumber)
return UInt32.Parse(Text, NumberStyles.HexNumber);
else
return UInt32.Parse(Text);
}
catch (Exception)
{
return UInt32.MaxValue;
}
}
}
public ulong ULongValue
{
get
{
try
{
if (String.IsNullOrEmpty(Text))
return 0;
else if (IsHexNumber)
return UInt64.Parse(Text, NumberStyles.HexNumber);
else
return UInt64.Parse(Text);
}
catch (Exception)
{
return UInt64.MaxValue;
}
}
}
public bool IsConversionValid()
{
if (String.IsNullOrEmpty(Text))
return true;
if (!IsHexNumber)
{
return ConvertToHex();
}
return true;
}
public WordTextBox()
{
InitializeComponent();
base.MaxLength = 8;
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress(e);
e.Handled = !IsValidChar(e.KeyChar);
}
private bool IsValidChar(char ch)
{
return (Char.IsControl(ch) ||
Char.IsDigit(ch) ||
(IsHexNumber && Char.IsLetter(ch) && Char.ToLower(ch) <= 'f'));
}
private void UpdateMaxLength()
{
if(Type == WordType.DWORD)
{
if (IsHexNumber)
base.MaxLength = 8;
else
base.MaxLength = 10;
}
else
{
if (IsHexNumber)
base.MaxLength = 16;
else
base.MaxLength = 20;
}
}
private bool ConvertToHex()
{
try
{
if (Type == WordType.DWORD)
UInt32.Parse(Text);
else
UInt64.Parse(Text);
return true;
}
catch (Exception)
{
return false;
}
}
}
}