📄 view.cpp
字号:
/////////////////////////////////////////////////////////////////////////////
#include "StdAfx.h"
#include "Doc.h"
#include "View.h"
#include "FontUI.h"
#include "resource.h"
#include <afxpriv.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
////////////////////////////////////////////////////////////////
// CPictureView
//
const TCHAR SETTINGS[] = _T("Settings");
IMPLEMENT_DYNCREATE(CPictureView, CFolderView)
BEGIN_MESSAGE_MAP(CPictureView, CFolderView)
ON_WM_ERASEBKGND()
ON_WM_SIZE()
ON_COMMAND_RANGE(ID_VIEW_TOFIT, ID_VIEW100, OnViewScale)
ON_UPDATE_COMMAND_UI_RANGE(ID_VIEW_TOFIT, ID_VIEW100, OnUpdateViewScale)
END_MESSAGE_MAP()
CFont CPictureView::g_font;
CPictureView::CPictureView()
{
m_iHowScale = ID_VIEW_TOFIT;
m_iPage = PAGE_IMAGE;
if (!g_font.m_hObject) {
// Restore font from profile
if (!CFontUI().GetProfileFont(SETTINGS, "Font", g_font)) {
// Use 8pt Courier (monospace) default
g_font.CreatePointFont(100,"Courier");
}
}
}
CPictureView::~CPictureView()
{
CFontUI().WriteProfileFont(SETTINGS, "Font", g_font);
}
void CPictureView::OnInitialUpdate()
{
CFolderView::OnInitialUpdate();
// Compute size/position of text rectangle
SetScrollSizes();
// Show folder view controls
GetFolderFrame()->ShowControls(CFolderFrame::bestFit);
// Hide folder view controls
// GetFolderFrame()->ShowControls(CFolderFrame::hide);
// size window perfectly around frame
GetParentFrame()->RecalcLayout();
}
//////////////////
// Set scroll sizes based on picture. Page size = client hieight/width;
// line size = 1/10 of this.
//
void CPictureView::SetScrollSizes()
{
CRect rcClient;
GetClientRect(&rcClient);
CRect rcImage;
GetImageRect(rcImage);
CSize szTotal = rcImage.Size();
CSize szPage = rcClient.Size();
CSize szLine = szPage;
szLine.cx /= 10;
szLine.cy /= 10;
CScrollView::SetScrollSizes(MM_TEXT, szTotal, szPage, szLine);
Invalidate();
}
//////////////////
// View was sized: readjust scroll sizes if I'm in "zoom to fit" mode
//
void CPictureView::OnSize(UINT nType, int cx, int cy)
{
CScrollView::OnSize(nType, cx, cy);
if (m_iHowScale==ID_VIEW_TOFIT) {
SetScrollSizes();
}
}
//////////////////
// Erase the background. This is required in case the image is smaller than
// the client area, to paint the extra background. Use clipping to avoid
// flicker.
//
BOOL CPictureView::OnEraseBkgnd(CDC* pDC)
{
if (m_iPage!=PAGE_IMAGE)
return CFolderView::OnEraseBkgnd(pDC);
CPictureDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// get client rectangle
CRect rcClient;
GetClientRect(&rcClient);
CRect rc = rcClient;
// get image rectangle
CRect rcImage;
GetImageRect(rcImage);
rc = rcImage;
CPoint pt = pDC->GetViewportOrg();
CSize sz = GetTotalSize();
// create clipping region
CRgn clipRgn;
clipRgn.CreateRectRgnIndirect(&rcClient);
pDC->SelectClipRgn(&clipRgn);
pDC->ExcludeClipRect(&rcImage);
CBrush brush(RGB(0,0,0)); // black
pDC->FillRect(&rcClient, &brush);
pDC->SelectClipRgn(NULL);
return TRUE;
}
//////////////////
// Draw the picture -- call CPicture to do it.
//
/*
void CPictureView::OnDraw(CDC* pDC)
{
CPictureDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CPicture* ppic = pDoc->GetPicture();
ASSERT(ppic);
if (*ppic) {
CRect rc;
GetImageRect(rc);
ppic->Render(pDC,rc);
}
}
*/
void CPictureView::OnDraw(CDC* pDC)
{
CPictureDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CPicture* ppic = pDoc->GetPicture();
ASSERT(ppic);
if (*ppic) {
if (m_iPage==PAGE_IMAGE) {
CRect rc;
GetImageRect(rc);
ppic->Render(pDC,rc);
} else if (m_iPage==PAGE_BITMAPINFO) {
// DrawInfo(dc, p);
DrawInfo(*pDC, ppic);
} else {
DrawHex(*pDC, ppic);
}
}
}
/*
//////////////////
// Draw the bitmap. Be careful to specify foreground/background.
// Following bitmap, display fields in BITMAPINFOHEADER
//
void CPictureView::OnDraw(CDC* pDC)
{
CDC& dc = *pDC;
CPicture* p = GetPicture();
if (p) {
if (m_iPage==PAGE_IMAGE) {
if (*p) {
CRect rc;
GetImageRect(rc);
p->Render(pDC,rc);
}
} else if (m_iPage==PAGE_BITMAPINFO) {
DrawInfo(dc, p);
} else {
DrawHex(dc, p);
}
}
}
*/
//////////////////
// Get image rectangle, scaled for current zoom factor.
//
void CPictureView::GetImageRect(CRect& rc)
{
CPictureDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CPicture* ppic = pDoc->GetPicture();
ASSERT(ppic);
if (!ppic || !*ppic) {
rc.SetRect(0,0,0,0);
} else if (m_iHowScale==ID_VIEW_TOFIT) {
GetClientRect(&rc);
} else {
CSize sz = ppic->GetImageSize();
switch (m_iHowScale) {
case ID_VIEW25:
sz.cx >>= 2;
sz.cy >>= 2;
break;
case ID_VIEW33:
sz.cx /= 3;
sz.cy /= 3;
break;
case ID_VIEW50:
sz.cx >>= 1;
sz.cy >>= 1;
break;
case ID_VIEW75:
sz.cx = (sz.cx * 3)/4;
sz.cy = (sz.cy * 3)/4;
break;
}
rc.SetRect(0,0,sz.cx,sz.cy);
}
}
//////////////////
// Handle zoom command.
//
void CPictureView::OnViewScale(UINT nID)
{
if (m_iHowScale != nID) {
m_iHowScale = nID;
ScrollToPosition(CPoint(0,0));
OnInitialUpdate();
}
}
//////////////////
// Update zoom menu -- check the whichever zoom factor I'm at now.
//
void CPictureView::OnUpdateViewScale(CCmdUI* pCmdUI)
{
pCmdUI->SetCheck(pCmdUI->m_nID == m_iHowScale);
}
//////////////////
// Helper fn to draw the formatted BITMAPINFOHEADER text.
// Because Windows is so brain damaged, this requires manually
// outputting each line with TabbedTextOut. DrawText is what I would ideally
// use here, but Windows doesn't let you use DT_CALCRECT and DT_TABSTOP at
// the same time. I need both, because I'm setting a custom tab stop to make
// my text align, but also need to calculate the size of the text in advance
// so I can set my scroll ranges.
//
void CPictureView::DrawInfo(CDC& dc, CPicture* p, UINT nFormat)
{
BITMAPINFOHEADER bmi;
memset(&bmi,0,sizeof(bmi));
// Format BITMAPINFOHEADER into a string
CString sCompression="none";
if (bmi.biCompression)
sCompression.Format("0x%04x",bmi.biCompression);
CString buf, text;
CPictureDoc* pDoc = GetDocument();
long hmWidth,hmHeight; // HIMETRIC units
p->GetHIMETRICSize(hmWidth, hmHeight);
short type;
p->GetPicType(type);
if (type){
CString stype = type == PICTYPE_BITMAP ? "背景(bitmap)" :
type==PICTYPE_METAFILE ? "原背景路径(metafile)" : "";
CString spicpath = (LPCTSTR)pDoc->GetPathName();
if (spicpath.GetLength() == 0 ){
spicpath = _T("");
}
buf.Format("%s%s \n%s%s \n%s%ld \n%s%ld\n",
"任务编辑一:",
(LPCTSTR)stype,
"任务编辑二:",
(LPCTSTR)spicpath,
"任务编辑三:",
hmWidth,
"任务编辑四:",
hmHeight);
text += buf;
}
// Now draw it using current font
CFont *pOldFont = dc.SelectObject(&g_font); // select my font
DRAWTEXTPARAMS dtp;
memset(&dtp,0,sizeof(dtp));
dtp.cbSize = sizeof(dtp); // size of struct
dtp.iTabLength = 20; // avg 20 chars per tab
CRect rc;
GetClientRect(&rc);
DrawTextEx(dc, (TCHAR*)(LPCTSTR)text, -1, &rc,
nFormat|DT_EXPANDTABS|DT_TABSTOP, &dtp);
dc.SelectObject(pOldFont);
}
//////////////////
// Draw hex mode display
//
void CPictureView::DrawHex(CDC& dc, CPicture *p)
{
CRect rc;
GetClientRect(&rc);
CString s;
if (m_iPage==PAGE_HEX) {
s = _T("待定:");
} else {
s.Format(_T("(第 %d 页)"), m_iPage-2);
}
CFont *pOldFont = dc.SelectObject(&g_font); // select my font
dc.DrawText(s, &rc, DT_CENTER|DT_VCENTER|DT_SINGLELINE);
dc.SelectObject(pOldFont);
}
void CPictureView::OnChangedFolder(int iPage)
{
m_iPage = iPage;
SetScrollSizes();
Invalidate();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -