📄 memview.cpp
字号:
#include "stdafx.h"
#include "memview.h"
#include "resource.h"
#define IDM_COPY 101
#define IDM_SELECT_ALL 102
#define BYTE_WIN_WIDTH 77
#define CLINESSHOW 8
#define ABS(x) ((x) < 0? -(x) : (x))
void WINAPI VScrollBytes (HWND, int, int, LPMEMVIEW);
void WINAPI KeyToScrollMsg (HWND, WPARAM);
void WINAPI DisplayBytes (HDC, LPRECT, LPMEMVIEW);
int WINAPI GetBytesLine (PVOID, UINT, DWORD, int, char *);
void ProcessKey(HWND hWnd,WPARAM wParam,MEMVIEW *pmv);
void CheckMemUpdate(void);
void CopyClipboard(MEMVIEW *pmv);
void MemToAscii(BYTE number,char* str);
void MemToAscii(WORD number,char* szAscii);
void MemToAscii(DWORD number,char* szAscii);
/* system constants */
int xChar,
yChar,
xScreen,
yScreen,
yFrame,
xFrame,
yCaption,
xVScrollBar;
HFONT hFont;
HWND hMemWnd;
HWND hWnd;
HMENU hexmenu;
BOOL hexWindow=FALSE;
int globalmemtype; // SDRAM, MMIO, User
int globaldumptype; // BYTE, WORD, DWORD
int xCaret;
int yCaret;
static DWORD value;
static PVOID mem;
PVOID selectionStart,selectionEnd;
BOOL selectionComplete=FALSE;
void RegisterHexWindow (void);
/* start app */
void RegisterHexWindow (void)
{
WNDCLASS wc;
char szClass[MAX_PATH];
static int first=1;
if(first)
{
/* register the memory viewer window class */
strcpy(szClass,"Hexwindow");
wc.style = 0;
wc.lpfnWndProc = (WNDPROC)MemWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = VIEWWXB;
wc.hInstance = AfxGetInstanceHandle();
wc.hIcon = NULL;
//LoadIcon (AfxGetInstanceHandle(), MAKEINTRESOURCE (IDR_MAINICON));
wc.hCursor = LoadCursor (0, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = szClass;
RegisterClass (&wc);
first=0;
}
}
/*********************************************************
Creates and displays a window for viewing memory
Arguments:
BaseAdr: Starting physical address (used for display only)
buffer: Starting linear address
*********************************************************/
void ViewMem (HWND hWndParent,DWORD BaseAdr,PVOID buffer,DWORD size,
int dumptype,int memtype,BOOL start)
{
LPMEMVIEW pmv;
char szClass[MAX_PATH];
static char szTitle[MAX_PATH]="Memory Dump";
static HWND hWnd=NULL;
HDC hDC;
TEXTMETRIC tm;
if(start)
{
if(memtype == 1)
sprintf(szTitle,"SDRAM Memory Dump From %08x",BaseAdr);
else if (memtype == 2)
sprintf(szTitle,"MMIO Memory Dump From %08x",BaseAdr);
else
sprintf(szTitle,"User Memory Dump From %08x",BaseAdr);
}
if(size > 65536*16) // Due to problem in GetScrollPos for THUMB
size=65536*16;
globalmemtype=memtype; // MMIO or SDRAM
globaldumptype=dumptype; // Byte, Word or Dword
if(!hexWindow) // If window not already created
{
if(!start)
return;
// LONG lRet = 1;
#if 1
static LOGFONT lf_Font = {
-10, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET,
OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY, FIXED_PITCH | FF_DONTCARE,
"Courier",
};
hexWindow=TRUE;
hDC = GetDC (hWndParent);
/* want a font with point size of 10 (smallest size it comes in) */
lf_Font.lfHeight = -(10 * GetDeviceCaps(hDC, LOGPIXELSY)/72);
hFont = CreateFontIndirect(&lf_Font);
SelectObject(hDC, hFont);
/* initialize system constants */
GetTextMetrics(hDC, &tm);
yChar = tm.tmHeight;
xChar = tm.tmAveCharWidth;
xScreen = GetSystemMetrics(SM_CXSCREEN);
yScreen = GetSystemMetrics(SM_CYSCREEN);
yFrame = GetSystemMetrics(SM_CYFRAME);
xFrame = GetSystemMetrics(SM_CXFRAME);
yCaption = GetSystemMetrics(SM_CYCAPTION);
xVScrollBar = GetSystemMetrics(SM_CXVSCROLL);
SelectObject(hDC, GetStockObject(SYSTEM_FONT));
ReleaseDC(hWndParent, hDC);
#endif
/* allocate local memory for memory view structure */
pmv = (LPMEMVIEW)LocalAlloc (LPTR, sizeof (MEMVIEW));
if(pmv == NULL)
{
AfxMessageBox("Error allocating memory view structure");
return;
}
pmv->lpMem = buffer;
pmv->nSize = size; // GetFileSize (pmv->hFile, NULL);
pmv->nBase = BaseAdr;
/* load resource strings for window class */
strcpy(szClass,"HexWindow");
/* create memory view window */
hWnd = CreateWindow (szClass,
szTitle,
WS_POPUP | WS_VISIBLE |
WS_VSCROLL | WS_THICKFRAME | WS_OVERLAPPEDWINDOW,
0,
0,
BYTE_WIN_WIDTH * xChar + xVScrollBar + 2*xFrame,
yScreen/2,
NULL /* hWndParent */,
NULL,
GetModuleHandle (NULL),
(LPVOID)pmv);
}
else // Window already created
{
SetWindowText(hWnd,szTitle);
xCaret=13;
if(start)
{
yCaret=0;
pmv = (LPMEMVIEW)GetWindowLong (hWnd, WXB_LPMEMVIEW);
pmv->lpMem = buffer;
pmv->nSize = size;
pmv->nLines = (pmv->nSize+15)/16;
pmv->nExtraBytes = (pmv->nSize & 0x0000000F);
pmv->nBase = BaseAdr;
pmv->PosV = 0;
pmv->RangeV = pmv->nLines-pmv->yWin/yChar;
if (pmv->RangeV < 0)
pmv->RangeV = 0;
if (pmv->PosV > pmv->RangeV)
pmv->PosV = pmv->RangeV;
SetScrollRange (hWnd, SB_VERT, 0, pmv->RangeV, FALSE);
SetScrollPos (hWnd, SB_VERT, pmv->PosV, TRUE);
}
InvalidateRect (hWnd, NULL, TRUE);
}
UpdateWindow (hWnd);
ShowWindow (hWnd, SW_SHOWNORMAL);
}
LONG WINAPI MemWndProc (
HWND hWnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam)
{
MEMVIEW *pmv = (LPMEMVIEW)GetWindowLong (hWnd, WXB_LPMEMVIEW);
static int xPos,yPos; //Mouse positions
switch (uMsg)
{
case WM_CREATE:
hexmenu=CreatePopupMenu();
AppendMenu(hexmenu,MF_STRING, IDM_COPY, "Copy");
AppendMenu(hexmenu,MF_STRING, IDM_SELECT_ALL, "Select All");
pmv = (LPMEMVIEW)((LPCREATESTRUCT)lParam)->lpCreateParams;
SetWindowLong (hWnd, WXB_LPMEMVIEW, (LONG)pmv);
pmv->nLines = (pmv->nSize+15)/16;
pmv->nExtraBytes = (pmv->nSize & 0x0000000F);
pmv->PosV = 0;
/* reposition window with proper x,y positions */
MoveWindow (hWnd,
15,
15,
((LPCREATESTRUCT)lParam)->cx,
((LPCREATESTRUCT)lParam)->cy,
TRUE);
xCaret=13;
yCaret=0;
break;
case WM_SIZE:
pmv->xWin = LOWORD (lParam);
pmv->yWin = HIWORD (lParam);
pmv->RangeV = pmv->nLines-pmv->yWin/yChar;
if (pmv->RangeV < 0)
pmv->RangeV = 0;
if (pmv->PosV > pmv->RangeV)
pmv->PosV = pmv->RangeV;
SetScrollRange (hWnd, SB_VERT, 0, pmv->RangeV, FALSE);
SetScrollPos (hWnd, SB_VERT, pmv->PosV, TRUE);
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
BeginPaint (hWnd, &ps);
DisplayBytes (ps.hdc, &(ps.rcPaint),pmv);
EndPaint (hWnd, &ps);
}
break;
case WM_KEYDOWN:
ProcessKey(hWnd,wParam,pmv);
break;
case WM_LBUTTONDOWN:
{
xPos=LOWORD(lParam)/xChar;
yPos=HIWORD(lParam)/yChar;
SetCapture(hWnd);
if(selectionStart)
{
selectionStart=0;
InvalidateRect (hWnd, NULL, FALSE);
}
yCaret=yPos;
if(xPos < 13)
xCaret=13;
else if(globaldumptype == 0)
{
if(xPos-13 > 15*3+1)
xCaret=13+15*3+1;
else
{
if(((xPos-13+1)%3) == 0)
xCaret=xPos+1;
else
xCaret=xPos;
}
}
else if(globaldumptype == 1)
{
if(xPos-13 > 7*5+3)
xCaret=13+7*5+3;
else
{
if(((xPos-13+1)%5) == 0)
xCaret=xPos+1;
else
xCaret=xPos;
}
}
else
{
if(xPos-13 > 3*9+7)
xCaret=13+3*9+7;
else
{
if(((xPos-13+1)%9) == 0)
xCaret=xPos+1;
else
xCaret=xPos;
}
}
CheckMemUpdate();
SetCaretPos(xCaret*xChar,yCaret*yChar);
}
break;
case WM_LBUTTONUP:
ReleaseCapture();
if(selectionStart != 0)
{
selectionComplete=TRUE;
}
break;
case WM_RBUTTONDOWN:
{
POINT point;
if(selectionStart == 0 || !selectionComplete)
EnableMenuItem(hexmenu,IDM_COPY,MF_DISABLED | MF_GRAYED);
else
EnableMenuItem(hexmenu,IDM_COPY,MF_ENABLED);
point.x=LOWORD(lParam);
point.y=HIWORD(lParam);
ClientToScreen(hWnd,&point);
TrackPopupMenu(hexmenu,TPM_LEFTALIGN,
point.x,point.y,0,hWnd,NULL);
}
break;
case WM_MOUSEMOVE:
if((wParam & MK_LBUTTON) &&
(LOWORD(lParam)/xChar != xPos || HIWORD(lParam)/yChar != yPos))
{
if(selectionStart == 0)
{
selectionStart=((PBYTE)(pmv->lpMem)+16*(yCaret+pmv->PosV));
selectionEnd=selectionStart;
selectionComplete=FALSE;
}
else
{
xPos=LOWORD(lParam)/xChar;
short int yPos=HIWORD(lParam)/yChar;
if(yPos > pmv->yWin/yChar)
SendMessage(hWnd,WM_KEYDOWN, VK_DOWN,0L);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -