?? hexedit.cs
字號:
using System;
using System.Text;
using System.Runtime.InteropServices;
namespace MyClasses
{
public class HexEdit
{
public const int ANSI_CHARSET = 0;
public const int ANSI_FIXED_FONT = 11;
public const int OEM_CHARSET = 255;
public const int OEM_FIXED_FONT = 10;
public const int OUT_CHARACTER_PRECIS = 2;
public const int OUT_DEFAULT_PRECIS = 0;
public const int OUT_DEVICE_PRECIS = 5;
//public const int OUT_OF_DISK = UINT(-20);
//public const int OUT_OF_MEMORY = UINT(-6);
public const int OUT_OUTLINE_PRECIS = 8;
public const int OUT_PS_ONLY_PRECIS = 10;
public const int OUT_RASTER_PRECIS = 6;
public const int OUT_SCREEN_OUTLINE_PRECIS = 9;
public const int OUT_STRING_PRECIS = 1;
public const int OUT_STROKE_PRECIS = 3;
public const int OUT_TT_ONLY_PRECIS = 7;
public const int OUT_TT_PRECIS = 4;
public const int CLIP_CHARACTER_PRECIS = 1;
public const int CLIP_DEFAULT_PRECIS = 0;
public const int CLIP_EMBEDDED = 128;
public const int CLIP_LH_ANGLES = 16;
public const int CLIP_MASK = 0xF;
public const int CLIP_NOT = 0x0;
public const int CLIP_STROKE_PRECIS = 2;
public const int CLIP_SUS = 0x20;
public const int CLIP_TO_PATH = 4097;
public const int CLIP_TT_ALWAYS = 32;
public const int DEFAULT_PRECIS = 0;
public const int DEFAULT_QUALITY = 0;
public const int FIXED_PITCH = 1;
public const int FF_DECORATIVE = 80;
public const int FF_DONTCARE = 0;
public const int FF_MODERN = 48;
public const int FF_ROMAN = 16;
public const int FF_SCRIPT = 64;
public const int FF_SWISS = 32;
public const int LOGPIXELSX = 88;
public const int LOGPIXELSY = 90;
public const int WM_SETFONT = 0x30;
public const int WM_GETFONT = 0x31;
[DllImport("user32.dll")] public static extern int
ReleaseDC(
int hWnd, // handle of window
int hDC // handle of device context
);
[DllImport("gdi32.dll")] public static extern int
DeleteObject(
int hObject // handle to graphic object
);
[DllImport("user32.dll")] public static extern int
GetDC(
int hWnd // handle of window
);
[DllImport("gdi32.dll")] public static extern int
GetDeviceCaps(
int hdc, // device-context handle
int nIndex // index of capability to query
);
[DllImport("gdi32.dll")] public static extern int
CreateFont(
int nHeight, // logical height of font
int nWidth, // logical average character width
int nEscapement, // angle of escapement
int nOrientation, // base-line orientation angle
int fnWeight, // font weight
uint fdwItalic, // italic attribute flag
uint fdwUnderline, // underline attribute flag
uint fdwStrikeOut, // strikeout attribute flag
uint fdwCharSet, // character set identifier
uint fdwOutputPrecision, // output precision
uint fdwClipPrecision, // clipping precision
uint fdwQuality, // output quality
uint fdwPitchAndFamily, // pitch and family
int lpszFace // pointer to typeface name string
);
[DllImport("kernel32.dll")] public static extern int
MulDiv(
int nNumber, // 32-bit signed multiplicand
int nNumerator, // 32-bit signed multiplier
int nDenominator // 32-bit signed divisor
);
[DllImport("user32.dll")] public static extern int
SendMessage(
int hWnd, // handle of destination window
uint Msg, // message to send
int wParam, // first message parameter
int lParam // second message parameter
);
public HexEdit()
{
}
private int MakeFont( int PacketDataHandle ) {
int iFontSize = 10;
int hFont = 0;
int CurrentFont = 0;
int hdc = GetDC( PacketDataHandle );
int nHeight = -MulDiv( iFontSize, GetDeviceCaps(hdc, LOGPIXELSY), 72);
ReleaseDC( PacketDataHandle , hdc );
int cset;
cset = ANSI_CHARSET;
CurrentFont = SendMessage( PacketDataHandle , WM_GETFONT , 0 , 0 );
hFont = CreateFont( nHeight,0,0,0,0,0,0,0,(uint)cset,OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,FIXED_PITCH | FF_DONTCARE,0);
SendMessage( PacketDataHandle , WM_SETFONT , hFont , 0x00010000 );
if( CurrentFont > 0 )
DeleteObject( CurrentFont );
return 1;
}
private string PadZeros( int inInt )
{
StringBuilder TextField = new StringBuilder();
string hex = Convert.ToString( inInt , 16 );
if( hex.Length < 8 )
{
int ix = 8-hex.Length;
for( int i = 0; i < ix; ++i )
{
TextField.Append( "0" );
}
}
TextField.Append( hex );
return TextField.ToString().ToUpper();
}
private string ConvertByteToHexString( byte inByte )
{
StringBuilder TextField = new StringBuilder();
string hex = Convert.ToString( inByte , 16 );
if( hex.Length == 1 )
TextField.Append( "0" );
TextField.Append( hex );
return TextField.ToString().ToLower();
}
public string GetHexString( byte [] Data, int PacketDataHandle )
{
StringBuilder HexField = new StringBuilder();
StringBuilder TextField = new StringBuilder();
int newrow = 0;
int global = 0;
string hex = " ";
string numb = " ";
string Tmp = "";
MakeFont( PacketDataHandle );
for (int i = 0; i < Data.Length; ++i )
{
if( newrow == 0 )
{
numb = PadZeros( global );
HexField.Append( " " + numb + " " );
global += 16;
}
hex = ConvertByteToHexString( Data[i] );
HexField.Append( " " + hex ); // 3 characters
int g = Data[i];
if( g > 31 && g < 127 )
{
TextField.Append( (char) Data[i] );
}
else
{
TextField.Append( "." );
}
++newrow;
if( newrow >= 16 )
{
HexField.Append( " " + TextField.ToString() + "\n" );
TextField = new StringBuilder(); // clear
newrow = 0; // reset
}
}
// get last row, if any
byte b = 0;
string s = "";
if( newrow > 0 && newrow < 16 )
{
for( int i = 0; i < ( 16 - newrow ); ++i )
{
s = ConvertByteToHexString( b );
HexField.Append( " " + s );
}
HexField.Append( " " + TextField );
}
HexField.Append( "\n\n" );
Tmp = HexField.ToString();
return Tmp;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -