?? drawexamview.cpp
字號:
// DrawExamView.cpp : implementation of the CDrawExamView class
//
#include "stdafx.h"
#include "DrawExam.h"
#include "DrawExamDoc.h"
#include "DrawExamView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDrawExamView
IMPLEMENT_DYNCREATE(CDrawExamView, CView)
BEGIN_MESSAGE_MAP(CDrawExamView, CView)
//{{AFX_MSG_MAP(CDrawExamView)
ON_COMMAND(ID_BTN_SELECT, OnBtnSelect)
ON_COMMAND(ID_BTN_LINE, OnBtnLine)
ON_COMMAND(ID_BTN_RECT, OnBtnRect)
ON_COMMAND(ID_BTN_FILLEDRECT, OnBtnFilledrect)
ON_COMMAND(ID_BTN_ELLIPSE, OnBtnEllipse)
ON_COMMAND(ID_BTN_FILLEDELLIPSED, OnBtnFilledellipsed)
ON_WM_LBUTTONDOWN()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONUP()
ON_COMMAND(ID_BTN_FRONTCOLOR1, OnBtnFrontcolor1)
ON_COMMAND(ID_BTN_FRONTCOLOR2, OnBtnFrontcolor2)
ON_COMMAND(ID_BTN_FRONTCOLOR3, OnBtnFrontcolor3)
ON_COMMAND(ID_BTN_FRONTCOLOR4, OnBtnFrontcolor4)
ON_COMMAND(ID_BTN_FRONTCOLOR5, OnBtnFrontcolor5)
ON_COMMAND(ID_BTN_FRONTCOLOR6, OnBtnFrontcolor6)
ON_COMMAND(ID_BTN_FRONTCOLOR7, OnBtnFrontcolor7)
ON_COMMAND(ID_BTN_FRONTCOLOR8, OnBtnFrontcolor8)
ON_COMMAND(ID_BTN_BACKCOLOR1, OnBtnBackcolor1)
ON_COMMAND(ID_BTN_BACKCOLOR2, OnBtnBackcolor2)
ON_COMMAND(ID_BTN_BACKCOLOR3, OnBtnBackcolor3)
ON_COMMAND(ID_BTN_BACKCOLOR4, OnBtnBackcolor4)
ON_COMMAND(ID_BTN_BACKCOLOR5, OnBtnBackcolor5)
ON_COMMAND(ID_BTN_BACKCOLOR6, OnBtnBackcolor6)
ON_COMMAND(ID_BTN_BACKCOLOR7, OnBtnBackcolor7)
ON_COMMAND(ID_BTN_BACKCOLOR8, OnBtnBackcolor8)
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDrawExamView construction/destruction
CDrawExamView::CDrawExamView()
{
// TODO: add construction code here
}
CDrawExamView::~CDrawExamView()
{
}
BOOL CDrawExamView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CDrawExamView drawing
void CDrawExamView::OnDraw(CDC* pDC)
{
CDrawExamDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CDrawExamView diagnostics
#ifdef _DEBUG
void CDrawExamView::AssertValid() const
{
CView::AssertValid();
}
void CDrawExamView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CDrawExamDoc* CDrawExamView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDrawExamDoc)));
return (CDrawExamDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CDrawExamView message handlers
/*
*函數介紹:畫直線
*入口參數:firstPoint : 表示開始點
lastPoint : 表示上一個點
currentPoint : 表示當前點
*出口參數:(無)
*返回值:(無)
*/
void CDrawExamView::DrawLine(CPoint firstPoint,CPoint lastPoint,CPoint currentPoint)
{
CDC *pDC = new CClientDC(this);
//擦去舊線
pDC->SetROP2(R2_NOT);
pDC->MoveTo(m_firstPoint);
pDC->LineTo(m_lastPoint);
//畫新線
pDC->MoveTo(m_firstPoint);
pDC->LineTo(currentPoint);
delete pDC;
}
/*
*函數介紹:畫矩形
*入口參數:firstPoint : 表示開始點
lastPoint : 表示上一個點
currentPoint : 表示當前點
filled : 表示矩形是否填充
*出口參數:(無)
*返回值:(無)
*/
void CDrawExamView::DrawRectangle(CPoint firstPoint,CPoint lastPoint,CPoint currentPoint,bool filled)
{
CDC *pDC = new CClientDC(this);
//創建背景色刷子
CBrush brush(m_backColor);
//如果是實心矩形
if (filled)
{
pDC->SelectObject(brush);
}
else
{
//創建空刷子
pDC->SelectStockObject(NULL_BRUSH);
}
//擦去舊矩形
pDC->SetROP2(R2_NOT);
pDC->Rectangle(m_firstPoint.x,m_firstPoint.y,m_lastPoint.x,m_lastPoint.y);
//畫新矩形
pDC->Rectangle(m_firstPoint.x,m_firstPoint.y,currentPoint.x,currentPoint.y);
//釋放刷子GDI對象
brush.DeleteObject();
delete pDC;
}
/*
*函數介紹:畫圓
*入口參數:firstPoint : 表示開始點
lastPoint : 表示上一個點
currentPoint : 表示當前點
filled : 表示圓是否填充
*出口參數:(無)
*返回值:(無)
*/
void CDrawExamView::DrawEllipse(CPoint firstPoint,CPoint lastPoint,CPoint currentPoint,bool filled)
{
CDC *pDC = new CClientDC(this);
//創建背景刷子
CBrush brush(m_backColor);
//如果實心圓
if (filled)
{
pDC->SelectObject(brush);
}
else
{
//創建空刷子
pDC->SelectStockObject(NULL_BRUSH);
}
//擦去舊圓
pDC->SetROP2(R2_NOT);
pDC->Ellipse(m_firstPoint.x,m_firstPoint.y,m_lastPoint.x,m_lastPoint.y);
//畫新圓
pDC->Ellipse(m_firstPoint.x,m_firstPoint.y,currentPoint.x,currentPoint.y);
//釋放刷子GDI對象
brush.DeleteObject();
delete pDC;
}
void CDrawExamView::OnPaint()
{
CPaintDC dc(this);
CRect rectClient;
GetClientRect(rectClient);
//創建m_oldDC設備環境
if (m_oldDC.GetSafeHdc() == NULL)
{
m_oldDC.CreateCompatibleDC(&dc);
m_oldBmp.CreateCompatibleBitmap(&dc,rectClient.Width(),rectClient.Height());
m_oldDC.SelectObject(m_oldBmp);
CBrush brushBack(RGB(255,255,255));
m_oldDC.FillRect(rectClient,&brushBack);
brushBack.DeleteObject();
}
//創建m_tmpDC設備環境
if (m_tmpDC.GetSafeHdc() == NULL)
{
m_tmpDC.CreateCompatibleDC(&dc);
m_tmpBmp.CreateCompatibleBitmap(&dc,rectClient.Width(),rectClient.Height());
m_tmpDC.SelectObject(m_tmpBmp);
m_tmpDC.PatBlt(0,0,rectClient.Width(),rectClient.Height(),WHITENESS);
}
//將m_oldDC設備環境繪制到窗體上
dc.BitBlt(0,0,rectClient.Width(),rectClient.Height(),&m_oldDC,0,0,SRCCOPY);
}
//鼠標按下左鍵事件
void CDrawExamView::OnLButtonDown(UINT nFlags, CPoint point)
{
//標識開始繪圖
m_bBeginDraw = TRUE;
//設置開始點坐標
m_firstPoint = point;
//設置結束點坐標
m_lastPoint = point;
//設置鼠標為捕捉狀態
SetCapture();
CView::OnLButtonDown(nFlags, point);
}
//鼠標移動事件
void CDrawExamView::OnMouseMove(UINT nFlags, CPoint point)
{
if (m_bBeginDraw)
{
switch (m_drawID)
{
//表示處于選擇狀態
case 0:
break;
//表示劃直線
case 1 :
{
DrawLine(m_firstPoint,m_lastPoint,point);
//設置末尾節點
m_lastPoint = point;
break;
}
//表示畫空心矩形
case 2 :
{
DrawRectangle(m_firstPoint,m_lastPoint,point,false);
m_lastPoint = point;
break;
}
//表示畫實心矩形
case 3:
{
DrawRectangle(m_firstPoint,m_lastPoint,point,true);
m_lastPoint = point;
break;
}
//表示空心圓
case 4:
{
DrawEllipse(m_firstPoint,m_lastPoint,point,false);
m_lastPoint = point;
}
//表示畫實心圓
case 5:
{
DrawEllipse(m_firstPoint,m_lastPoint,point,false);
m_lastPoint = point;
}
}
}
CView::OnMouseMove(nFlags, point);
}
//鼠標松開事件
void CDrawExamView::OnLButtonUp(UINT nFlags, CPoint point)
{
//釋放鼠標捕捉
ReleaseCapture();
m_bBeginDraw = false;
CRect rectClient;
GetClientRect(&rectClient);
CPen newPen(PS_SOLID,1,m_frontColor);
CBrush newBrush(m_backColor);
CPen *pOldPen;
CBrush *pOldBrush;
switch(m_drawID)
{
case 1://畫新線
{
pOldPen = m_tmpDC.SelectObject(&newPen);
m_tmpDC.MoveTo(m_firstPoint);
m_tmpDC.LineTo(m_lastPoint);
m_tmpDC.SelectObject(pOldPen);
break;
}
case 2://畫空心矩形
{
m_tmpDC.SelectStockObject(NULL_BRUSH);
pOldPen = m_tmpDC.SelectObject(&newPen);
m_tmpDC.Rectangle(m_firstPoint.x,m_firstPoint.y,m_lastPoint.x,m_lastPoint.y);
m_tmpDC.SelectObject(pOldPen);
break;
}
case 3://畫實心矩形
{
pOldBrush = m_tmpDC.SelectObject(&newBrush);
pOldPen = m_tmpDC.SelectObject(&newPen);
m_tmpDC.Rectangle(m_firstPoint.x,m_firstPoint.y,m_lastPoint.x,m_lastPoint.y);
m_tmpDC.SelectObject(pOldPen);
m_tmpDC.SelectObject(pOldBrush);
break;
}
case 4://畫空心圓
{
m_tmpDC.SelectStockObject(NULL_BRUSH);
pOldPen = m_tmpDC.SelectObject(&newPen);
m_tmpDC.Ellipse(m_firstPoint.x,m_firstPoint.y,m_lastPoint.x,m_lastPoint.y);
m_tmpDC.SelectObject(pOldPen);
break;
}
case 5://畫實心圓
{
pOldBrush = m_tmpDC.SelectObject(&newBrush);
pOldPen = m_tmpDC.SelectObject(&newPen);
m_tmpDC.Ellipse(m_firstPoint.x,m_firstPoint.y,m_lastPoint.x,m_lastPoint.y);
m_tmpDC.SelectObject(pOldPen);
m_tmpDC.SelectObject(pOldBrush);
break;
}
}
//將m_tmpDC內容繪制到m_oldDC上
m_oldDC.BitBlt(0, 0, rectClient.Width(), rectClient.Height(),
&m_tmpDC, 0, 0, SRCCOPY) ;
//刪除畫筆GDI對象
newPen.DeleteObject();
//刪除畫刷GDI對象
newBrush.DeleteObject();
//觸發窗體重畫
Invalidate();
CView::OnLButtonUp(nFlags, point);
}
//設置繪圖為選擇狀態
void CDrawExamView::OnBtnSelect()
{
m_drawID = 0 ;
}
//設置繪圖為畫直線
void CDrawExamView::OnBtnLine()
{
m_drawID = 1;
}
//設置繪圖為畫空心矩形
void CDrawExamView::OnBtnRect()
{
m_drawID = 2;
}
//設置繪圖為畫實心矩形
void CDrawExamView::OnBtnFilledrect()
{
m_drawID = 3;
}
//設置繪圖為畫空心矩形
void CDrawExamView::OnBtnEllipse()
{
m_drawID = 4;
}
//設置繪圖為畫實心矩形
void CDrawExamView::OnBtnFilledellipsed()
{
m_drawID = 5;
}
//設置前景色
void CDrawExamView::OnBtnFrontcolor1()
{
m_frontColor = RGB(0,0,0);
}
void CDrawExamView::OnBtnFrontcolor2()
{
m_frontColor = RGB(255,255,255);
}
void CDrawExamView::OnBtnFrontcolor3()
{
m_frontColor = RGB(255,0,0);
}
void CDrawExamView::OnBtnFrontcolor4()
{
m_frontColor = RGB(0,255,0);
}
void CDrawExamView::OnBtnFrontcolor5()
{
m_frontColor = RGB(0,0,255);
}
void CDrawExamView::OnBtnFrontcolor6()
{
m_frontColor = RGB(0,255,255);
}
void CDrawExamView::OnBtnFrontcolor7()
{
m_frontColor = RGB(255,0,255);
}
void CDrawExamView::OnBtnFrontcolor8()
{
m_frontColor = RGB(255,255,0);
}
//設置背景色
void CDrawExamView::OnBtnBackcolor1()
{
m_backColor = RGB(0,0,0);
}
void CDrawExamView::OnBtnBackcolor2()
{
m_backColor = RGB(255,255,255);
}
void CDrawExamView::OnBtnBackcolor3()
{
m_backColor = RGB(255,0,0);
}
void CDrawExamView::OnBtnBackcolor4()
{
m_backColor = RGB(0,255,0);
}
void CDrawExamView::OnBtnBackcolor5()
{
m_backColor = RGB(0,0,255);
}
void CDrawExamView::OnBtnBackcolor6()
{
m_backColor = RGB(0,255,255);
}
void CDrawExamView::OnBtnBackcolor7()
{
m_backColor = RGB(255,0,255);
}
void CDrawExamView::OnBtnBackcolor8()
{
m_backColor = RGB(255,255,0);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -