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

📄 trimdlg.cpp

📁 在开发操作系统时
💻 CPP
字号:
// TrimDlg.cpp : implementation file
//

#include "stdafx.h"
#include "VirtualFloppyMgr.h"
#include "TrimDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CTrimDlg dialog


CTrimDlg::CTrimDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTrimDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTrimDlg)
	m_InFile = _T("");
	m_OutFile = _T("");
	//}}AFX_DATA_INIT
	bFilled = TRUE;
}


void CTrimDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTrimDlg)
	DDX_Text(pDX, IDC_EDIT_SL_IN_TRIM, m_InFile);
	DDX_Text(pDX, IDC_EDIT_SL_OUT_TRIM, m_OutFile);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CTrimDlg, CDialog)
	//{{AFX_MSG_MAP(CTrimDlg)
	ON_BN_CLICKED(IDC_BUTTON_TRIM, OnButtonTrim)
	ON_BN_CLICKED(IDC_BUTTON_SL_OUT_TRIM, OnButtonSlOutTrim)
	ON_BN_CLICKED(IDC_BUTTON_SL_IN_TRIM, OnButtonSlInTrim)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTrimDlg message handlers

void CTrimDlg::OnButtonTrim() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	HANDLE hfImage = ::CreateFile((LPCSTR)this->m_OutFile,
		GENERIC_WRITE,
		0,
		NULL,
		CREATE_ALWAYS,//TRUNCATE_EXISTING|CREATE_NEW|CREATE_ALWAYS,//OPEN_EXISTING,
		FILE_ATTRIBUTE_NORMAL,
		NULL);
	HANDLE hfrd = ::CreateFile((LPCSTR)this->m_InFile,
		GENERIC_READ,
		0,
		NULL,
		OPEN_EXISTING,
		FILE_ATTRIBUTE_NORMAL,
		NULL);

	DWORD dwFileSizeHigh = 0;
	DWORD dwFilesize = ::GetFileSize(hfrd, &dwFileSizeHigh);

	::SetFilePointer(hfImage, 0, 0, FILE_BEGIN);

	const int tbufLen = 512;//1024;
	BYTE tbuff[tbufLen];


	HANDLE hfwt = NULL;

	unsigned long nWriteFilePos = 0;
	DWORD dwReadByteCnt;
	DWORD tSZ = dwFilesize;
	long wtBeg = -1;
	long wtEnd = tSZ;
	long tCurIndex = 0;

	while(tSZ)
	{
		memset(tbuff,0,sizeof(tbuff));
		dwReadByteCnt = (tSZ < tbufLen)? tSZ : tbufLen;
		DWORD dwRead = 0;
		if (!ReadFile(hfrd, tbuff, dwReadByteCnt, &dwRead, NULL)) {
			int iErr;
			char szError[128];
			iErr = GetLastError();
			::sprintf(szError, "文件读取错误!\n错误代码: %d", iErr);
			::MessageBox(m_hWnd, szError, "Error", MB_OK);
			
			::CloseHandle(hfrd);
			::CloseHandle(hfImage);
			return;
		}
		for(unsigned long tii=0; tii<dwRead; tii++)
		{
			if(tbuff[tii])
			{
				if( wtBeg == -1 )
				{
					wtBeg = tCurIndex;
				}
				wtEnd = tCurIndex;
			}
			tCurIndex++;
		}
		tSZ -= dwRead;
	}
	::SetFilePointer(hfrd, wtBeg, 0, FILE_BEGIN);
	long tmpSize = wtEnd - wtBeg +1;
	tSZ = tmpSize;
	while(tSZ)
	{
		memset(tbuff,0,sizeof(tbuff));
		dwReadByteCnt = (tSZ < tbufLen)? tSZ : tbufLen;
		DWORD dwRead = 0;
		if (!ReadFile(hfrd, tbuff, dwReadByteCnt, &dwRead, NULL)) {
			int iErr;
			char szError[128];
			iErr = GetLastError();
			::sprintf(szError, "文件读取错误!\n错误代码: %d", iErr);
			::MessageBox(m_hWnd, szError, "Error", MB_OK);
			
			::CloseHandle(hfrd);
			::CloseHandle(hfImage);
			return;
		}
		tSZ -= dwRead;
		DWORD dwWrite = 0;
		if(tSZ||!this->bFilled)
		{
			dwWrite = dwRead;
		}
		else
		{
//			DWORD udpos = tmpSize;
//			udpos = ((udpos%512)==0)?udpos:(((udpos+512)/512)*512);
//			dwWrite = udpos -;
			dwWrite = 512;
		}
		
		DWORD dwBytesWritten;	
		if (!::WriteFile(hfImage,
			tbuff,
			dwWrite,
			&dwBytesWritten,
			NULL) )
		{
			int err;
			char error[10];
			err=GetLastError ();
			itoa (err, error, 10);
			MessageBox (error, "Writing sectors ...Failed  ");
			
			::CloseHandle(hfrd);
			::CloseHandle(hfImage);
			return ;
		}
	}
	::CloseHandle(hfrd);
	
	::CloseHandle(hfImage);

	::MessageBox(this->m_hWnd, "成功!", "Floppy writer", MB_OK);
	
	
}

void CTrimDlg::OnButtonSlOutTrim() 
{
	OPENFILENAME ofn;			// common dialog box structure
	char szFile[MAX_PATH] = "out.img";	// buffer for file name
//	char szFileFilterBIN[255]= "Binary files(*.bin)\0*.bin\0All(*.*)\0*.*\0";
	char szFileFilterIMG[255]= "Image files(*.img)\0*.img\0All(*.*)\0*.*\0";
	HWND hwnd = this->m_hWnd;		// owner window
	
	// Initialize OPENFILENAME
	ZeroMemory(&ofn, sizeof(OPENFILENAME));
	ofn.lStructSize = sizeof(OPENFILENAME);
	ofn.hwndOwner = hwnd;
	ofn.lpstrFile = szFile;
	ofn.nMaxFile = sizeof(szFile);
	ofn.lpstrFilter = szFileFilterIMG;
	ofn.nFilterIndex = 1;
	ofn.lpstrFileTitle = NULL;
	ofn.nMaxFileTitle = 0;
	ofn.lpstrInitialDir = NULL;
	ofn.lpstrTitle = "打开文件";
	ofn.Flags = OFN_PATHMUSTEXIST ;//| OFN_FILEMUSTEXIST;
	
	// Display the Open dialog box. 
	
	if (GetOpenFileName(&ofn)==FALSE){
		return;
	}
	this->m_OutFile = (LPSTR)szFile;
	UpdateData(FALSE);
}

void CTrimDlg::OnButtonSlInTrim() 
{
	// TODO: Add your control notification handler code here
	OPENFILENAME ofn;			// common dialog box structure
	char szFile[MAX_PATH] = "in.bin";	// buffer for file name
	char szFileFilterBIN[255]= "Binary files(*.bin)\0*.bin\0All(*.*)\0*.*\0";
//	char szFileFilterIMG[255]= "Image files(*.img)\0*.img\0All(*.*)\0*.*\0";
	HWND hwnd = this->m_hWnd;		// owner window
	
	// Initialize OPENFILENAME
	ZeroMemory(&ofn, sizeof(OPENFILENAME));
	ofn.lStructSize = sizeof(OPENFILENAME);
	ofn.hwndOwner = hwnd;
	ofn.lpstrFile = szFile;
	ofn.nMaxFile = sizeof(szFile);
	ofn.lpstrFilter = szFileFilterBIN;
	ofn.nFilterIndex = 1;
	ofn.lpstrFileTitle = NULL;
	ofn.nMaxFileTitle = 0;
	ofn.lpstrInitialDir = NULL;
	ofn.lpstrTitle = "打开文件";
	ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

	if (GetOpenFileName(&ofn)==FALSE){
		return;
	}
	this->m_InFile = (LPSTR)szFile;

	UpdateData(FALSE);
	
}

⌨️ 快捷键说明

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