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

📄 diskreadwritedlg.cpp

📁 一个可对磁盘扇道直接进行读写操作的程序
💻 CPP
字号:
// DiskreadwriteDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Diskreadwrite.h"
#include "DiskreadwriteDlg.h"
#include <direct.h>
#include "WriteRead.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()

/////////////////////////////////////////////////////////////////////////////
// CDiskreadwriteDlg dialog

CDiskreadwriteDlg::CDiskreadwriteDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDiskreadwriteDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDiskreadwriteDlg)
	m_StartRector = 10000;
	m_EndRector = 10000;
	m_StartTime = 0;
	m_pIntOrginalData=NULL;
	m_pIntReadData=NULL;
	m_Timelong = 1;
	m_Sectornum=0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_crf=RGB(0,255,0);
}

void CDiskreadwriteDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDiskreadwriteDlg)
	DDX_Control(pDX, IDC_DISKNUM_COMBO, m_DiskNum);
	DDX_Text(pDX, IDC_STARTRECTOR_EDIT, m_StartRector);
	DDV_MinMaxUInt(pDX, m_StartRector, 10000, 100000);
	DDX_Text(pDX, IDC_ENDRECTOR_EDIT, m_EndRector);
	DDV_MinMaxUInt(pDX, m_EndRector, 10000, 100000);
	DDX_DateTimeCtrl(pDX, IDC_STARTTIME_DATETIMEPICKER, m_StartTime);
	DDX_Text(pDX, IDC_TIMELONG_EDIT, m_Timelong);
	DDV_MinMaxUInt(pDX, m_Timelong, 1, 100);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDiskreadwriteDlg, CDialog)
	//{{AFX_MSG_MAP(CDiskreadwriteDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_READFILE_BTN, OnReadfileBtn)
	ON_BN_CLICKED(IDC_WRITEDATA_BTN, OnWritedataBtn)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_BASEDONRECTOR_BTN, OnReadsectorBtn)
	ON_BN_CLICKED(IDC_SAVEFILE_BTN, OnSavefileBtn)
	ON_BN_CLICKED(IDC_BASEDONTIME_BTN, OnBasedontimeBtn)
	ON_BN_CLICKED(IDC_READBASEDONTIME_BTN, OnReadbasedontimeBtn)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDiskreadwriteDlg message handlers

BOOL CDiskreadwriteDlg::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
	
	// TODO: Add extra initialization here
	m_DiskNum.Dir (DDL_DRIVES | DDL_EXCLUSIVE, _T("*.*"));  //把所有逻辑分区显示
	m_DiskNum.SetCurSel(11);
/*
	_chdrive (3);

	// Set combo string to current drive
	CString strdrive = "[-a-]";
	strdrive.SetAt (2, strdrive.GetAt (2) + _getdrive ()-1);
	m_DiskNum.SetCurSel (m_DiskNum.FindString (0, strdrive));
*/

	CTime temp( 2007, 9, 10, 14, 37, 0 ); 
	m_StartTime = temp;
	UpdateData(FALSE);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CDiskreadwriteDlg::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 CDiskreadwriteDlg::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);
		ShowDisk(m_crf);
	}
	else
	{
		ShowDisk(m_crf);
		CDialog::OnPaint();
	}
}

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

void CDiskreadwriteDlg::OnReadfileBtn() 
{
	// TODO: Add your control notification handler code here
	CFileDialog filedlg(TRUE);
	filedlg.m_ofn.lpstrTitle="打开";
	filedlg.m_ofn.lpstrFilter="Dat File(*.dat)\0*.dat\0All File(*.*)\0*.*";
	if(IDOK==filedlg.DoModal())
	{
		CFile file(filedlg.GetFileName(),CFile::modeRead);
		m_Filelen=file.GetLength();
		m_pIntOrginalData=new short[m_Filelen/2];
		memset(m_pIntOrginalData,0,m_Filelen);
		file.Read(m_pIntOrginalData,m_Filelen);
		file.Close();
	}
}

void CDiskreadwriteDlg::OnWritedataBtn() 
{
	// TODO: Add your control notification handler code here
	UpdateData(FALSE);//将上一次的末尾扇区+1后作为此次写入的首扇区,并更新m_StartRector
	UpdateData(TRUE);
	//将压缩数据写入磁盘
	char cTemp[5];
	BYTE udisk;
	CWriteRead wr;
	m_DiskNum.GetLBText(m_DiskNum.GetCurSel(),cTemp);
	udisk=cTemp[2]-96;//变成与`的偏移量
	//由于10000这第一个扇区也算作一个扇区,因此当文件长度刚好是512的整数时,要减调1,不是512的整数时,不用减
	m_EndRector=m_StartRector+(m_Filelen%512 ? m_Filelen/512 : m_Filelen/512-1);
	WORD Sectorsnum=m_Filelen%512 ? m_Filelen/512+1 : m_Filelen/512;//要用的扇区个数
	if (m_StartRector>=10000&&m_EndRector<=100000)
	{
		if(!wr.WriteSectors(udisk,m_StartRector,Sectorsnum,m_pIntOrginalData))
		{
			 MessageBox("写入失败","错误",MB_OK|MB_ICONERROR);
		     return;
		}
	}
	
	//写入该段数据的cfg信息
	CTime ct=CTime::GetCurrentTime();
	CString starttime=ct.Format("%H:%M:%S");
	FILE* fp;
	fp=fopen("disk.cfg","a");
	if (fp!=NULL)
	{
		fseek(fp,0,SEEK_END);
		fprintf(fp,"%s,%d,%d\n",starttime,m_StartRector,m_Filelen);
//		fflush(fp);
		fclose(fp);
	}
	else
		MessageBox("cannot open the cfg file");
	
	UpdateData(FALSE);
	m_StartRector=m_EndRector+1;//将上一次的末尾扇区+1后作为此次写入的首扇区,在函数开头更新m_StartRector控件

	m_crf=RGB(255,0,0);
	Invalidate();
/*
	Sleep(1000);
	m_crf=RGB(0,255,0);
	Invalidate();
*/
	SetTimer(1,1000,NULL);
}

void CDiskreadwriteDlg::OnReadsectorBtn() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	char cTemp[5];
	BYTE udisk;
	CWriteRead wr;
	m_DiskNum.GetLBText(m_DiskNum.GetCurSel(),cTemp);
	udisk=cTemp[2]-96;//变成与`的偏移量
	UINT sectornum=m_EndRector-m_StartRector+1;

	m_pIntReadData=new short[sectornum*512/2];
	if(!wr.ReadSectors(udisk,m_StartRector,sectornum,m_pIntReadData))
	{
		MessageBox("读取失败","错误",MB_OK|MB_ICONERROR);
		return;
	}
	UpdateData(FALSE);
	
	m_crf=RGB(255,0,0);
	Invalidate();
//	Sleep(1000);
//	m_crf=RGB(0,255,0);
//	Invalidate();
	SetTimer(1,1000,NULL);
}

void CDiskreadwriteDlg::OnSavefileBtn() 
{
	// TODO: Add your control notification handler code here
	if (m_pIntReadData==NULL)
	{
		MessageBox("先读取磁盘数据");
		return;
	}
	CFile file("DiskData.dat",CFile::modeWrite|CFile::modeCreate);
	file.Write(m_pIntReadData,m_Filelen);	
	file.Close();
}

void CDiskreadwriteDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	if (m_pIntOrginalData!=NULL)
		delete[] m_pIntOrginalData;
	if (m_pIntReadData!=NULL)
		delete[] m_pIntReadData;
}

void CDiskreadwriteDlg::OnBasedontimeBtn()//读取配置文件 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	m_Sectornum=0;
	int datalen=0;
	CFileDialog fileDlg(TRUE);
	fileDlg.m_ofn.lpstrFilter="CFG FILE(*.cfg)\0*.cfg\0All File(*.*)\0*.*";
	fileDlg.m_ofn.lpstrTitle="打开";
	char ch1[256],ch2[256];
	memset(ch1,0,256);
	memset(ch2,0,256);
	CString strinput;
	CString strtemp1;
	CString strtemp2;
	CString strfirst;
	if (IDOK==fileDlg.DoModal())
	{
		FILE* fp=fopen(fileDlg.GetFileName(),"r");
		if (fp!=NULL)
		{
			strinput=m_StartTime.Format("%H:%M:%S");
			int linenum=m_Timelong/4+(m_Timelong%4 ? 1:0);//要读的行数
			fgets(ch1,256,fp);//读第一行
			strtemp1=ch1;
			strtemp1=strtemp1.Mid(0,strtemp1.Find(','));//该行起始时间
			while (1)
			{
				fgets(ch2,256,fp);//下一行
				strtemp2=ch2;
				strtemp2=strtemp2.Mid(0,strtemp2.Find(','));//下一行起始时间
				if (strtemp1<=strinput&&strtemp2>strinput)//若该行(ch1)的开始时间小于欲查询的开始时间并且下一行的开始时间大于欲查询的开始时间,则从该行开始进行读取
				{
					strfirst=ch1;
					strfirst=strfirst.Mid(strfirst.Find(',')+1,strfirst.GetLength());
					m_StartRector=atoi(strfirst.Mid(0,strfirst.Find(',')));//取第二个字段起始扇区
					strfirst=strfirst.Mid(strfirst.Find(',')+1,strfirst.GetLength());//第三个字段数据长度
					datalen=atoi(strfirst);//该行的数据长度
					m_Sectornum+=( datalen/512+(datalen%512 ? 1 : 0) );
					for(int i=0;i<linenum-1;i++)
					{
						fgets(ch1,256,fp);
						strtemp1=ch1;
						strtemp1=strtemp1.Mid(strtemp1.Find(',')+1,strtemp1.GetLength());//第二个字段
						strtemp1=strtemp1.Mid(strtemp1.Find(',')+1,strtemp1.GetLength());//最后一个字段
						datalen=atoi(strtemp1);//该行要读取的长度
						m_Sectornum+=( datalen/512+(datalen%512 ? 1 : 0) );
						if (feof(fp))//若到文件末尾
						{
							MessageBox("到cfg文件底了,可能没有满足要求时间段长度的记录");
							break;
						}
						
					}
					break;
				}
				strcpy(ch1,ch2);//上一个时间段的值作为ch1
				strtemp1=strtemp2;
				if (feof(fp))//若到文件末尾
				{
					strtemp1=ch1;
					strtemp1=strtemp1.Mid(0,strtemp1.Find(','));
					if (strinput==strtemp1)
					{
						strtemp1=strtemp1.Mid(strtemp1.Find(',')+1,strtemp1.GetLength());//第二个字段
						strtemp1=strtemp1.Mid(strtemp1.Find(',')+1,strtemp1.GetLength());//最后一个字段
						datalen=atoi(strtemp1);//该行要读取的长度
						m_Sectornum+=( datalen/512+(datalen%512 ? 1 : 0) );
					}
					else
						MessageBox("到文件底了,没有该时间段的记录");
					break;
				}
			}
			fclose(fp);
			m_EndRector=m_StartRector+m_Sectornum-1;
			strtemp1.Format("首地址%d,扇区个数%d,尾地址%d",m_StartRector,m_Sectornum,m_EndRector);
			MessageBox(strtemp1);
		}
		else
		{
			MessageBox("can't open the file");
			return;
		}
	}
	UpdateData(FALSE);
}

void CDiskreadwriteDlg::OnReadbasedontimeBtn()//读取磁盘数据
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	char cTemp[5];
	BYTE udisk;
	CWriteRead wr;
	m_DiskNum.GetLBText(m_DiskNum.GetCurSel(),cTemp);
	udisk=cTemp[2]-96;//变成与`的偏移量

	m_pIntReadData=new short[m_Sectornum*512];
	if(!wr.ReadSectors(udisk,m_StartRector,m_Sectornum,m_pIntReadData))
	{
		MessageBox("读取失败","错误",MB_OK|MB_ICONERROR);
		return;
	}
	UpdateData(FALSE);
	m_crf=RGB(255,0,0);
	Invalidate();
//	Sleep(1000);
//	m_crf=RGB(255,0,0);
//	Invalidate();
	SetTimer(1,1000,NULL);
}

void CDiskreadwriteDlg::ShowDisk(COLORREF crf)
{
	CPaintDC dc(this);
	CRect rect;
	GetDlgItem(IDC_DISKSHOW_STATIC)->GetWindowRect(&rect);
	ScreenToClient(&rect);
	CBrush br(crf);
	CBrush* pOldBrush=dc.SelectObject(&br);
	dc.Ellipse(rect);
	dc.SelectObject(pOldBrush);
}

void CDiskreadwriteDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	m_crf=RGB(0,255,0);
	Invalidate();
	KillTimer(1);
	CDialog::OnTimer(nIDEvent);
}

⌨️ 快捷键说明

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