📄 hexviewview.cpp
字号:
// map to printer font metrics
HDC hDCFrom = ::GetDC(NULL);
lf.lfHeight = ::MulDiv(lf.lfHeight, pDC->GetDeviceCaps(LOGPIXELSY),
::GetDeviceCaps(hDCFrom, LOGPIXELSY));
lf.lfWidth = ::MulDiv(lf.lfWidth, pDC->GetDeviceCaps(LOGPIXELSX),
::GetDeviceCaps(hDCFrom, LOGPIXELSX));
::ReleaseDC(NULL, hDCFrom);
// create it, if it fails we just use the printer's default.
if (m_cfPrintFont)
delete m_cfPrintFont;
if (m_cfPrintBoldFont)
delete m_cfPrintBoldFont;
m_cfPrintFont = new CFont;
m_cfPrintBoldFont = new CFont;
m_cfPrintFont->CreateFontIndirect(&lf);
lf.lfUnderline = TRUE;
m_cfPrintBoldFont->CreateFontIndirect(&lf);
CFont *pOldFont = pDC->SelectObject(m_cfPrintFont);
TEXTMETRIC tm;
pDC->GetTextMetrics(&tm);
int nPageHeight = pDC->GetDeviceCaps(VERTRES);
m_iFontHeight = tm.tmHeight + tm.tmExternalLeading;
m_iWndLines = nPageHeight / m_iFontHeight - 5;
CHexviewDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
m_iFirstLine = 0;
int nTotalLines = (pDoc->TotalFileLength() + 15) / 16;
pInfo->SetMaxPage((nTotalLines + m_iWndLines - 1) / m_iWndLines);
pDC->SelectObject(pOldFont);
SYSTEMTIME sysTime;
GetSystemTime(&sysTime);
CString strDate;
GetDateFormat(NULL, LOCALE_NOUSEROVERRIDE, &sysTime, NULL,
strDate.GetBuffer(255), 255);
strDate.ReleaseBuffer();
GetTimeFormat(NULL, LOCALE_NOUSEROVERRIDE, &sysTime, NULL,
m_strTimePrint.GetBuffer(255), 255);
m_strTimePrint.ReleaseBuffer();
m_strTimePrint = strDate + " " + m_strTimePrint;
}
void CHexviewView::OnEndPrinting(CDC* pDC, CPrintInfo* /*pInfo*/)
{
UpdateScrollSizes();
}
/////////////////////////////////////////////////////////////////////////////
// CHexviewView diagnostics
#ifdef _DEBUG
void CHexviewView::AssertValid() const
{
CScrollView::AssertValid();
}
void CHexviewView::Dump(CDumpContext& dc) const
{
CScrollView::Dump(dc);
}
CHexviewDoc* CHexviewView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CHexviewDoc)));
return (CHexviewDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CHexviewView message handlers
void CHexviewView::OnDestroy()
{
delete m_cfFixedFont;
CScrollView::OnDestroy();
}
void CHexviewView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo)
{
// TODO: Add your specialized code here and/or call the base class
CScrollView::OnPrepareDC(pDC, pInfo);
CPoint pt = pDC->GetViewportOrg();
pt.y = 0;
pDC->SetViewportOrg(pt);
}
void CHexviewView::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
switch (nSBCode)
{
case SB_TOP:
m_iFirstLine = 0;
break;
case SB_BOTTOM:
m_iFirstLine = m_iLastLine;
break;
case SB_LINEUP:
m_iFirstLine--;
break;
case SB_PAGEUP:
m_iFirstLine -= m_iWndLines;
break;
case SB_LINEDOWN:
m_iFirstLine++;
break;
case SB_PAGEDOWN:
m_iFirstLine += m_iWndLines;
break;
case SB_THUMBPOSITION:
case SB_THUMBTRACK:
m_iFirstLine = (nPos * m_iLastLine) / (m_iFontHeight * m_iViewHeight);
if (m_iFirstLine == m_iLastLine - m_iWndLines - 1)
m_iFirstLine = m_iLastLine - m_iWndLines;
break;
default:
return;
}
if (m_iFirstLine >= m_iLastLine - m_iWndLines)
{
m_iFirstLine = m_iLastLine - m_iWndLines;
}
if (m_iFirstLine < 0) m_iFirstLine = 0;
if (m_iWndLines >= m_iLastLine)
{
m_iFirstLine = 0;
}
int iPos = 0;
if (m_iWndLines < m_iLastLine)
iPos = ((m_iViewHeight * m_iFirstLine) / (m_iLastLine - m_iWndLines)) * m_iFontHeight;
TRACE("First Line: %d, Last Line: %d, Window lines: %d, Position: %d\n",
m_iFirstLine, m_iLastLine, m_iWndLines, iPos);
SetScrollPos(SB_VERT, iPos, TRUE);
Invalidate(TRUE);
}
void CHexviewView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
switch (nChar)
{
case VK_HOME:
OnVScroll(SB_TOP, 0, NULL);
break;
case VK_END:
OnVScroll(SB_BOTTOM, 0, NULL);
break;
case VK_UP:
OnVScroll(SB_LINEUP, 0, NULL);
break;
case VK_DOWN:
OnVScroll(SB_LINEDOWN, 0, NULL);
break;
case VK_PRIOR:
OnVScroll(SB_PAGEUP, 0, NULL);
break;
case VK_NEXT:
OnVScroll(SB_PAGEDOWN, 0, NULL);
break;
default:
CScrollView::OnKeyDown(nChar, nRepCnt, nFlags);
break;
}
}
void CHexviewView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
{
CFont *pOldFont = pDC->SelectObject(m_cfPrintFont);
// get string to show as "filename" in header/footer
LPCTSTR pszFileName = GetDocument()->GetPathName();
if (pszFileName[0] == 0)
pszFileName = GetDocument()->GetTitle();
// go thru global CPageSetupDlg to format the header and footer
CString strHeader;
strHeader = pszFileName;
CString strFooter;
CHexviewDoc* pDoc = GetDocument();
int nTotalLines = (pDoc->TotalFileLength() + 15) / 16;
wsprintf(strFooter.GetBuffer(81), "Page %d of %d", pInfo->m_nCurPage,
(nTotalLines + m_iWndLines - 1) / m_iWndLines);
strFooter.ReleaseBuffer();
TEXTMETRIC tm;
pDC->GetTextMetrics(&tm);
int cyChar = tm.tmHeight;
CRect rectPage = pInfo->m_rectDraw;
// draw and exclude space for header
if (!strHeader.IsEmpty())
{
pDC->TextOut(rectPage.left, rectPage.top, strHeader);
int nAlign = pDC->SetTextAlign(TA_NOUPDATECP | TA_RIGHT | TA_TOP);
strHeader = _T("Printed by HexView: http://www.funduc.com");
pDC->TextOut(rectPage.right, rectPage.top, strHeader);
pDC->SetTextAlign(nAlign);
rectPage.top += cyChar + cyChar / 4;
pDC->MoveTo(rectPage.left, rectPage.top);
pDC->LineTo(rectPage.right, rectPage.top);
rectPage.top += cyChar / 4;
pInfo->m_rectDraw.top = rectPage.top;
}
// allow space for footer
if (!strFooter.IsEmpty())
pInfo->m_rectDraw.bottom -= cyChar + cyChar/4 + cyChar/4;
int nPageHeight = pDC->GetDeviceCaps(VERTRES);
int iFontHeight = tm.tmHeight + tm.tmExternalLeading;
int iFontWidth = tm.tmMaxCharWidth;
int iWndLines = nPageHeight / iFontHeight - 5;
int iFirstLine = (pInfo->m_nCurPage -1) * iWndLines;
iFirstLine += (2 * (pInfo->m_nCurPage -1));
// Print the rows for the current page.
int yTopOfPage = (pInfo->m_nCurPage -1) * iWndLines
* iFontHeight;
ASSERT_VALID(pDoc);
DispData(pDC, iFirstLine, pDoc->TotalFileLength(), m_cfPrintFont,
m_cfPrintBoldFont, iWndLines + 4, iFontHeight, 0, iFontWidth, 2);
// draw footer
if (!strFooter.IsEmpty())
{
rectPage.bottom -= cyChar;
pDC->TextOut(rectPage.left, rectPage.bottom, strFooter);
int nAlign = pDC->SetTextAlign(TA_NOUPDATECP | TA_RIGHT | TA_TOP);
pDC->TextOut(rectPage.right, rectPage.bottom, m_strTimePrint);
pDC->SetTextAlign(nAlign);
rectPage.bottom -= cyChar / 4;
pDC->MoveTo(rectPage.left, rectPage.bottom);
pDC->LineTo(rectPage.right, rectPage.bottom);
rectPage.bottom -= cyChar / 4;
}
pDC->SelectObject(pOldFont);
}
void CHexviewView::OnSize(UINT nType, int cx, int cy)
{
UpdateScrollSizes();
CScrollView::OnSize(nType, cx, cy);
}
void CHexviewView::OnViewNextblock()
{
CHexviewDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (pDoc->GetNextBlock(m_iCurrentOffset))
{
m_iFirstLine = 0;
m_iLastLine = (pDoc->BlockLength(m_iCurrentOffset) + 15) / 16;
SetScrollPos(SB_VERT, 0, TRUE);
UpdateScrollSizes();
Invalidate();
}
}
void CHexviewView::OnViewPreviousblock()
{
CHexviewDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (pDoc->GetPrevBlock(m_iCurrentOffset))
{
m_iFirstLine = 0;
m_iLastLine = (pDoc->BlockLength(m_iCurrentOffset) + 15) / 16;
SetScrollPos(SB_VERT, 0, TRUE);
UpdateScrollSizes();
Invalidate();
}
}
void CHexviewView::OnUpdateViewNextblock(CCmdUI* pCmdUI)
{
CHexviewDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
int iCurrentOffset = m_iCurrentOffset;
pCmdUI->Enable(pDoc->GetNextBlock(iCurrentOffset));
}
void CHexviewView::OnUpdateViewPreviousblock(CCmdUI* pCmdUI)
{
CHexviewDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
int iCurrentOffset = m_iCurrentOffset;
pCmdUI->Enable(pDoc->GetPrevBlock(iCurrentOffset));
}
void CHexviewView::OnViewGoto()
{
CGotoDlg dlgGoto;
CHexviewDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (pDoc->m_lStartOffset != -1) // Show the initial offset
dlgGoto.m_lNewOffset = pDoc->m_lStartOffset;
else // Show the top of current block
dlgGoto.m_lNewOffset = m_iCurrentOffset;
if (dlgGoto.DoModal() == IDOK)
{
// Bogus offset
if (dlgGoto.m_lNewOffset > pDoc->TotalFileLength())
{
m_iFirstLine = 0;
}
else
{
// Find the right block
while (dlgGoto.m_lNewOffset > m_iCurrentOffset + pDoc->BlockLength(m_iCurrentOffset))
{
pDoc->GetNextBlock(m_iCurrentOffset);
}
m_iFirstLine = (dlgGoto.m_lNewOffset - m_iCurrentOffset) / 16;
m_iLastLine = (pDoc->BlockLength(m_iCurrentOffset) + 15) / 16;
UpdateScrollSizes();
}
Invalidate();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -