📄 graphicsview.cpp
字号:
// GraphicsView.cpp : implementation of the CGraphicsView class
//
#include "stdafx.h"
#include "Graphics.h"
#include "GraphicsDoc.h"
#include "GraphicsView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CGraphicsView
IMPLEMENT_DYNCREATE(CGraphicsView, CScrollView)
BEGIN_MESSAGE_MAP(CGraphicsView, CScrollView)
//{{AFX_MSG_MAP(CGraphicsView)
ON_COMMAND(IDM_SCALE_11, OnScale11)
ON_COMMAND(IDM_SCALE_12, OnScale12)
ON_COMMAND(IDM_SCALE_21, OnScale21)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CGraphicsView construction/destruction
CGraphicsView::CGraphicsView()
{
m_nScale = 100;
m_pPix = NULL;
}
CGraphicsView::~CGraphicsView()
{
}
BOOL CGraphicsView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CScrollView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CGraphicsView drawing
void CGraphicsView::OnDraw(CDC* pDC)
{
CGraphicsDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!m_pPix)
return;
// get width and height of picture
long hmWidth;
long hmHeight;
m_pPix->get_Width(&hmWidth);
m_pPix->get_Height(&hmHeight);
// Let the DC convert himetric to pixels
SIZE sz;
sz.cx = hmWidth;
sz.cy = hmHeight;
pDC->HIMETRICtoDP (&sz);
sz.cx = (sz.cx * m_nScale) / 100;
sz.cy = (sz.cy * m_nScale) / 100;
RECT rc;
GetClientRect(&rc);
SetScrollSizes(MM_TEXT, sz);
// display the picture
m_pPix->Render(pDC->m_hDC,
0, // Horizontal position of image in the device context
0, // Vertical position of image in the device context
sz.cx, // Horizontal dimension of destination rectangle
sz.cy, // Vertical dimension of destination rectangle
0, // Horizontal offset in source picture
hmHeight, // Vertical offset in source picture
hmWidth, // Amount to copy horizontally in source picture
-hmHeight, // Amount to copy vertically in source picture
&rc); // Pointer to position of destination for a metafile hdc)
}
/////////////////////////////////////////////////////////////////////////////
// CGraphicsView printing
BOOL CGraphicsView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CGraphicsView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CGraphicsView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CGraphicsView diagnostics
#ifdef _DEBUG
void CGraphicsView::AssertValid() const
{
CScrollView::AssertValid();
}
void CGraphicsView::Dump(CDumpContext& dc) const
{
CScrollView::Dump(dc);
}
CGraphicsDoc* CGraphicsView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CGraphicsDoc)));
return (CGraphicsDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CGraphicsView message handlers
void CGraphicsView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
{
if (pSender == this)
return;
//
// If the screen is being cleared, hide the scroll bars.
//
// if (lHint == 1)
// {
// ShowScrollBar(SB_HORZ, FALSE);
// ShowScrollBar(SB_VERT, FALSE);
// }
LPSTREAM pStream = NULL;
DWORD dwSize = GetDocument()->GetGlobalSize();
HGLOBAL hGlobal = GetDocument()->GetGlobalBuffer();
//
// If there's already a picture, dump it
//
if (m_pPix)
{
m_pPix->Release();
m_pPix = NULL;
Invalidate ();
}
if (hGlobal == NULL)
return;
// Create an IStream* from global memory
HRESULT hr = CreateStreamOnHGlobal(hGlobal, TRUE, &pStream);
_ASSERTE(SUCCEEDED(hr) && pStream);
// Create IPicture from image file
hr = ::OleLoadPicture(pStream, dwSize, FALSE, IID_IPicture, (LPVOID *)&m_pPix);
_ASSERTE(SUCCEEDED(hr) && m_pPix);
pStream->Release();
m_nScale = 100;
long hmWidth, hmHeight;
m_pPix->get_Width(&hmWidth);
m_pPix->get_Height(&hmHeight);
Invalidate ();
}
void CGraphicsView::OnScale11()
{
if (m_nScale == 100)
return;
m_nScale = 100;
ScrollToPosition (CPoint (0, 0));
Invalidate ();
}
void CGraphicsView::OnScale12()
{
if (m_nScale == 50)
return;
m_nScale = 50;
ScrollToPosition (CPoint (0, 0));
Invalidate ();
}
void CGraphicsView::OnScale21()
{
if (m_nScale == 200)
return;
m_nScale = 200;
ScrollToPosition (CPoint (0, 0));
Invalidate ();
}
void CGraphicsView::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();
CSize sizeTotal;
// TODO: calculate the total size of this view
sizeTotal.cx = sizeTotal.cy = 100;
SetScrollSizes(MM_TEXT, sizeTotal);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -