关于滚动条的问题?????

Blue_123 2004-07-13 10:35:24
我用SDI编了一个编辑二进制程序,视图继承的ScrollView,遇到了两大问题,一个是拖动滚动条数据显示不对,滚动条位置不对;一个是不能用键盘的上下左右和pageup、pagedown键控制滚动,程序如下,请大虾们指点一下,谢谢!!!!!!

// HexView.cpp : implementation of the CHexView class

CHexView::CHexView()
{
// TODO: add construction code here
memset(&m_logfont, 0, sizeof(m_logfont));
m_nPointSize = 120;
_tcscpy(m_logfont.lfFaceName, _T("Fixedsys"));

// start out with a system font

CWindowDC dc(NULL);
m_logfont.lfHeight = ::MulDiv(m_nPointSize, dc.GetDeviceCaps(LOGPIXELSY), 720);
m_logfont.lfPitchAndFamily = FIXED_PITCH;

m_pFont = new CFont;
m_pFont->CreateFontIndirect(&m_logfont);
m_pPrintFont = NULL;
m_bPrinting = FALSE;
}

CHexView::~CHexView()
{
if (m_pFont != NULL)
delete m_pFont;
if (m_pPrintFont != NULL)
delete m_pPrintFont;
}


/////////////////////////////////////////////////////////////////////////////
// CHexView drawing

void CHexView::OnDraw(CDC* pDC)
{
CHexDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
CString strRender;
CFont* pOldFont;
CSize ScrolledSize;
// int nStartLine;
int nHeight;
CRect ScrollRect;
CPoint ScrolledPos = GetScrollPosition();

if (m_bPrinting)
{
// find the first line for this page

ScrolledSize = CSize(m_nPageWidth, m_nPageHeight);
ScrollRect = CRect(0, ScrolledPos.y,
ScrolledSize.cx,
ScrolledSize.cy + ScrolledPos.y);
pOldFont = pDC->SelectObject(m_pPrintFont);
nStartLine = m_nPrintLine;
}
else
{
CRect rectClient;
GetClientRect(&rectClient);

// figure out how high each line is

pOldFont = pDC->SelectObject(m_pFont);
nHeight = MeasureFontHeight(m_pFont, pDC);

// find a starting line based on scrolling

ScrolledSize = CSize(rectClient.Width(), rectClient.Height());
ScrollRect = CRect(rectClient.left, ScrolledPos.y,
rectClient.right,
ScrolledSize.cy + ScrolledPos.y);
nStartLine = ScrolledPos.y/16;

// make sure we are drawing where we should

ScrollRect.top = nStartLine*nHeight;
ScrollRect.bottom =ScrollRect.top+ScrolledSize.cy;
}

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);
}

void CHexView::OnInitialUpdate()
{
CHexDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// CScrollView::OnInitialUpdate();

// CSize sizeTotal;
// TODO: calculate the total size of this view
// sizeTotal.cx = sizeTotal.cy = 100;
// SetScrollSizes(MM_TEXT, sizeTotal);

CSize sizeTotal(5000, pDoc->m_lFileSize);
SetScrollSizes(MM_TEXT, sizeTotal);

ResizeParentToFit();

CScrollView::OnInitialUpdate();
}


int CHexView::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;
}

void CHexView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
{
// TODO: Add your specialized code here and/or call the base class
CHexDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);

CRect rect;
GetClientRect(&rect);
UINT m_PageRows; //一页的行数
UINT m_PageCols;
m_PageRows=rect.Height()/16;
m_PageCols=rect.Width()/8;

CSize sizeTotal(300, pDoc->m_lFileSize);
CSize sizePage(m_PageCols*8,m_PageRows*16);
CSize sizeLine(8*3,16);
SetScrollSizes(MM_TEXT, sizeTotal,sizePage,sizeLine);
}

void CHexView::OnSize(UINT nType, int cx, int cy)
{
CScrollView::OnSize(nType, cx, cy);

// TODO: Add your message handler code here
CHexDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);

CRect rect;
GetClientRect(&rect);
UINT m_PageRows; //一页的行数
UINT m_PageCols;
m_PageRows=rect.Height()/16;
m_PageCols=rect.Width()/8;

CSize sizeTotal(300, pDoc->m_lFileSize);
CSize sizePage(m_PageCols*8,m_PageRows*16);
CSize sizeLine(8*3,16);
SetScrollSizes(MM_TEXT, sizeTotal,sizePage,sizeLine);

}


void CHexView::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// TODO: Add your message handler code here and/or call default
SCROLLINFO si;
si.cbSize = sizeof(si);
si.fMask = SIF_ALL;
VERIFY(GetScrollInfo(SB_VERT, &si));

CScrollView::OnVScroll(nSBCode, nPos, pScrollBar);
}

// HexDoc.cpp : implementation of the CHexDoc class
/////////////////////////////////////////////////////////////////////////////
// CHexDoc construction/destruction

CHexDoc::CHexDoc()
{
// TODO: add one-time construction code here
m_pFile = NULL;
m_lFileSize = 0L;
}

CHexDoc::~CHexDoc()
{
if (m_pFile != NULL)
{
m_pFile->Close();
delete m_pFile;
m_pFile = NULL;
}
}

BOOL CHexDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;

// TODO: add reinitialization code here
// (SDI documents will reuse this document)

return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CHexDoc serialization
// CHexDoc commands

BOOL CHexDoc::ReadLine(CString &strLine, int nLength, LONG lOffset)
{
LONG lPosition;

if (lOffset != -1L)
lPosition = m_pFile->Seek(lOffset, CFile::begin);
else
lPosition = m_pFile->GetPosition();

if (lPosition == -1L)
{
TRACE2("CHexViewDoc::ReadLine returns FALSE Seek(%8.81X, %8.81X)\n",
lOffset, lPosition);
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)\n",
nLength, nReturned);
delete pszBuffer;
return FALSE;
}

CString strTemp;
strLine = strTemp;

for (int nIndex = 0; nIndex < nReturned; nIndex++)
{
if (nIndex == 0)
strTemp.Format(_T("%2.2X"), pszBuffer[nIndex]);
else if (nIndex % 16 == 0)
strTemp.Format(_T("=%2.2X"), pszBuffer[nIndex]);
else
strTemp.Format(_T(" %2.2X"), pszBuffer[nIndex]);

strLine += strTemp;
}

delete pszBuffer;
return TRUE;
}

BOOL CHexDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
if (!CDocument::OnOpenDocument(lpszPathName))
return FALSE;

// TODO: Add your specialized creation code here
if (m_pFile != NULL)
{
m_pFile->Close();
delete m_pFile;
}

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;
}

...全文
114 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧