MFC 橡皮筋

// roptestView.h : CroptestView 类的接口
//

#pragma once


class CroptestView : public CView
{
protected: // 仅从序列化创建
    CroptestView();
    DECLARE_DYNCREATE(CroptestView)

// 特性
public:
    CroptestDoc* GetDocument() const;
    CPoint m_start;
    CPoint m_finish;
    CPoint m_prevFinish;
    BOOL m_draw;
    BOOL m_init;
// 操作
public:
    void Draw();
public:
    virtual void OnDraw(CDC* pDC);  // 重写以绘制该视图
    virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
    virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
    virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
    virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);

// 实现
public:
    virtual ~CroptestView();
#ifdef _DEBUG
    virtual void AssertValid() const;
    virtual void Dump(CDumpContext& dc) const;
#endif

protected:

// 生成的消息映射函数
protected:
    afx_msg void OnFilePrintPreview();
    afx_msg void OnRButtonUp(UINT nFlags, CPoint point);
    afx_msg void OnContextMenu(CWnd* pWnd, CPoint point);
    DECLARE_MESSAGE_MAP()
public:
    afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
    afx_msg void OnMouseMove(UINT nFlags, CPoint point);
    afx_msg void OnRButtonDown(UINT nFlags, CPoint point);
};

#ifndef _DEBUG  // roptestView.cpp 中的调试版本
inline CroptestDoc* CroptestView::GetDocument() const
   { return reinterpret_cast<CroptestDoc*>(m_pDocument); }
#endif
// roptestView.cpp : CroptestView 类的实现
//

#include "stdafx.h"
// SHARED_HANDLERS 可以在实现预览、缩略图和搜索筛选器句柄的
// ATL 项目中进行定义,并允许与该项目共享文档代码。
#ifndef SHARED_HANDLERS
#include "roptest.h"
#endif

#include "roptestDoc.h"
#include "roptestView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CroptestView

IMPLEMENT_DYNCREATE(CroptestView, CView)

BEGIN_MESSAGE_MAP(CroptestView, CView)
    // 标准打印命令
    ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
    ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
    ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CroptestView::OnFilePrintPreview)
    ON_WM_CONTEXTMENU()
    ON_WM_RBUTTONUP()
    ON_WM_LBUTTONDOWN()
    ON_WM_MOUSEMOVE()
    ON_WM_RBUTTONDOWN()
END_MESSAGE_MAP()

// CroptestView 构造/析构

CroptestView::CroptestView()
{
    // TODO: 在此处添加构造代码
    m_draw = FALSE;
    m_init = TRUE;
}

CroptestView::~CroptestView()
{
}

BOOL CroptestView::PreCreateWindow(CREATESTRUCT& cs)
{
    // TODO: 在此处通过修改
    //  CREATESTRUCT cs 来修改窗口类或样式

    return CView::PreCreateWindow(cs);
}

// CroptestView 绘制
void CroptestView::Draw()
{
    CDC* pDC = GetDC();
    if (m_draw)
    {
        CPen Pen;
        Pen.CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
        CPen *OldPen = (CPen *)pDC->SelectObject(&Pen);
        pDC->SelectStockObject(NULL_BRUSH);
        pDC->SetBkMode(TRANSPARENT);
        
        int oldRop2 = R2_NOP;
        if (!m_init)
        {
            int oldRop2 = pDC->SetROP2(R2_NOTXORPEN);
            CRect rect1(m_start, m_prevFinish);
            pDC->Rectangle(rect1);
        }

        CRect rect2(m_start, m_finish);
        pDC->Rectangle(rect2);

        m_init = FALSE;
    }

    ReleaseDC(pDC);
}

void CroptestView::OnDraw(CDC* pDC)
{
    CroptestDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    if (!pDoc)
        return;

    // TODO: 在此处为本机数据添加绘制代码
    //Draw();
//     CRect rect;
//     GetClientRect(rect);
//     pDC->FillSolidRect(rect, RGB(0, 0, 255));
}


// CroptestView 打印


void CroptestView::OnFilePrintPreview()
{
#ifndef SHARED_HANDLERS
    AFXPrintPreview(this);
#endif
}

BOOL CroptestView::OnPreparePrinting(CPrintInfo* pInfo)
{
    // 默认准备
    return DoPreparePrinting(pInfo);
}

void CroptestView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
    // TODO: 添加额外的打印前进行的初始化过程
}

void CroptestView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
    // TODO: 添加打印后进行的清理过程
}

void CroptestView::OnRButtonUp(UINT /* nFlags */, CPoint point)
{
    ClientToScreen(&point);
    //OnContextMenu(this, point);
}

void CroptestView::OnContextMenu(CWnd* /* pWnd */, CPoint point)
{
#ifndef SHARED_HANDLERS
    theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
#endif
}


// CroptestView 诊断

#ifdef _DEBUG
void CroptestView::AssertValid() const
{
    CView::AssertValid();
}

void CroptestView::Dump(CDumpContext& dc) const
{
    CView::Dump(dc);
}

CroptestDoc* CroptestView::GetDocument() const // 非调试版本是内联的
{
    ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CroptestDoc)));
    return (CroptestDoc*)m_pDocument;
}
#endif //_DEBUG


// CroptestView 消息处理程序


void CroptestView::OnLButtonDown(UINT nFlags, CPoint point)
{
    // TODO: 在此添加消息处理程序代码和/或调用默认值
    m_draw = TRUE;
    m_start = point;
    CView::OnLButtonDown(nFlags, point);
}


void CroptestView::OnMouseMove(UINT nFlags, CPoint point)
{
    // TODO: 在此添加消息处理程序代码和/或调用默认值
    m_prevFinish = m_finish;
    m_finish = point;
    Draw();
    CView::OnMouseMove(nFlags, point);
}


void CroptestView::OnRButtonDown(UINT nFlags, CPoint point)
{
    // TODO: 在此添加消息处理程序代码和/或调用默认值
    m_draw = FALSE;
    CView::OnRButtonDown(nFlags, point);
}
作者:快雪
本文版权归作者所有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/kuaixue/p/15309524.html