Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
THex Editor - HexBox.vb

HexBox.vb

Caricato da: Totem
Scarica il programma completo

  1. Imports System.ComponentModel
  2. Imports System.Text.RegularExpressions
  3. Public Class HexBox
  4.     Inherits RichTextBox
  5.  
  6.     Public Event SelectedBytesChanged As EventHandler
  7.  
  8.     Private EventsDisabled As Boolean = False
  9.     Private HexValueRegex As New Regex("[ABCDEFabcdef\d]{2}", RegexOptions.Multiline)
  10.  
  11.     Private _Columns As Int16
  12.     Private _Bytes() As Byte
  13.     Private _SelectedByte As Byte
  14.     Private _SelectedBytes() As Byte
  15.  
  16.     Public Property Columns() As Int16
  17.         Get
  18.             Return _Columns
  19.         End Get
  20.         Set(ByVal value As Int16)
  21.             _Columns = value
  22.  
  23.             If (Me.Bytes IsNot Nothing) AndAlso (Me.Bytes.Length > 0) Then
  24.                 Dim Temp() As Byte = Me.Bytes
  25.                 _Bytes = Nothing
  26.                 Me.LoadBytes(Temp)
  27.             End If
  28.         End Set
  29.     End Property
  30.  
  31.     Public ReadOnly Property Bytes() As Byte()
  32.         Get
  33.             Return _Bytes
  34.         End Get
  35.     End Property
  36.  
  37.     <Browsable(False)> _
  38.     Public ReadOnly Property SelectedByte() As Byte
  39.         Get
  40.             Return _SelectedByte
  41.         End Get
  42.     End Property
  43.  
  44.     <Browsable(False)> _
  45.     Public Property SelectedBytes() As Byte()
  46.         Get
  47.             Return _SelectedBytes
  48.         End Get
  49.         Set(ByVal value As Byte())
  50.             If value Is Nothing Then
  51.                 Exit Property
  52.             End If
  53.  
  54.             Dim Matches As MatchCollection = HexValueRegex.Matches(Me.SelectedText)
  55.             Dim Temp(Me.Bytes.Length - Matches.Count + value.Length - 1) As Byte
  56.  
  57.             Array.ConstrainedCopy(Me.Bytes, 0, Temp, 0, Me.CurrentIntOffset)
  58.             Array.ConstrainedCopy(value, 0, Temp, Me.CurrentIntOffset, value.Length)
  59.             Array.ConstrainedCopy(Me.Bytes, Me.CurrentIntOffset + Matches.Count, Temp, Me.CurrentIntOffset + value.Length, Me.Bytes.Length - Me.CurrentIntOffset - Matches.Count)
  60.  
  61.             Me.LoadBytes(Temp)
  62.         End Set
  63.     End Property
  64.  
  65.     Public ReadOnly Property CurrentLineIndex() As Int32
  66.         Get
  67.             Return Me.GetLineFromCharIndex(Me.SelectionStart)
  68.         End Get
  69.     End Property
  70.  
  71.     <Browsable(False)> _
  72.     Public Property CurrentHexOffset() As String
  73.         Get
  74.             Return HexValue.ToHexadecimal(Me.CurrentIntOffset)
  75.         End Get
  76.         Set(ByVal value As String)
  77.             Try
  78.                 Me.SelectionStart = HexValue.ToDecimal(value) * 3
  79.                 Me.SelectionLength = 2
  80.             Catch ex As Exception
  81.  
  82.             End Try
  83.         End Set
  84.     End Property
  85.  
  86.     <Browsable(False)> _
  87.     Public Property CurrentIntOffset() As Int64
  88.         Get
  89.             Return Me.SelectionStart \ 3
  90.         End Get
  91.         Set(ByVal value As Int64)
  92.             Me.SelectionStart = value * 3
  93.             Me.SelectionLength = 2
  94.         End Set
  95.     End Property
  96.  
  97.     <Browsable(False)> _
  98.     Public Property CurrentBinOffset() As BinaryValue.BitArray
  99.         Get
  100.             Return BinaryValue.ToBinary(Me.CurrentIntOffset)
  101.         End Get
  102.         Set(ByVal value As BinaryValue.BitArray)
  103.             Me.SelectionStart = BinaryValue.ToDecimal(value) * 3
  104.             Me.SelectionLength = 2
  105.         End Set
  106.     End Property
  107.  
  108.  
  109.     Sub New()
  110.         Me.Columns = 16
  111.         _Bytes = New Byte() {}
  112.     End Sub
  113.  
  114.  
  115.     Public Sub LoadBytes(ByVal Bytes() As Byte)
  116.         Dim Odd As Boolean = True
  117.         Dim Text As New System.Text.StringBuilder
  118.  
  119.         Me.Text = ""
  120.         For I As Int32 = 0 To Bytes.Length - 1 Step Columns
  121.             Dim Max As Int16
  122.  
  123.             If Bytes.Length - I >= Columns Then
  124.                 Max = Columns - 1
  125.             Else
  126.                 Max = Bytes.Length - I - 1
  127.             End If
  128.  
  129.             For C As Int16 = 0 To Max
  130.                 Text.AppendFormat("{0:X2}", Bytes(I + C))
  131.                 If C < Max Then
  132.                     Text.Append(" ")
  133.                 End If
  134.             Next
  135.  
  136.             If Max = Columns - 1 Then
  137.                 Text.AppendLine()
  138.             End If
  139.         Next
  140.  
  141.         Me.Text = Text.ToString
  142.         _Bytes = Bytes
  143.     End Sub
  144.  
  145.     Public Sub ReloadBytes()
  146.         Me.LoadBytes(Me.Bytes)
  147.     End Sub
  148.  
  149.     Private Sub Me_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
  150.         If EventsDisabled Then
  151.             Exit Sub
  152.         End If
  153.  
  154.         If e.KeyCode = Keys.Right Then
  155.             If Me.SelectionStart >= 0 Then
  156.                 Dim Start As Int32 = ((Me.SelectionStart + 1) \ 3) * 3 + 3
  157.                 Me.Select(Start, 2)
  158.             ElseIf Me.TextLength >= 2 Then
  159.                 Me.Select(0, 2)
  160.             End If
  161.         End If
  162.  
  163.         If e.KeyCode = Keys.Left Then
  164.             If Me.SelectionStart >= 2 Then
  165.                 Dim Start As Int32 = ((Me.SelectionStart + 1) \ 3) * 3 - 3
  166.                 Me.Select(Start, 2)
  167.             ElseIf Me.TextLength >= 2 Then
  168.                 Me.Select(0, 2)
  169.             End If
  170.         End If
  171.  
  172.         If e.KeyCode = Keys.Up Then
  173.             If Me.CurrentLineIndex > 0 Then
  174.                 Dim LinePos As Int32 = ((Me.SelectionStart + 1) \ 3) * 3 - Me.GetFirstCharIndexOfCurrentLine
  175.                 Dim PrevLine As Int32 = Me.CurrentLineIndex - 1
  176.                 Me.Select(Me.GetFirstCharIndexFromLine(PrevLine) + LinePos, 2)
  177.             ElseIf Me.TextLength >= 2 Then
  178.                 Me.Select(0, 2)
  179.             End If
  180.         End If
  181.  
  182.         If e.KeyCode = Keys.Down Then
  183.             If Me.CurrentLineIndex < Me.Lines.Length - 1 Then
  184.                 Dim LinePos As Int32 = ((Me.SelectionStart + 1) \ 3) * 3 - Me.GetFirstCharIndexOfCurrentLine
  185.                 Dim SuccLine As Int32 = Me.CurrentLineIndex + 1
  186.                 Me.Select(Me.GetFirstCharIndexFromLine(SuccLine) + LinePos, 2)
  187.             End If
  188.         End If
  189.  
  190.         Try
  191.             If Me.SelectedText.Length <= 3 Then
  192.                 _SelectedByte = HexValue.ToDecimal(Me.SelectedText)
  193.                 RaiseEvent SelectedBytesChanged(Me, EventArgs.Empty)
  194.             End If
  195.         Catch ex As Exception
  196.  
  197.         End Try
  198.  
  199.         e.SuppressKeyPress = True
  200.         e.Handled = True
  201.     End Sub
  202.  
  203.     Private Sub Me_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.SelectionChanged
  204.         If EventsDisabled Then
  205.             Exit Sub
  206.         End If
  207.  
  208.         Try
  209.             Dim Matches As MatchCollection = HexValueRegex.Matches(Me.SelectedText)
  210.             Dim Bytes(Matches.Count - 1) As Byte
  211.  
  212.             For I As Int32 = 0 To Bytes.Length - 1
  213.                 Bytes(I) = HexValue.ToDecimal(Matches(I).Value)
  214.             Next
  215.             _SelectedBytes = Bytes
  216.  
  217.             RaiseEvent SelectedBytesChanged(Me, EventArgs.Empty)
  218.         Catch ex As Exception
  219.  
  220.         End Try
  221.     End Sub
  222.  
  223.  
  224.     Public Sub DisableEvents()
  225.         EventsDisabled = True
  226.     End Sub
  227.  
  228.     Public Sub EnableEvents()
  229.         EventsDisabled = False
  230.     End Sub
  231. End Class