天天看點

CBrush實作畫矩形

在建立的MFC單文檔檔案中,選擇類視圖,在其中選擇視圖類,定義一個新的點類型變量myPoint,并在視圖類構造函數中初始化為零。在滑鼠單擊按下事件内将目前點賦給myPoint,在擡起事件中,實作畫矩形。

// lessonMyBrushView.cpp : implementation of the ClessonMyBrushView class
//

#include "stdafx.h"
// SHARED_HANDLERS can be defined in an ATL project implementing preview, thumbnail
// and search filter handlers and allows sharing of document code with that project.
#ifndef SHARED_HANDLERS
#include "lessonMyBrush.h"
#endif

#include "lessonMyBrushDoc.h"
#include "lessonMyBrushView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// ClessonMyBrushView

IMPLEMENT_DYNCREATE(ClessonMyBrushView, CView)

BEGIN_MESSAGE_MAP(ClessonMyBrushView, CView)
  // Standard printing commands
  ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
  ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
  ON_COMMAND(ID_FILE_PRINT_PREVIEW, &ClessonMyBrushView::OnFilePrintPreview)
  ON_WM_CONTEXTMENU()
  ON_WM_RBUTTONUP()
  ON_WM_LBUTTONUP()
  ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()

// ClessonMyBrushView construction/destruction

ClessonMyBrushView::ClessonMyBrushView()
  : myPoint(0)
{
  // TODO: add construction code here

}

ClessonMyBrushView::~ClessonMyBrushView()
{
}

BOOL ClessonMyBrushView::PreCreateWindow(CREATESTRUCT& cs)
{
  // TODO: Modify the Window class or styles here by modifying
  //  the CREATESTRUCT cs

  return CView::PreCreateWindow(cs);
}

// ClessonMyBrushView drawing

void ClessonMyBrushView::OnDraw(CDC* /*pDC*/)
{
  ClessonMyBrushDoc* pDoc = GetDocument();
  ASSERT_VALID(pDoc);
  if (!pDoc)
    return;

  // TODO: add draw code for native data here
}


// ClessonMyBrushView printing


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

BOOL ClessonMyBrushView::OnPreparePrinting(CPrintInfo* pInfo)
{
  // default preparation
  return DoPreparePrinting(pInfo);
}

void ClessonMyBrushView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
  // TODO: add extra initialization before printing
}

void ClessonMyBrushView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
  // TODO: add cleanup after printing
}

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

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


// ClessonMyBrushView diagnostics

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

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

ClessonMyBrushDoc* ClessonMyBrushView::GetDocument() const // non-debug version is inline
{
  ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(ClessonMyBrushDoc)));
  return (ClessonMyBrushDoc*)m_pDocument;
}
#endif //_DEBUG


// ClessonMyBrushView message handlers


void ClessonMyBrushView::OnLButtonUp(UINT nFlags, CPoint point)
{
  // TODO: Add your message handler code here and/or call default
  //CBitmap bitmap;
  CBrush brush(RGB(255,0,0));
  CClientDC ccdc(this);
  ccdc.FillRect(CRect(myPoint,point),&brush);

  CView::OnLButtonUp(nFlags, point);
}


void ClessonMyBrushView::OnLButtonDown(UINT nFlags, CPoint point)
{
  // TODO: Add your message handler code here and/or call default
  myPoint=point;
  CView::OnLButtonDown(nFlags, point);
}      

繼續閱讀