📄 drawpictureview.cpp
字号:
// DrawPictureView.cpp : implementation of the CDrawPictureView class
//
#include "stdafx.h"
#include "DrawPicture.h"
#include "DrawPictureDoc.h"
#include "DrawPictureView.h"
#include "SettingProperty.h"
#include "resource.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDrawPictureView
IMPLEMENT_DYNCREATE(CDrawPictureView, CView)
BEGIN_MESSAGE_MAP(CDrawPictureView, CView)
ON_WM_CONTEXTMENU()
//{{AFX_MSG_MAP(CDrawPictureView)
ON_COMMAND(ID_MENUITEM32776, OnSettingProperty)
ON_COMMAND(ID_MENUITEM32771, OnDrawLine)
ON_COMMAND(ID_MENUITEM32772, OnDrawRectangle)
ON_COMMAND(ID_MENUITEM32773, OnDrawRRectangle)
ON_COMMAND(ID_MENUITEM32774, OnDrawEllipse)
ON_COMMAND(ID_MENUITEM32775, OnDrawCopyPencil)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_COMMAND(ID_MENUITEM32785, OnDrawREllipse)
ON_WM_CREATE()
ON_COMMAND(ID_MENUITEM32792, OnDrawPolygen)
ON_WM_LBUTTONDBLCLK()
ON_WM_RBUTTONDOWN()
ON_COMMAND(ID_MENUITEM32799, OnDrawBezier)
ON_COMMAND(ID_MENUITEM32800, OnRefresh)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDrawPictureView construction/destruction
CDrawPictureView::CDrawPictureView()
{
// TODO: add construction code here
m_ptOrigin =m_ptEnd=0;
m_bDraw =FALSE;
m_uLineWidth=1;
m_nLineStyle=PS_SOLID;
m_color =RGB(0,0,0);
m_nDrawType =0;
m_bDrawPencil=FALSE;
m_bDrawPolygen=FALSE;
m_pointNumber=0;
}
CDrawPictureView::~CDrawPictureView()
{
}
BOOL CDrawPictureView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
// cs.lpszClass=AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW,LoadCursor(NULL,IDC_CROSS),(HBRUSH)GetStockObject(WHITE_BRUSH),0);
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CDrawPictureView drawing
void CDrawPictureView::OnDraw(CDC* pDC)
{
CDrawPictureDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
pDC->TextOut(10,20,(_T("画图时可选择画图类型和属性 ;")));
pDC->TextOut(10,40,(_T("菜单栏, 工具栏, 鼠标右键均可选择 !")));
m_nDrawType=1;
}
/////////////////////////////////////////////////////////////////////////////
// CDrawPictureView printing
BOOL CDrawPictureView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CDrawPictureView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CDrawPictureView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CDrawPictureView diagnostics
#ifdef _DEBUG
void CDrawPictureView::AssertValid() const
{
CView::AssertValid();
}
void CDrawPictureView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CDrawPictureDoc* CDrawPictureView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDrawPictureDoc)));
return (CDrawPictureDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CDrawPictureView message handlers
//主要的画图程序
void CDrawPictureView::DrawPicture(CDC * pDC, int nPictureShape, CPoint ptOrigin, CPoint ptEnd)
{
CPen pen(m_nLineStyle,m_uLineWidth,m_color);
CPen * OldPen=pDC->SelectObject(&pen);
CBrush * pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
CBrush * pOldBrush=pDC->SelectObject(pBrush);
switch(nPictureShape)
{
case 1:
//画直线
pDC->MoveTo(ptOrigin);
pDC->LineTo(ptEnd);
break;
case 2:
//画矩形
pDC->Rectangle(CRect(ptOrigin,ptEnd));
break;
case 3:
//画圆角矩形
pDC->RoundRect(CRect(ptOrigin,ptEnd),CPoint(20,20));
break;
case 4:
//画椭圆
pDC->Ellipse(CRect(ptOrigin,ptEnd));
break;
case 5:
//画圆
if(abs(ptEnd.y-ptOrigin.y)>abs(ptEnd.x-ptOrigin.x))
{
ptEnd.y=ptOrigin.y+ptEnd.x-ptOrigin.x;
}
else
{
ptEnd.x=ptOrigin.x+ptEnd.y-ptOrigin.y;
}
pDC->Ellipse(CRect(ptOrigin,ptEnd));
break;
}
pDC->SelectObject(OldPen);
pDC->SelectObject(pOldBrush);
}
//设置线的属性
void CDrawPictureView::OnSettingProperty()
{
// TODO: Add your command handler code here
CSettingProperty proDlg;
proDlg.m_nLineStyle=m_nLineStyle;
proDlg.m_uLineWidth=m_uLineWidth;
proDlg.m_crColor=m_color;
if(IDOK==proDlg.DoModal())
{
m_nLineStyle=proDlg.m_nLineStyle;
m_uLineWidth=proDlg.m_uLineWidth;
m_color=proDlg.m_crColor;
}
}
//画直线
void CDrawPictureView::OnDrawLine()
{
// TODO: Add your command handler code here
m_nDrawType=1;
m_bDrawPencil=false;
}
//画矩形
void CDrawPictureView::OnDrawRectangle()
{
// TODO: Add your command handler code here
m_nDrawType=2;
m_bDrawPencil=false;
}
//画圆角矩形
void CDrawPictureView::OnDrawRRectangle()
{
// TODO: Add your command handler code here
m_nDrawType=3;
m_bDrawPencil=false;
}
//画椭圆
void CDrawPictureView::OnDrawEllipse()
{
// TODO: Add your command handler code here
m_nDrawType=4;
m_bDrawPencil=false;
}
//画圆
void CDrawPictureView::OnDrawREllipse()
{
// TODO: Add your command handler code here
m_nDrawType=5;
m_bDrawPencil=false;
}
//仿铅笔
void CDrawPictureView::OnDrawCopyPencil()
{
// TODO: Add your command handler code here
m_nDrawType=6;
m_bDrawPencil=true;
}
//鼠标左键按下,捕捉起始点的位置
void CDrawPictureView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(m_nDrawType!=0)
{
m_ptOrigin=m_ptEnd=point;
m_bDraw=true;
if (m_nDrawType==7)
{
m_pointArray[m_pointNumber]=point;
m_pointNumber++;
if (m_bDrawPolygen==FALSE)
{
m_firstPoint=point;
m_bDrawPolygen=TRUE;
}
m_bDraw=FALSE;
}
}
else
{
MessageBox(_T("请选择画图的类型 !"));
}
CView::OnLButtonDown(nFlags, point);
}
//鼠标左键弹起,画出最终的图形
void CDrawPictureView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(true==m_bDraw)
{
m_bDraw=false;
CClientDC dc(this);
// if(m_nDrawType!=7)
// DrawPicture(&dc,m_nDrawType,m_ptOrigin,m_ptEnd);
}
CView::OnLButtonUp(nFlags, point);
}
//响应鼠标移动的消息,并实时显示图形
void CDrawPictureView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CClientDC dc(this);
if(true==m_bDraw)
{
if(true==m_bDrawPencil)
{
CPen pen(m_nLineStyle,m_uLineWidth,m_color);
CPen * OldPen=dc.SelectObject(&pen);
CBrush * pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
CBrush * pOldBrush=dc.SelectObject(pBrush);
dc.MoveTo(m_ptOrigin);
dc.LineTo(point);
m_ptOrigin=point;
dc.SelectObject(OldPen);
dc.SelectObject(pOldBrush);
}
else
{
int drawMode=dc.GetROP2(); //原有绘图模式
dc.SetROP2(R2_NOTXORPEN); //设置当前绘图模式
DrawPicture(&dc,m_nDrawType,m_ptOrigin,m_ptEnd);
//重新绘线
m_ptEnd=point;
DrawPicture(&dc,m_nDrawType,m_ptOrigin,m_ptEnd);
dc.SetROP2(drawMode); //恢复绘图模式
}
}
if (m_nDrawType==7&&m_bDrawPolygen==TRUE)
{
CPen pen(m_nLineStyle,m_uLineWidth,m_color);
CPen * OldPen=dc.SelectObject(&pen);
int drawMode=dc.GetROP2(); //原有绘图模式
dc.SetROP2(R2_NOTXORPEN); //设置当前绘图模式
dc.MoveTo(m_ptOrigin); //
dc.LineTo(m_ptEnd); //上次鼠标移动到临时终点位置的临时线
m_ptEnd=point; //为临时终点赋值当前坐标值
dc.MoveTo(m_ptOrigin);
dc.LineTo(m_ptEnd); //这两行代码为从直线起点到直线终点画一条直线
dc.SetROP2(drawMode); //恢复绘图模式
dc.SelectObject(OldPen);
}
CView::OnMouseMove(nFlags, point);
}
void CDrawPictureView::OnContextMenu(CWnd*, CPoint point)
{
// CG: This block was added by the Pop-up Menu component { if (point.x == -1 && point.y == -1){ //keystroke invocation CRect rect; GetClientRect(rect); ClientToScreen(rect); point = rect.TopLeft(); point.Offset(5, 5); } CMenu menu; VERIFY(menu.LoadMenu(CG_IDR_POPUP_DRAW_PICTURE_VIEW)); CMenu* pPopup = menu.GetSubMenu(0); ASSERT(pPopup != NULL); CWnd* pWndPopupOwner = this; while (pWndPopupOwner->GetStyle() & WS_CHILD) pWndPopupOwner = pWndPopupOwner->GetParent(); pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, pWndPopupOwner); }
}
int CDrawPictureView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
SetClassLong(m_hWnd,GCL_HCURSOR,(LONG)LoadCursor(NULL,IDC_CROSS));
return 0;
}
void CDrawPictureView::OnDrawPolygen()
{
// TODO: Add your command handler code here
if (m_pointNumber==0)
{
// Invalidate();
}
m_nDrawType=7;
m_bDrawPencil=false;
}
void CDrawPictureView::OnLButtonDblClk(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if (m_nDrawType==7)
{
m_bDrawPolygen=false;
CClientDC dc(this);
CPen pen(m_nLineStyle,m_uLineWidth,m_color);
CPen * OldPen=dc.SelectObject(&pen);
dc.MoveTo(m_firstPoint);
dc.LineTo(point);
dc.SelectObject(OldPen);
}
CView::OnLButtonDblClk(nFlags, point);
}
void CDrawPictureView::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CView::OnRButtonDown(nFlags, point);
}
void CDrawPictureView::OnDrawBezier()
{
// TODO: Add your command handler code here
CClientDC dc(this);
CPen pen(m_nLineStyle,m_uLineWidth,RGB(255,0,0));
CPen * OldPen=dc.SelectObject(&pen);
dc.PolyBezierTo(m_pointArray,m_pointNumber);
for (int i=0; i<m_pointNumber; i++)
{
m_pointArray[i]=0;
}
m_pointNumber=0;
dc.SelectObject(OldPen);
}
void CDrawPictureView::OnRefresh()
{
// TODO: Add your command handler code here
Invalidate();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -