📄 hexedit.cs
字号:
/*
***************************************************************************
**
** Module: HexEdit.cs
**
** Description:
** CopyRight INCO - Howard Glenn Inman
**
** Revision History:
** ------------------------------------------------------------------------
** Date Name Reason
** 6/24/2005 HGI Initial Creation
**
**
**
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
using Win32API;
namespace HexEdit
{
/*
***************************************************************************
**
** Class: HexEditBase
**
*/
///<summary>
/// Base class for the Hex Edit Pair of boxes
///</summary>
///<remarks>
///</remarks>
///
public class HexEditBase : RichTextBox
{
/*
** Local Variables
*/
///<summary>Flag to prevent linked update</summary>
protected bool m_bNoUpdate = false; // Control the Updates between windows
///<summary>The Controls Contect Menu</summary>
protected ContextMenu m_menuContext = null;
/*
** Constants
*/
///<summary>Constants for Line Index - SDK Call</summary>
protected const int EM_LINEINDEX = 0xbb;
///<summary>Constants for Line From Char - SDK Call</summary>
protected const int EM_LINEFROMCHAR = 0xc9;
///<summary>Constants for Get Selection - SDK Call</summary>
protected const int EM_GETSEL = 0xb0;
/*
** Definitions from the Outer World
*/
/*
***************************************************************************
**
** Class: CharPosition
**
*/
///<summary>
/// A class the contains the line and character position in the bxo
///</summary>
///<remarks>
///</remarks>
///
public class CharPosition
{
/*
** Local Variables
*/
///<summary>Current Line Position</summary>
protected int m_iLine = 0;
///<summary>Current Character Position</summary>
protected int m_iChar = 0;
/*
***************************************************************************
**
** Function: CharPosition
*/
///<summary>
/// Default empty constructor
///</summary>
///<returns>void</returns>
///
public CharPosition()
{
}
/*
***************************************************************************
**
** Function: CharPosition
*/
///<summary>
/// Constructor that sets the Line and Char position
///</summary>
///<param name="iLine" type="int">Line position to create with</param>
///<param name="iChar" type="int">Character position to create with</param>
///<returns>void</returns>
///
public CharPosition(int iLine, int iChar)
{
LinePos = iLine;
CharPos = iChar;
}
/*
***************************************************************************
**
** Property(int): LinePos
*/
///<summary>
/// Line Position
///</summary>
///<value>
///
///</value>
///
public int LinePos
{
get{return m_iLine;}
set{m_iLine = value;}
}
/*
***************************************************************************
**
** Property(int): CharPos
*/
///<summary>
/// Character Position
///</summary>
///<value>
///
///</value>
///
public int CharPos
{
get{return m_iChar;}
set{m_iChar = value;}
}
/*
***************************************************************************
**
** Function: ToString
*/
///<summary>
/// Convert the Object to a string
///</summary>
///<returns>string</returns>
///
public override string ToString()
{
return(String.Format("{{L={0}, C={1}}}", LinePos, CharPos));
}
}
/*
***************************************************************************
**
** Property(System.Drawing.Point): CaretPosition
*/
///<summary>
/// Get the Caret Position
///</summary>
///<value>
///
///</value>
///
public Point CaretPosition
{
get
{
Point pt = Point.Empty;
Win32API.Window.GetCaretPos(ref pt);
return pt;
}
}
/*
***************************************************************************
**
** Function: LineIndex
*/
///<summary>
///The return value is the number of characters that
///precede the first character in the line containing
///the caret.
///
///</summary>
///<param name="iLine" type="int">line to get the Caracters to</param>
///<returns>int - Number of characters to the beginning of iLine</returns>
///
public int LineIndex(int iLine)
{
return (int)Win32API.Window.SendMessage(new HandleRef(this, Handle), EM_LINEINDEX, iLine, 0);
}
/*
***************************************************************************
**
** Function: LineIndex
*/
///<summary>
///Send the EM_LINEINDEX message with the value of -1
///in wParam.
///
///</summary>
///<returns>int</returns>
///
public int LineIndex()
{
return LineIndex(-1);
}
/*
***************************************************************************
**
** Property(HexEdit.HexEditBase.CharPosition): Position
*/
///<summary>
///Get the Line Char positions in the buffer
///</summary>
///<value>
///
///</value>
///
public CharPosition Position
{
get
{
CharPosition cp = new CharPosition();
cp.LinePos = GetLineFromCharIndex(SelectionStart);
cp.CharPos = SelectionStart - LineIndex();
return cp;
}
}
/*
***************************************************************************
**
** Function: GetDisplayChar
*/
///<summary>
///Get the Display char from a entered char
///this prevents non displayable characters from
///going to the display
///</summary>
///<param name="cData" type="char">character to check</param>
///<returns>char</returns>
///
protected char GetDisplayChar(char cData)
{
if(20 > cData)
{
cData = (char)0xB7;
}
return cData;
}
/*
***************************************************************************
**
** Function: GetDisplayChar
*/
///<summary>
///<see cref="GetDisplayChar"/>Converts to byte to char before doing the
///display filtering
///</summary>
///<param name="byData" type="byte"></param>
///<returns>char</returns>
///
protected char GetDisplayChar(byte byData)
{
return GetDisplayChar((char)byData);
}
/*
***************************************************************************
**
** Function: GetFontWidth
*/
///<summary>
/// Return the Font Width, assumes fixed font
///</summary>
///<returns>int - the width of the font</returns>
///
protected int GetFontWidth()
{
Graphics g = CreateGraphics();
int iWidth = 0;
string strTest = "WWWWWWWWWWWWWWWW";
SizeF Size;
Size = g.MeasureString(strTest, Font);
iWidth = (int)(Size.Width + .09) / strTest.Length;
return iWidth;
}
/*
***************************************************************************
**
** Function: SetTextTabLocations
*/
///<summary>
///
///</summary>
///<returns>void</returns>
///<exception cref="System.Exception">Thrown</exception>
///<remarks>
///</remarks>
///<example>How to use this function
///<code>
///</code>
///</example>
///
public void SetTextTabLocations()
{
/*
** Local Variables
*/
int iTabSize = 4;
int iNoTabs = 32;
int[] aiTabs = new int[iNoTabs];
int iWidth = GetFontWidth();
for(int i = 0; i < iNoTabs; i++)
{
aiTabs[i] = iWidth * ((i + 1) * iTabSize);
}
SelectionTabs = aiTabs;
}
/*
***************************************************************************
**
** Function: HexCtoB
*/
///<summary>
/// Convert a character to its Byte Equivalent
///</summary>
///<param name="cVar" type="char"></param>
///<returns>byte</returns>
///<example>char = A (0x41) returns a byte of A (0xA)
///</example>
///
protected byte HexCtoB(char cVar)
{
byte byRet = 0;
if((cVar >= '0') && (cVar <= '9'))
{
byRet = (byte)(cVar - '0');
}
else
{
byRet = (byte)((cVar - 'A') + 10);
}
return byRet;
}
/*
***************************************************************************
**
** Function: InitializeComponent
*/
///<summary>
/// This is a overridable function to process any initialization stuff the box
/// might need
///</summary>
///<returns>void</returns>
///
virtual public void InitializeComponent()
{
}
}
/*
***************************************************************************
**
** Class: HexEditBox
**
*/
///<summary>
///This is the Hex Edit Box Display
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -