⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainfrm.cpp

📁 直接IRP操作文件的实现问题
💻 CPP
字号:
// MainFrm.cpp : implmentation of the CMainFrame class
//
/////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "resource.h"

#include "aboutdlg.h"
#include "ExToolsView.h"
#include "hexview.h"
#include "MainFrm.h"

extern "C" HANDLE ExCreateFile(LPTSTR lpName);

BOOL CMainFrame::PreTranslateMessage(MSG* pMsg)
{
	if(CFrameWindowImpl<CMainFrame>::PreTranslateMessage(pMsg))
		return TRUE;

	//return m_view.PreTranslateMessage(pMsg);
	return FALSE;
}

BOOL CMainFrame::OnIdle()
{
	UIUpdateToolBar();
	return FALSE;
}

LRESULT CMainFrame::OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	// create command bar window
	HWND hWndCmdBar = m_CmdBar.Create(m_hWnd, rcDefault, NULL, ATL_SIMPLE_CMDBAR_PANE_STYLE);
	// attach menu
	m_CmdBar.AttachMenu(GetMenu());
	// load command bar images
	m_CmdBar.LoadImages(IDR_MAINFRAME);
	// remove old menu
	SetMenu(NULL);

	HWND hWndToolBar = CreateSimpleToolBarCtrl(m_hWnd, IDR_MAINFRAME, FALSE, ATL_SIMPLE_TOOLBAR_PANE_STYLE);

	CreateSimpleReBar(ATL_SIMPLE_REBAR_NOBORDER_STYLE);
	AddSimpleReBarBand(hWndCmdBar);
	AddSimpleReBarBand(hWndToolBar, NULL, TRUE);

	CreateSimpleStatusBar();

	//m_hWndClient = m_view.Create(m_hWnd, rcDefault, NULL, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | LVS_REPORT | LVS_SHOWSELALWAYS, WS_EX_CLIENTEDGE);
	InitHexViewClass();
	m_hWndClient = m_view.Create(	"HexView",
									m_hWnd,
									rcDefault,
									NULL,
									WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL | WS_TABSTOP,
									0,
									IDC_HEXVIEW);

	DragAcceptFiles(TRUE);

	UIAddToolBar(hWndToolBar);
	UISetCheck(ID_VIEW_TOOLBAR, 1);
	UISetCheck(ID_VIEW_STATUS_BAR, 1);

	UIEnable(ID_FILE_SAVE, FALSE);
	UIEnable(ID_FILE_SAVE_AS, FALSE);
	UIEnable(ID_FILE_REPLACE, FALSE);
	UIEnable(ID_EDIT_UNDO, FALSE);
	UIEnable(ID_EDIT_REDO, FALSE);
	UIEnable(ID_EDIT_CUT, FALSE);
	UIEnable(ID_EDIT_COPY, FALSE);
	UIEnable(ID_EDIT_COPY_HEX, FALSE);
	UIEnable(ID_EDIT_PASTE, FALSE);
	UIEnable(ID_EDIT_FIND, FALSE);

	// register object for message filtering and idle updates
	CMessageLoop* pLoop = _Module.GetMessageLoop();
	ATLASSERT(pLoop != NULL);
	pLoop->AddMessageFilter(this);
	pLoop->AddIdleHandler(this);

	return 0;
}

BOOL CMainFrame::DocLoadFile(LPTSTR lpFileName)
{
	HANDLE hFile = ExCreateFile(lpFileName);

	if (!hFile)
		return FALSE;

	HANDLE hMap = CreateFileMapping(	hFile,
										NULL,
										PAGE_READWRITE,
										0,
										0,
										NULL);

	PBYTE Buffer = (PBYTE)MapViewOfFile(hMap, FILE_MAP_WRITE, 0, 0, 0);
	DWORD Length = GetFileSize(hFile, NULL);

	CloseHandle(hMap);

	SendMessage(m_view, HV_SETHANDLE, (WPARAM)Length, (LPARAM)Buffer);

	if (pMap)
		UnmapViewOfFile(pMap);
	pMap = Buffer;
	nCurFileSize = Length;
	strcpy(szCurFileName, lpFileName);

	CString strTitle = lpFileName;
	CString strOldTitle;
	strOldTitle.LoadString(IDR_MAINFRAME);
	strTitle += " - " + strOldTitle;
	SetWindowText(strTitle);

	UIEnable(ID_FILE_SAVE, TRUE);
	UIEnable(ID_FILE_SAVE_AS, TRUE);
	UIEnable(ID_FILE_REPLACE, TRUE);

	return TRUE;
}

LRESULT CMainFrame::OnDropFiles(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	TCHAR szFileName[MAX_PATH];
	HDROP hDrop = (HDROP)wParam;

	DragQueryFile(hDrop, 0, szFileName, MAX_PATH);
	DocLoadFile(szFileName);

	return 0;
}

LRESULT CMainFrame::OnFileExit(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	if (pMap)
		UnmapViewOfFile(pMap);
	pMap = NULL;

	PostMessage(WM_CLOSE);
	return 0;
}

LRESULT CMainFrame::OnFileOpen(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	OPENFILENAME ofn;
	TCHAR szFileName[MAX_PATH];

	memset(&ofn, 0, sizeof(OPENFILENAME));
	szFileName[0]		= '\0';
	ofn.lStructSize		= sizeof(OPENFILENAME);
	ofn.hwndOwner		= NULL;
	ofn.hInstance		= GetModuleHandle(NULL);
	ofn.lpstrFilter		= "全部文件\t(*.*)\t";
	ofn.lpstrFile		= szFileName;
	ofn.nMaxFile		= MAX_PATH;
	ofn.Flags			= OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_LONGNAMES |
							  OFN_EXPLORER | OFN_HIDEREADONLY;

	if (!GetOpenFileName(&ofn))
	{
		return 0;
	}

	DocLoadFile(szFileName);

	HV_FIND hv_find;

	hv_find.pMem = "不可思议";
	hv_find.nLen = 8;
	hv_find.cpMax = 0;
	hv_find.cpMin = 0;
	hv_find.dwFlags = HV_FIND_TEXT | HV_FIND_MATCHCASE;

	//SendMessage(m_view, HV_FINDTEXT, 0, (LPARAM)&hv_find);

	return 0;
}

LRESULT CMainFrame::OnFileSave(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	SendMessage(m_view, HV_APPLYMODIFY, 0, (LPARAM)NULL);
	return 0;
}

LRESULT CMainFrame::OnFileSaveAs(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	OPENFILENAME ofn;
	TCHAR szFileType[MAX_PATH];
	TCHAR szFileName[MAX_PATH];

	memset(&ofn, 0, sizeof(OPENFILENAME));
	szFileType[0]		= '\0';
	szFileName[0]		= '\0';
	ofn.lStructSize		= sizeof(OPENFILENAME);
	ofn.hwndOwner		= NULL;
	ofn.hInstance		= GetModuleHandle(NULL);
	ofn.lpstrFilter		= szFileType;
	ofn.lpstrFile		= szFileName;
	ofn.nMaxFile		= MAX_PATH;
	ofn.Flags			= OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST | OFN_LONGNAMES |
		OFN_EXPLORER | OFN_HIDEREADONLY;

	char *p = strrchr(szCurFileName, '.');
	if (p != NULL)
	{
		wsprintf(szFileType, "当前文件类型 (*%s)\t*%s\t", p, p);
		wsprintf(szFileName, "*%s", p);
		ofn.lpstrDefExt = p;
	}
	strcat(szFileType, "全部文件\t(*.*)\t");
	p = szFileType;
	do
	{
		if (*p == '\t')
			*p = '\0';
	} while (*++p);

	if (!GetSaveFileName(&ofn))
	{
		return 0;
	}

	HANDLE hFile = CreateFile(	szFileName,
								GENERIC_READ | GENERIC_WRITE,
								0,
								NULL,
								CREATE_ALWAYS,
								0,
								NULL);

	if (!hFile)
	{
		return -1;
	}

	HANDLE hMap = CreateFileMapping(	hFile,
										NULL,
										PAGE_READWRITE,
										0,
										nCurFileSize,
										NULL);

	PBYTE Buffer = (PBYTE)MapViewOfFile(hMap, FILE_MAP_WRITE, 0, 0, 0);

	CloseHandle(hFile);
	CloseHandle(hMap);

	if (Buffer != NULL)
	{
		SendMessage(m_view, HV_APPLYMODIFY, 0, (LPARAM)Buffer);
		UnmapViewOfFile(pMap);
		pMap = Buffer;
		strcpy(szCurFileName, szFileName);

		CString strTitle = szFileName;
		CString strOldTitle;
		strOldTitle.LoadString(IDR_MAINFRAME);
		strTitle += " - " + strOldTitle;
		SetWindowText(strTitle);
	}

	return 0;
}

LRESULT CMainFrame::OnFileReplace(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	OPENFILENAME ofn;
	TCHAR szFileName[MAX_PATH];

	memset(&ofn, 0, sizeof(OPENFILENAME));
	szFileName[0]		= '\0';
	ofn.lStructSize		= sizeof(OPENFILENAME);
	ofn.hwndOwner		= NULL;
	ofn.hInstance		= GetModuleHandle(NULL);
	ofn.lpstrFilter		= "全部文件\t(*.*)\t";
	ofn.lpstrFile		= szFileName;
	ofn.nMaxFile		= MAX_PATH;
	ofn.Flags			= OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_LONGNAMES |
							  OFN_EXPLORER | OFN_HIDEREADONLY;

	if (!GetOpenFileName(&ofn))
	{
		return 0;
	}

	HANDLE hSrcFile = CreateFile(	szFileName,
									GENERIC_READ,
									FILE_SHARE_READ,
									NULL,
									OPEN_EXISTING,
									0,
									NULL);

	if (hSrcFile == INVALID_HANDLE_VALUE)
	{
		MessageBox("无法打开源文件!");
		return -1;
	}

	DWORD nReadByte, nWriteByte;
	HANDLE hDstFile = ExCreateFile(szCurFileName);

	if (hDstFile == NULL)
	{
		CloseHandle(hSrcFile);
		return -1;
	}

	PBYTE buf = new BYTE[1024 * 256];

	if (buf == NULL)
	{
		CloseHandle(hSrcFile);
		CloseHandle(hDstFile);
		return -1;
	}

	UnmapViewOfFile(pMap);
	pMap = NULL;

	nCurFileSize = GetFileSize(hSrcFile, NULL);
	SetFilePointer(hDstFile, nCurFileSize, NULL, FILE_BEGIN);
	SetEndOfFile(hDstFile);
	SetFilePointer(hDstFile, 0, NULL, FILE_BEGIN);

	for (DWORD i = 0; i < nCurFileSize; i += nReadByte)
	{
		ReadFile(	hSrcFile,
					buf,
					min(nCurFileSize - i, 1024 * 256),
					&nReadByte,
					NULL);
		WriteFile(hDstFile, buf, nReadByte, &nWriteByte, NULL);
	}

	delete [] buf;
	CloseHandle(hSrcFile);
	CloseHandle(hDstFile);

	DocLoadFile(szCurFileName);

	return 0;
}

extern "C" void TEST1(HWND);

LRESULT CMainFrame::OnFileNew(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	TEST1(m_hWnd);

	return 0;
}

LRESULT CMainFrame::OnFileNewWindow(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	::PostThreadMessage(_Module.m_dwMainThreadID, WM_USER, 0, 0L);
	return 0;
}

LRESULT CMainFrame::OnEditUndo(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	SendMessage(m_view, HV_UNDO, 0, 0);
	return 0;
}

LRESULT CMainFrame::OnEditRedo(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	SendMessage(m_view, HV_REDO, 0, 0);
	return 0;
}
LRESULT CMainFrame::OnEditCopy(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	SendMessage(m_view, HV_COPY, 0, 0);
	return 0;
}

LRESULT CMainFrame::OnEditCopyHex(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	SendMessage(m_view, HV_COPYHEX, 0, 0);
	return 0;
}

LRESULT CMainFrame::OnEditPaste(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	return 0;
}


LRESULT CMainFrame::OnViewChange(int wParam, LPNMHDR lParam, BOOL& bHandled)
{
	NM_LISTVIEW *nmlv = (NM_LISTVIEW *)lParam;
	BOOL result;
	DWORD n1, n2;

	switch (lParam->code)
	{
	case HV_EN_CHANGE:
		result = SendMessage(m_view, HV_CANUNDO, 0, 0);
		UIEnable(ID_EDIT_UNDO, result);
		result = SendMessage(m_view, HV_CANREDO, 0, 0);
		UIEnable(ID_EDIT_REDO, result);
		break;

	case HV_EN_SELCHANGE:
		SendMessage(m_view, HV_GETSEL, (WPARAM)&n1, (LPARAM)&n2);
		if (n2 == -1)
		{
			UIEnable(ID_EDIT_COPY, FALSE);
			UIEnable(ID_EDIT_COPY_HEX, FALSE);
		}
		else
		{
			UIEnable(ID_EDIT_COPY, TRUE);
			UIEnable(ID_EDIT_COPY_HEX, TRUE);
		}
		break;
	}//*/

	return 0;
}

LRESULT CMainFrame::OnViewToolBar(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	static BOOL bVisible = TRUE;	// initially visible
	bVisible = !bVisible;
	CReBarCtrl rebar = m_hWndToolBar;
	int nBandIndex = rebar.IdToIndex(ATL_IDW_BAND_FIRST + 1);	// toolbar is 2nd added band
	rebar.ShowBand(nBandIndex, bVisible);
	UISetCheck(ID_VIEW_TOOLBAR, bVisible);
	UpdateLayout();
	return 0;
}

LRESULT CMainFrame::OnViewStatusBar(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	BOOL bVisible = !::IsWindowVisible(m_hWndStatusBar);
	::ShowWindow(m_hWndStatusBar, bVisible ? SW_SHOWNOACTIVATE : SW_HIDE);
	UISetCheck(ID_VIEW_STATUS_BAR, bVisible);
	UpdateLayout();
	return 0;
}

LRESULT CMainFrame::OnAppAbout(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	CAboutDlg dlg;
	dlg.DoModal();
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -