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

📄 floppyimagedlg.cpp

📁 从软盘读取IMG文件和写IMG文件到软盘的程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// FloppyImageDlg.cpp : implementation file
//

#include "stdafx.h"
#include "FloppyImage.h"
#include "FloppyImageDlg.h"

#include "FloppyIos.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFloppyImageDlg dialog

CFloppyImageDlg::CFloppyImageDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CFloppyImageDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CFloppyImageDlg)
	m_strOpenDrive = _T("");
	m_strLockVolume = _T("");
	m_strGetGeometry = _T("");
	m_strProcessFloppy = _T("");
	m_strDismountVolume = _T("");
	m_strUnlockVolume = _T("");
	m_strCloseDrive = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CFloppyImageDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFloppyImageDlg)
	DDX_Control(pDX, IDC_PROGRESS, m_ctrlProgress);
	DDX_Text(pDX, IDC_OPEN_DRIVE, m_strOpenDrive);
	DDX_Text(pDX, IDC_LOCK_VOLUME, m_strLockVolume);
	DDX_Text(pDX, IDC_GET_GEOMETRY, m_strGetGeometry);
	DDX_Text(pDX, IDC_PROCESS_FLOPPY, m_strProcessFloppy);
	DDX_Text(pDX, IDC_DISMOUNT_VOLUME, m_strDismountVolume);
	DDX_Text(pDX, IDC_UNLOCK_VOLUME, m_strUnlockVolume);
	DDX_Text(pDX, IDC_CLOSE_DRIVE, m_strCloseDrive);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CFloppyImageDlg, CDialog)
	//{{AFX_MSG_MAP(CFloppyImageDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(ID_READ_FLOPPY, OnReadFloppy)
	ON_BN_CLICKED(ID_WRITE_FLOPPY, OnWriteFloppy)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFloppyImageDlg message handlers

BOOL CFloppyImageDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	m_ctrlProgress.SetStep(1);

	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CFloppyImageDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CFloppyImageDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CFloppyImageDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}


void CFloppyImageDlg::OnReadFloppy() 
{
	// TODO: Add your control notification handler code here

    HANDLE hDrive;				// handle of drive
    DISK_GEOMETRY Geometry;		// disk geometry
	CString strFilePath;		// full path of disk image file
	BOOL bResult;				// return value of IOCTRL
	CFile file;					// disk image file
	enum {
		stOpenDrive,
		stLockVolume,
		stGetGeometry,
		stReadFloppy,
//		stDismountVolume,
		stUnlockVolume,
		stCloseDrive,
		stExit
	} nState;					// state machine of processing

	m_strOpenDrive = _T("");
	m_strLockVolume = _T("");
	m_strGetGeometry = _T("");
	m_strProcessFloppy = _T("");
	m_strDismountVolume = _T("");
	m_strUnlockVolume = _T("");
	m_strCloseDrive = _T("");

	UpdateData(FALSE);

	m_ctrlProgress.SetPos(0);

	// use a file dialog to get the image file name to save

	CFileDialog dlgSave(TRUE, "*.img", 
			NULL, 
			OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, 
			"Floppy image files (*.img)|*.img|All Files (*.*)|*.*||");
	
	dlgSave.m_ofn.lpstrTitle = "Save floppy image file";
	
	if(dlgSave.DoModal() != IDOK)
	{
		return;
	}

	strFilePath = dlgSave.GetPathName();

	// create a diak image file
	if(!file.Open(strFilePath, CFile::typeBinary|CFile::modeCreate|CFile::modeWrite))
	{
		CString strTmp;
		
		strTmp.Format("Cannot create file %s!", strFilePath);
		AfxMessageBox(strTmp);

		return;
	}

	
	// wait cursor 

	CWaitCursor Wait;

	nState = stOpenDrive;

	// process loop
	while (nState != stExit)
	{
		switch(nState)
		{
			case stOpenDrive:			// open drive
			{
				m_strOpenDrive.Format("Opening drive ... ");

				UpdateData(FALSE);

				hDrive=::OpenDisk("\\\\.\\A:");

				if(hDrive != INVALID_HANDLE_VALUE)
				{
					m_strOpenDrive += "OK";
					nState = stLockVolume;		// success, next state: lock volume
				}
				else
				{
					m_strOpenDrive += "failed";
					nState = stExit;		// failed, next state: exit
				}

				UpdateData(FALSE);

				break;
			}

			case stLockVolume:			// lock volume
			{
				m_strLockVolume.Format("Locking volume ... ");

				UpdateData(FALSE);

				bResult = ::LockVolume(hDrive);

				if(bResult)
				{
					m_strLockVolume += "OK";
					nState = stGetGeometry;		// success, next state: get geometry
				}
				else
				{
					m_strLockVolume += "failed";
					nState = stCloseDrive;		// failed, next state: close drive
				}

				UpdateData(FALSE);

				break;
			}

			case stGetGeometry:			// // get geometry
			{
				m_strGetGeometry.Format("Getting geometry ... ");

				UpdateData(FALSE);

				bResult = ::GetDiskGeometry(hDrive, &Geometry);

				if(bResult)
				{
					m_strGetGeometry += "OK";
					nState = stReadFloppy;		// success, next state: read from floppy
				}
				else
				{
					m_strGetGeometry += "failed";
					nState = stUnlockVolume;		// failed, next state: unlock volume
				}

				UpdateData(FALSE);

				break;
			}

			case stReadFloppy:			// read from floppy disk and write to image file
			{
				m_strProcessFloppy.Format("Reading floppy ... ");

				UpdateData(FALSE);

				m_ctrlProgress.SetRange(0, (short)Geometry.Cylinders.LowPart);

				// write disk geometry as file header
				file.Write(&Geometry, sizeof(DISK_GEOMETRY));

				// calculate buffer size needed
				DWORD dwBufSize =  Geometry.TracksPerCylinder * Geometry.SectorsPerTrack * Geometry.BytesPerSector;

				BYTE* pBuf=new BYTE[dwBufSize];

				// read one cylinder each step, in order to display in the progress bar
				for(DWORD dwCylinder=0; dwCylinder<Geometry.Cylinders.LowPart; dwCylinder++)
				{
					bResult = ::ReadTracks(hDrive, &Geometry, pBuf, dwCylinder, 1);

					if(!bResult) break;

					m_ctrlProgress.StepIt();

					file.Write(pBuf, dwBufSize);
				}

				delete [] pBuf;

				if(bResult)
				{
					m_strProcessFloppy += "OK";
				}
				else
				{
					m_strProcessFloppy += "failed";
				}

⌨️ 快捷键说明

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