1、建立名為HexViewer的SDI項目,在創建單文檔時把Base Class(基類)改為CScrollView。CScrollView就是CView的一個派生類,通過它可以很容易地處理水平和垂直滾動。
2、打開和讀取文件
首先,在CHexViewerDoc類中定義兩個成員變量,分別用于檢索要打開文件的指針以及文件的大小,并初始化這些值,同時添加用于清除的代碼。
class CHexViewerDoc : public CDocument
{
..
public:
CFile* m_pFile;
LONG m_lFileSize;
..
};
CHexViewerDoc::CHexViewerDoc()
{
m_pFile=NULL;
m_lFileSize=0L;
}
CHexViewerDoc::~CHexViewerDoc()
{
if (m_pFile!=NULL)
{
m_pFile->Close();
delete m_pFile;
m_pFile=NULL;
m_lFileSize=0L;
}
}
在使用MFC框架的因公程序中打開文檔后,還會調用CHexViewerDoc::OnOpenDocument虛函數。
BOOL CHexViewerDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
if (!CDocument::OnOpenDocument(lpszPathName))
return FALSE;
if (m_pFile!=NULL)
{
m_pFile->Close();
delete m_pFile;
m_pFile=NULL;
m_lFileSize=0L;
}
try
{
m_pFile=new CFile(lpszPathName,CFile::modeRead|CFile::typeBinary);
}
catch(CFileException* e)
{
CString strError;
strError.Format(_T("Couldn't open file:%d"),_sys_errlist[e->m_lOsError]);
AfxMessageBox(strError);
return FALSE;
}
m_lFileSize=m_pFile->GetLength();
return TRUE;
}
最后添加一個輔助成員函數,視圖調用該函數從打開的文件中讀取一行數據。
BOOL CHexViewerDoc::ReadLine(CString& strLine,int nLength,LONG lOffset/* =-1L */)
{
LONG lPosition;
if(lOffset!=-1L)
lPosition=m_pFile->Seek(lOffset,CFile::begin);
else
lPosition=m_pFile->GetPosition();
if(lPosition==-1L)
{
TRACE("CHexViewDoc::ReadLine returns False Seek (%8.8lX,%8.8lX)\n",lOffset,lPosition);
//這個宏是用來調試用的,在VC的窗口的輸入框中會看到輸出的內容!只有在debug版本中才會運行
return FALSE;
}
BYTE* pszBuffer=new BYTE[nLength];
int nReturned=m_pFile->Read(pszBuffer,nLength);
if(nReturned<=0)
{
TRACE2("CHexViewDoc::ReadLine returns FALSE Read (%d,%d)",nLength,nReturned);
delete pszBuffer;
return FALSE;
}
CString strTemp;
CString strCharsIn;
strTemp.Format(_T("%8.8lX - "),lPosition);
strLine=strTemp;
for(int nIndex=0;nIndex<nReturned;nIndex++)
{
if(nIndex==0)
strTemp.Format(_T("%2.2X"),pszBuffer[nIndex]);
//以2位的十六進制讀取
else if(nIndex%16==0)
strTemp.Format(_T("=%2.2X"),pszBuffer[nIndex]);
else if(nIndex%8==0)
strTemp.Format(_T("-%2.2X"),pszBuffer[nIndex]);
else
strTemp.Format(_T(" %2.2X"),pszBuffer[nIndex]);
if(_istprint(pszBuffer[nIndex]))
strCharsIn+=pszBuffer[nIndex];
else
strCharsIn+=_T('.');
strLine+=strTemp;
}
if(nReturned<nLength)
{
CString strPadding(_T(' '),3*(nLength-nReturned));
strLine+=strPadding;
}
strLine+=_T(" ");
strLine+=strCharsIn;
delete pszBuffer;
return true;
}
3、視圖的編碼
首先,定義一些變量,并對數據的字體進行改變
class CHexViewerView : public CScrollView
{
.
protected:
CFont* m_pFont;
LOGFONT m_logfont;
int m_nPointSize;
int m_nPageHeight;
int m_nPageWidth;
.
};
CHexViewerView::CHexViewerView()
{
// TODO: add construction code here
memset(&m_logfont,0,sizeof(m_logfont));
m_nPointSize=120;
_tcscpy(m_logfont.lfFaceName,_T("Fixedsys"));
//函數原型:char *strcpy( char *strDestination, const char *strSource );
CWindowDC dc(NULL);
m_logfont.lfHeight=::MulDiv(m_nPointSize,dc.GetDeviceCaps(LOGPIXELSY),720);
//MulDiv字體高度值和磅值有如下的換算公式;GetDeviceCaps該函數檢索指定設備的設備指定信息。
//LOGPIXELSY沿屏幕高度每邏輯英寸的像素數,在多顯示器系統中,該值對所顯示器相同;
m_logfont.lfPitchAndFamily=FIXED_PITCH;//指定字體間距和字體族,低端二位指定字體的字符間距
m_pFont=new CFont;
m_pFont->CreateFontIndirect(&m_logfont);
}
CHexViewerView::~CHexViewerView()
{
if (m_pFont!=NULL)
{
delete m_pFont;
}
}
設置滾動的大小,可以向其傳遞映射模式(MM_TEXT顯示文本)和文檔大小(m_lFileSize)
void CHexViewerView::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();
CHexViewerDoc* pDoc=GetDocument();
ASSERT_VALID(pDoc);
CSize sizeTotal(0,pDoc->m_lFileSize);
//sizeTotal.cx = sizeTotal.cy = 100;
SetScrollSizes(MM_TEXT, sizeTotal);
}
給視圖添加為當前設備上下文(DC)計算字體高度(以像素計)的輔助函數。這里的技巧是傳遞-1作為字符串長度。這意味著不進行任何實際繪制操作,但是DrawText函數仍返回指定文本的高度。
int CHexViewerView::MeasureFontHeight(CFont* pFont,CDC* pDC)
{
CFont* pOldFont;
pOldFont=pDC->SelectObject(pFont);
CRect rectDummy;
CString strRender=_T("1234567890ABCDEF- ");
int nHeight=pDC->DrawText(strRender,-1,rectDummy,DT_TOP|DT_SINGLELINE|DT_CALCRECT);
pDC->SelectObject(pOldFont);
return nHeight;
}
最后隊試圖進行修改,用GetScrollPosition返回用戶滾動的位置,用GetClientRect來返回工作區的大小,用MeasureFontHeight返回每行的高度,然后可以用一個for循環通過ReadLine函數來從文檔中檢索每行數據。
void CHexViewerView::OnDraw(CDC* pDC)
{
CHexViewerDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CString strRender;
CFont* pOldFont;
CSize ScrolledSize;
int nStartLine;
int nHeight;
CRect ScrollRect;
CPoint ScrolledPos=GetScrollPosition();//獲取該屏滾動條的位置
CRect rectClient;
GetClientRect(&rectClient);
pOldFont=pDC->SelectObject(m_pFont);
nHeight=MeasureFontHeight(m_pFont,pDC);
ScrolledSize=CSize(rectClient.Width(),rectClient.Height());
ScrollRect=CRect(rectClient.left,ScrolledPos.y,rectClient.right,ScrolledSize.cy+ScrolledPos.y);
nStartLine=ScrolledPos.y/16;
ScrollRect.top=nStartLine*nHeight;
if(pDoc->m_pFile!=NULL)
{
int nLine;
for(nLine=nStartLine;ScrollRect.top<ScrollRect.bottom;nLine++)
{
if(!pDoc->ReadLine(strRender,16,nLine*16))
break;
nHeight=pDC->DrawText(strRender,-1,&ScrollRect,DT_TOP|DT_NOPREFIX|DT_SINGLELINE);
ScrollRect.top+=nHeight;
}
}
pDC->SelectObject(pOldFont);
}
posted on 2009-07-02 11:15
The_Moment 閱讀(1074)
評論(1) 編輯 收藏 引用 所屬分類:
VC實踐