📄 hexeditwnd.cpp
字号:
#include "16Edit.h"
#include <commctrl.h>
#include <stddef.h>
#include "HexEditWnd.h"
#include "File.h"
#include "Common.h"
#include "16EditDll.h"
#include "CPathString.h"
#include "WideChar.h"
//
// function prototypes
//
LRESULT __stdcall HEditWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
LRESULT __stdcall TBHookProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
BOOL __stdcall GotoDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
BOOL __stdcall SelBlockDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
BOOL __stdcall SearchDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
DWORD __stdcall HEditWindowThread(void* pArg);
//
// constants
//
//const DWORD dwMinusOne = -1;
#define ID_TB 0x2000
#define ID_TRAYICON 0x2001
#define IDT_FIRST 0x1000
#define IDT_RESTORE IDT_FIRST + 1
#define IDT_EXIT IDT_FIRST + 2
#define WM_TRAYMENU WM_USER + IDT_FIRST
/*
#define TB_FIRST 0x500
#define TB_CLOSE TB_FIRST + 1
#define TB_SAVE TB_FIRST + 2
#define TB_GOTO TB_FIRST + 3
#define TB_ABOUT TB_FIRST + 4
#define TB_WIN2TOP TB_FIRST + 5
#define TB_SEARCH TB_FIRST + 6
#define TB_SELBLOCK TB_FIRST + 7
#define TB_CUT TB_FIRST + 8
#define TB_COPY TB_FIRST + 9
#define TB_PASTE TB_FIRST + 10
*/
// tb buttons
const TBBUTTON TBbs[] =
{
{ 6, TB_WIN2TOP, TBSTATE_ENABLED, (BYTE)TBSTYLE_BUTTON, 0, 0},
{ 1, TB_GOTO, TBSTATE_ENABLED, (BYTE)TBSTYLE_BUTTON, 0, 0},
{ 8, TB_SELBLOCK, TBSTATE_ENABLED, (BYTE)TBSTYLE_BUTTON, 0, 0},
{ 15, TB_SELALL, TBSTATE_ENABLED, (BYTE)TBSTYLE_BUTTON, 0, 0},
{ 7, TB_SEARCH, TBSTATE_ENABLED, (BYTE)TBSTYLE_BUTTON, 0, 0},
{ 13, TB_SEARCHDOWN, TBSTATE_ENABLED, (BYTE)TBSTYLE_BUTTON, 0, 0},
{ 12, TB_SEARCHUP, TBSTATE_ENABLED, (BYTE)TBSTYLE_BUTTON, 0, 0},
{ 0, NULL, TBSTATE_ENABLED, (BYTE)TBSTYLE_SEP, 0, 0},
{ 14, TB_UNDO, TBSTATE_ENABLED, (BYTE)TBSTYLE_BUTTON, 0, 0},
{ 2, TB_SAVE, TBSTATE_ENABLED, (BYTE)TBSTYLE_BUTTON, 0, 0},
{ 0, NULL, TBSTATE_ENABLED, (BYTE)TBSTYLE_SEP, 0, 0},
{ 9, TB_CUT, TBSTATE_ENABLED, (BYTE)TBSTYLE_BUTTON, 0, 0},
{ 10, TB_COPY, TBSTATE_ENABLED, (BYTE)TBSTYLE_BUTTON, 0, 0},
{ 11, TB_PASTE, TBSTATE_ENABLED, (BYTE)TBSTYLE_BUTTON, 0, 0},
{ 0, NULL, TBSTATE_ENABLED, (BYTE)TBSTYLE_SEP, 0, 0},
{ 3, TB_ABOUT, TBSTATE_ENABLED, (BYTE)TBSTYLE_BUTTON, 0, 0},
{ 0, TB_CLOSE, TBSTATE_ENABLED, (BYTE)TBSTYLE_BUTTON, 0, 0}
};
#define HEDIT_WND_CLASS "16Edit Main Window"
#define HEDIT_WND_TITLE "[ 16Edit FX ]"
#define HEDIT_TRAY_TIP "[16Edit]"
#define szMemoryBuff "memory buffer"
#define HEDIT_WND_WIDTH 638
#define HEDIT_WND_HEIGHT 400
#define SB_HEIGHT 20
#define INI_NAME "16Edit.ini"
#define INI_SECTION "Options"
#define INI_WINPOSKEY "WinPos"
#define CF_16Edit "CF_16EDIT"
#define STR_NO_MEM "Not enough memory available !"
#define DEF_FONT_HEIGHT 17
#define DEF_FONT_WIDTH 8
#define RGB_BLACK RGB(0, 0, 0)
#define RGB_RED RGB(0xFF, 0, 0)
#define RGB_SBGRAY RGB(0x40, 0x40, 0x40)
#define H2 "%02lX"
#define H8 "%08lX"
#define _H8 "0x%08lX"
#define DEF_MAX_LINES 19 // A LOOK UP VALUE
#define PAIRS_X (11 * uFontWidth)
#define DIGIT_PAIR_WIDTH (3 * uFontWidth)
#define CHARS_X (PAIRS_X + 16 * DIGIT_PAIR_WIDTH + uFontWidth)
#define WM_MOUSEWHEEL 0x020A
#define WHEEL_MOVE_STEPS 4
//
// global variables
//
BOOL bStartCaretPos;
HE_POS posStartCaretPos;
BOOL bStartSel;
DWORD dwStartSelStart;
DWORD dwStartSelEnd;
BOOL bStartOnTop;
BOOL bParentWin;
HWND hwndParent;
WNDPROC pOrgTBWndProc;
HexEditWnd::HexEditWnd()
{
LOGFONT lf;
// save dll base
hDllBase = GetModuleHandle(HEDIT_DLL_NAME);
//
// init some variables
//
iyHETop = 0;
iyHEBottom = 0;
bHEVisible = FALSE;
InitEdition();
ZERO(search);
// path/dir stuff
GetModuleFileName( hInst, cInitialDir, sizeof(cInitialDir) );
CPathString::PathToDir( cInitialDir );
CPathString::ForceEndBackslash( cInitialDir);
wsprintf(cIniPath, "%s"INI_NAME, cInitialDir);
// create text font
ZERO(lf);
lf.lfHeight = DEF_FONT_HEIGHT;
lf.lfWidth = DEF_FONT_WIDTH;
lf.lfWeight = FW_LIGHT;
lstrcpy(lf.lfFaceName, "Courier New");
hFont = CreateFontIndirect(&lf);
lf.lfUnderline = TRUE;
hFontU = CreateFontIndirect(&lf);
// create an own clipboard format
cf16Edit = RegisterClipboardFormat(CF_16Edit);
// build tray popup menu
hmTray = CreatePopupMenu();
AppendMenu(hmTray, MF_STRING, IDT_RESTORE, "restore");
AppendMenu(hmTray, MF_STRING, IDT_EXIT, "exit");
}
HexEditWnd::~HexEditWnd()
{
//
// Cleanup
//
DeleteObject(hFont);
DeleteObject(hFontU);
DestroyMenu(hmTray);
if (search.bInited)
{
if (search.pData)
free(search.pData);
if (search.pDlgStr)
free(search.pDlgStr);
}
}
void HexEditWnd::InitEdition()
{
//
// init some variables
//
ZERO(stat);
ZERO(search);
hMainWnd = 0;
diData.dwSize = 0;
bHEOnTop = 0;
uMaxLines = DEF_MAX_LINES;
bResizingAllowed = FALSE;
pActionHandler = NULL;
bStartCaretPos = FALSE;
bStartSel = FALSE;
bStartOnTop = FALSE;
bParentWin = FALSE;
bMinToTray = FALSE;
bSaveWinPos = FALSE;
bRestoreWinPos = FALSE;
bUserWinPos = FALSE;
return;
}
void HexEditWnd::QuitEdition()
{
free(diData.pDataBuff);
// diOrgData.pDataBuff is freeed by "FILE::~FILE"
return;
}
LRESULT __stdcall HEditWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_SHOWWINDOW:
HEdit.HEHandleWM_SHOWWINDOW(hWnd, uMsg, wParam, lParam);
break;
case WM_SIZE:
HEdit.HEHandleWM_SIZE(hWnd, wParam, lParam);
break;
case WM_NOTIFY:
if (HEdit.HEHandleWM_NOTIFY(hWnd, uMsg, wParam, lParam))
return 0;
break;
case WM_MOVE:
HEdit.HEHandleWM_MOVE(hWnd, wParam, lParam);
break;
case WM_PAINT:
HEdit.PaintText(hWnd);
break;
case WM_KEYDOWN:
HEdit.HEHandleWM_KEYDOWN(hWnd, uMsg, wParam, lParam);
break;
case WM_CHAR:
return HEdit.HEHandleWM_CHAR(hWnd, uMsg, wParam, lParam);
case WM_LBUTTONDOWN:
case WM_LBUTTONUP:
HEdit.HEHandleLButton(hWnd, uMsg, wParam, lParam);
break;
case WM_MOUSEMOVE:
HEdit.HEHandleWM_MOUSEMOVE(hWnd, uMsg, wParam, lParam);
break;
case WM_MOUSEWHEEL: // mouse scrolling support
HEdit.HEHandleWM_MOUSEWHEEL(hWnd, uMsg, wParam, lParam);
break;
case WM_VSCROLL:
HEdit.HEHandleWM_VSCROLL(hWnd, uMsg, wParam, lParam);
break;
case WM_SETFOCUS:
HEdit.HEHandleWM_SETFOCUS(hWnd);
break;
case WM_KILLFOCUS:
HEdit.HEHandleWM_KILLFOCUS(hWnd);
break;
case WM_COMMAND:
HEdit.HEHandleWM_COMMAND(hWnd, uMsg, wParam, lParam);
break;
case WM_TRAYMENU:
HEdit.HEHandleWM_TRAYMENU(hWnd, uMsg, wParam, lParam);
return 0;
case WM_CLOSE:
HEdit.HEHandleWM_CLOSE(hWnd, uMsg, wParam, lParam);
break;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
//
// report the main window procedure about WM_MOUSEMOVE to support scrolling up
// while selecting a block
//
LRESULT __stdcall TBHookProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_MOUSEMOVE:
HEdit.HEHandleWM_MOUSEMOVE(hWnd, uMsg, wParam, lParam);
break;
case WM_LBUTTONUP:
SendMessage(HEdit.GetHEditWndHandle(), uMsg, 0, 0);
break;
}
return CallWindowProc(pOrgTBWndProc, hWnd, uMsg, wParam, lParam);
}
BOOL __stdcall GotoDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_INITDIALOG:
HEdit.InitGotoDlg(hDlg);
return TRUE;
case WM_SHOWWINDOW:
SetFocus( GetDlgItem(hDlg, GO_OFFSET) );
return TRUE;
case WM_COMMAND:
return HEdit.GDHandleWM_COMMAND(hDlg, uMsg, wParam, lParam);
case WM_CLOSE:
EndDialog(hDlg, wParam);
return TRUE;
}
return FALSE; // ERR
}
BOOL __stdcall SelBlockDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_INITDIALOG:
HEdit.InitSelBlockDlg(hDlg);
return TRUE;
case WM_SHOWWINDOW:
CheckDlgButton(hDlg, SB_RADIOEND, TRUE);
SetFocus( GetDlgItem(hDlg, SB_START) );
return TRUE;
case WM_COMMAND:
return HEdit.SBHandleWM_COMMAND(hDlg, uMsg, wParam, lParam);
case WM_CLOSE:
EndDialog(hDlg, 0);
return TRUE;
}
return FALSE; // ERR
}
BOOL __stdcall SearchDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_INITDIALOG:
HEdit.SSInitDlg(hDlg, uMsg, wParam, lParam);
return TRUE;
case WM_SHOWWINDOW:
SetFocus( GetDlgItem(hDlg, SS_STR) );
return TRUE;
case WM_COMMAND:
return HEdit.SSHandleWM_COMMAND(hDlg, uMsg, wParam, lParam);
case WM_CLOSE:
EndDialog(hDlg, 0);
return TRUE;
}
return FALSE; // ERR
}
BOOL HexEditWnd::CreateMainWndThread()
{
return GuiThread.Create(HEditWindowThread, NULL, 0);
}
DWORD __stdcall HEditWindowThread(void* pArg)
{
WNDCLASS wc;
MSG msg;
UINT icx, icy, ix, iy;
HWND hWnd, hTB;
RECT rct;
HACCEL hAccel;
HE_ACTION act;
HE_WIN_POS wp;
InitCommonControls();
//
// do window stuff
//
ZERO(wc);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.hInstance = HEdit.GetInstance();
wc.hIcon = LoadIcon(HEdit.GetInstance(), (PSTR)IDI_16Edit);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszClassName = HEDIT_WND_CLASS;
wc.lpfnWndProc = HEditWndProc;
RegisterClass(&wc);
icx = HEDIT_WND_WIDTH;
icy = HEDIT_WND_HEIGHT;
ix = (GetSystemMetrics(SM_CXFULLSCREEN) - icx) / 2;
iy = (GetSystemMetrics(SM_CYFULLSCREEN) - icy) / 2;
// create main wnd
hWnd = CreateWindow(
HEDIT_WND_CLASS,
HEDIT_WND_TITLE,
WS_OVERLAPPEDWINDOW | WS_VSCROLL,
ix,
iy,
icx,
icy,
bParentWin ? hwndParent : 0, // HE_SET_PARENTWINDOW
NULL,
HEdit.GetInstance(),
NULL);
HEdit.SetHEditWndHandle(hWnd);
// handle parent window state
if (bParentWin)
if (IsWindowEnabled(hwndParent))
EnableWindow(hwndParent, FALSE);
// create toolbar
hTB = CreateToolbarEx(
hWnd,
WS_CHILD | WS_VISIBLE | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
ID_TB,
16, // number of buttons in the bitmap
NULL,
(DWORD)LoadBitmap(HEdit.GetInstance(), (PSTR)IDB_TOOLBAR),
(LPTBBUTTON)&TBbs,
17, // number of buttons to create
16,
16,
16,
16,
sizeof(TBBUTTON));
// save TB information
HEdit.SetTBHandle(hTB);
GetClientRect(hTB, &rct);
HEdit.iyHETop = rct.bottom - rct.top + 2;
// hook TB
pOrgTBWndProc = (WNDPROC)SetWindowLong(
hTB,
GWL_WNDPROC,
(DWORD)TBHookProc);
// load accelerator
hAccel = LoadAccelerators(HEdit.GetInstance(), (PSTR)IDR_ACCEL);
// show wnd
ShowWindow(hWnd, SW_SHOWNORMAL);
UpdateWindow(hWnd);
//
// handle HE_SET_USERWINDOWPOSITION/HE_SET_RESTOREWINDOWPOSITION
//
if ( HEdit.bUserWinPos )
{
wp = HEdit.wpUser;
SetWindowPos( hWnd, HWND_NOTOPMOST, wp.ix, wp.iy, wp.icx, wp.icy, SWP_SHOWWINDOW );
ShowWindow( hWnd, wp.iState );
}
else if ( HEdit.bRestoreWinPos )
{
if ( GetPrivateProfileStruct(INI_SECTION, INI_WINPOSKEY, &wp, sizeof(wp), HEdit.cIniPath) )
{
SetWindowPos(
hWnd,
HWND_NOTOPMOST,
wp.ix, wp.iy, wp.icx, wp.icy,
SWP_SHOWWINDOW);
ShowWindow(hWnd, wp.iState);
}
}
//
// handle specified caret/sel settings
//
HEdit.HandleStartCaretPosSel(hWnd);
//
// HE_SET_ONTOP ?
//
if (bStartOnTop)
SendMessage(hWnd, WM_COMMAND, TB_WIN2TOP, 0);
HEdit.bHEVisible = TRUE;
//
// inform about HE_ACTION_WINDOWCREATED
//
if (HEdit.pActionHandler)
{
ZERO(act);
act.dwActionCode = HE_ACTION_WINDOWCREATED;
act.hwnd16Edit = hWnd;
HEdit.pActionHandler(&act);
}
//
// enter win loop
//
while(GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(hWnd, hAccel, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return 0;
}
HWND HexEditWnd::GetHEditWndHandle()
{
return hMainWnd;
}
void HexEditWnd::SetHEditWndHandle(HWND hWnd)
{
hMainWnd = hWnd;
return;
}
void HexEditWnd::SetTBHandle(HWND hTB)
{
this->hTB = hTB;
return;
}
HINSTANCE HexEditWnd::GetInstance()
{
return hDllBase;
}
void HexEditWnd::SetVisible(BOOL bVisible)
{
if (bVisible)
{
ShowWindow(hMainWnd, SW_SHOWNORMAL);
UpdateWindow(hMainWnd);
}
else
ShowWindow(hMainWnd, SW_HIDE);
return;
}
BOOL HexEditWnd::SetHEDataInfo(PHE_DATA_INFO pDI)
{
void* p;
// cleanup file map
fInput.Destroy();
// copy data mem
p = malloc(pDI->dwSize);
if (!p)
return FALSE; // ERR
memcpy(
p,
pDI->pDataBuff,
pDI->dwSize);
// save infos
diData = diOrgData = *pDI;
diData.pDataBuff = p;
// set title
SetHEWndCaption();
return TRUE; // OK
}
BOOL HexEditWnd::DoEditFile(char* szFilePath, BOOL bForceReadOnly)
{
BOOL bRet;
void *p;
HE_ACTION act;
// cleanup file map
fInput.Destroy();
// get file handle
if (bForceReadOnly)
bRet = fInput.GetFileHandle(szFilePath, F_OPENEXISTING_R);
else
bRet = fInput.GetFileHandleWithMaxAccess(szFilePath);
if (!bRet)
return FALSE; // FAILURE
// map file
if (!fInput.MapFile())
return FALSE; // FAILURE
// fill diData
diOrgData.bReadOnly = fInput.IsFileReadOnly();
diOrgData.dwSize = fInput.GetFSize();
diOrgData.pDataBuff = fInput.GetMapPtr();
// make a copy of this memory block
p = malloc(diOrgData.dwSize);
if (!p)
return FALSE; // ERR
memcpy(
p,
diOrgData.pDataBuff,
diOrgData.dwSize);
diData = diOrgData;
diData.pDataBuff = p;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -