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

📄 flatmodedlg.cpp

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

#include "stdafx.h"
#include "VirtualFloppyMgr.h"
#include "FlatModeDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CFlatModeDlg dialog
//typedef unsigned char BYTE;

CFlatModeDlg::CFlatModeDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CFlatModeDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CFlatModeDlg)
	m_curFileLoadAddr = 0;
	m_inputBinFile = _T("");
	m_outputImgFile = _T("");
	m_strTotalSize = _T("");
	m_strHaveUsed = _T("");
	//}}AFX_DATA_INIT

	this->bFilled = TRUE;
}


void CFlatModeDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFlatModeDlg)
	DDX_Control(pDX, IDC_SPIN_SL_ADDR, m_SpinCtrl);
	DDX_Control(pDX, IDC_LIST_FILEADDR_BOX, m_fileAddrBox);
	DDX_Text(pDX, IDC_EDIT_SL_ADDR, m_curFileLoadAddr);
	DDX_Text(pDX, IDC_EDIT_SL_BIN, m_inputBinFile);
	DDX_Text(pDX, IDC_EDIT_SL_IMG, m_outputImgFile);
	DDX_Text(pDX, IDC_STATIC_TOTAL_SZ, m_strTotalSize);
	DDX_Text(pDX, IDC_STATIC_HAVE_USED, m_strHaveUsed);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CFlatModeDlg, CDialog)
	//{{AFX_MSG_MAP(CFlatModeDlg)
	ON_BN_CLICKED(IDC_BUTTON_PREPARE, OnButtonPrepare)
	ON_BN_CLICKED(IDC_BUTTON_SL_BIN, OnButtonSlBin)
	ON_BN_CLICKED(IDC_BUTTON_SL_IMG, OnButtonSlImg)
	ON_BN_CLICKED(IDC_BUTTON_WRITE, OnButtonWrite)
	ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN_SL_ADDR, OnDeltaposSpinSlAddr)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFlatModeDlg message handlers

void CFlatModeDlg::OnButtonPrepare() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	HANDLE hf = ::CreateFile((LPCSTR)this->m_inputBinFile,
				GENERIC_READ,
				0,
				NULL,
				OPEN_EXISTING,
				FILE_ATTRIBUTE_NORMAL,
				NULL);
	DWORD dwFileSizeHigh = 0;
	DWORD dwFilesize = ::GetFileSize(hf, &dwFileSizeHigh);
	if ((long)dwFilesize <= 0) {
		::MessageBox(m_hWnd, "文件错误!", "Error", MB_OK);
		return;
	}
	::CloseHandle(hf);
	FileAddrStru tfas;
	tfas.strFileName = this->m_inputBinFile;
	tfas.nLoadBgnPos = this->m_curFileLoadAddr;
	tfas.nFileSize	 = dwFilesize;

	this->m_fileAddrAry.Add(tfas);
	if(!this->bFilled)
	{
		this->m_curFileLoadAddr += dwFilesize;
	}
	else
	{
		DWORD udpos = tfas.nLoadBgnPos+tfas.nFileSize;
		udpos = ((udpos%512)==0)?udpos:(((udpos+512)/512)*512);
		this->m_curFileLoadAddr = udpos;
	}

	CString tstr ="";
	CString tstt = "";
	tstr += tfas.strFileName;
	tstr += " || ";
	tstt.Format("BG:%d",tfas.nLoadBgnPos);
	tstr += tstt;
	tstr += " || ";
	tstt.Format("SZ:%d",tfas.nFileSize);
	tstr += tstt;
	tstr += " || ";
	this->m_fileAddrBox.AddString(tstr);


	UpdateData(FALSE);
}

void CFlatModeDlg::OnButtonSlBin() 
{
	// 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_inputBinFile = (LPSTR)szFile;

	UpdateData(FALSE);
}

void CFlatModeDlg::OnButtonSlImg() 
{
	// TODO: Add your control notification handler code here

	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_outputImgFile = (LPSTR)szFile;
	UpdateData(FALSE);
}

void CFlatModeDlg::OnButtonWrite() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	HANDLE hfImage = ::CreateFile((LPCSTR)this->m_outputImgFile,
		GENERIC_WRITE,
		0,
		NULL,
		OPEN_ALWAYS,//TRUNCATE_EXISTING,//OPEN_EXISTING,
		FILE_ATTRIBUTE_NORMAL,
		NULL);
	::SetFilePointer(hfImage, 0, 0, FILE_BEGIN);
	const int tbufLen = 512;//1024;
	BYTE tbuff[tbufLen];

	memset(tbuff,0,sizeof(tbuff));
	unsigned long tl1=512;
	unsigned long tl2 = 0;
	for(int tmpi=0;tmpi<2880;tmpi++)
	{
		if(!::WriteFile(hfImage,tbuff,tl1,&tl2,NULL))
		{
			int err;
			char error[10];
			err=GetLastError ();
			itoa (err, error, 10);
			MessageBox (error, "Writing sectors ...Failed  ");
				
			::CloseHandle(hfImage);
			return ;
		}
	}

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

	int tcnt = this->m_fileAddrAry.GetSize();
	

	HANDLE hfrd = NULL;
	HANDLE hfwt = NULL;

	unsigned long nWriteFilePos = 0;

	for(int tii=0;tii<tcnt;tii++)
	{

		FileAddrStru tfas = (FileAddrStru) this->m_fileAddrAry.GetAt(tii);
		nWriteFilePos = tfas.nLoadBgnPos;
		// 下面部分读写文件 ------------------------------------------------------------------
		hfrd = ::CreateFile((LPCSTR)tfas.strFileName,
				GENERIC_READ,
				0,
				NULL,
				OPEN_EXISTING,
				FILE_ATTRIBUTE_NORMAL,
				NULL);

		DWORD dwReadByteCnt;
		DWORD tSZ = tfas.nFileSize;
		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 = tfas.nLoadBgnPos+tfas.nFileSize;
				udpos = ((udpos%512)==0)?udpos:(((udpos+512)/512)*512);
				dwWrite = udpos - nWriteFilePos;
			}

			::SetFilePointer(hfImage, nWriteFilePos, 0, FILE_BEGIN);
			
			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 ;
			}
			nWriteFilePos += dwWrite;
		}
		::CloseHandle(hfrd);
	}
	
	::CloseHandle(hfImage);

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

void CFlatModeDlg::OnDeltaposSpinSlAddr(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR;
	// TODO: Add your control notification handler code here
	
	*pResult = 0;
}

BOOL CFlatModeDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	
	this->m_SpinCtrl.SetBuddy(this->GetDlgItem(IDC_EDIT_SL_ADDR));

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

⌨️ 快捷键说明

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