MFC画图
// HelloMFCView.cpp : implementation of the CHelloMFCView class // #include "stdafx.h" #include "HelloMFC.h" #include "HelloMFCDoc.h" #include "HelloMFCView.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CHelloMFCView IMPLEMENT_DYNCREATE(CHelloMFCView, CView) BEGIN_MESSAGE_MAP(CHelloMFCView, CView) //{{AFX_MSG_MAP(CHelloMFCView) ON_WM_CREATE() ON_WM_LBUTTONDOWN() ON_WM_LBUTTONUP() ON_WM_MOUSEMOVE() //}}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() ///////////////////////////////////////////////////////////////////////////// // CHelloMFCView construction/destruction CHelloMFCView::CHelloMFCView() { // TODO: add construction code here this->m_ptOrigin=NULL; } CHelloMFCView::~CHelloMFCView() { } BOOL CHelloMFCView::PreCreateWindow(CREATESTRUCT& cs) { // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return CView::PreCreateWindow(cs); } ///////////////////////////////////////////////////////////////////////////// // CHelloMFCView drawing void CHelloMFCView::OnDraw(CDC* pDC) { CHelloMFCDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // TODO: add draw code for native data here } ///////////////////////////////////////////////////////////////////////////// // CHelloMFCView printing BOOL CHelloMFCView::OnPreparePrinting(CPrintInfo* pInfo) { // default preparation return DoPreparePrinting(pInfo); } void CHelloMFCView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add extra initialization before printing } void CHelloMFCView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add cleanup after printing } ///////////////////////////////////////////////////////////////////////////// // CHelloMFCView diagnostics #ifdef _DEBUG void CHelloMFCView::AssertValid() const { CView::AssertValid(); } void CHelloMFCView::Dump(CDumpContext& dc) const { CView::Dump(dc); } CHelloMFCDoc* CHelloMFCView::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CHelloMFCDoc))); return (CHelloMFCDoc*)m_pDocument; } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CHelloMFCView message handlers int CHelloMFCView::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CView::OnCreate(lpCreateStruct) == -1) return -1; // TODO: Add your specialized creation code here // m_btn.Create("吴xx",WS_CHILD | WS_VISIBLE | BS_AUTO3STATE | BS_TOP // ,CRect(0,0,100,200),this,123); //m_btn.ShowWindow(SW_NORMAL); return 0; } //点击左键 void CHelloMFCView::OnLButtonDown(UINT nFlags, CPoint point) { //MessageBox("View Click","提示",MB_OKCANCEL); this->m_ptOrigin = m_ptStart = point; CView::OnLButtonDown(nFlags, point); } void CHelloMFCView::OnLButtonUp(UINT nFlags, CPoint point) { ////////////////////////////////////////////////////////////////////////// /* //Win API方式画线 HDC hdc; hdc=::GetDC(this->m_hWnd); MoveToEx(hdc,m_ptOrigin.x,m_ptOrigin.y,NULL); LineTo(hdc, point.x, point.y); ::ReleaseDC(this->m_hWnd,hdc); */ ////////////////////////////////////////////////////////////////////////// /*CDC *pdc; pdc= this->GetDC(); pdc->MoveTo(this->m_ptOrigin); pdc->LineTo(point); this->ReleaseDC(pdc);*/ ////////////////////////////////////////////////////////////////////////// /*不需要GetDC和ReleaseDC, 在构造函数和析构函数中包含。 CClientDC dc(this); //CClientDC dc(this->GetParent()); dc.MoveTo(m_ptOrigin); dc.LineTo(point);*/ ////////////////////////////////////////////////////////////////////////// /* //CWindowDC dc(this); //CWindowDC dc(this->GetParent()); CWindowDC dc( this->GetDesktopWindow()); dc.MoveTo(this->m_ptOrigin); dc.LineTo(point); */ ////////////////////////////////////////////////////////////////////////// /* CPen pen(PS_DASHDOT,1,RGB(0xff,0xff,0)); CClientDC dc(this); CPen *pOldPen = dc.SelectObject(&pen); dc.MoveTo(m_ptOrigin); dc.LineTo(point); dc.SelectObject(pOldPen); */ ////////////////////////////////////////////////////////////////////////// /*CBrush brush(RGB(0,255,0)); CClientDC dc(this); CRect rect(m_ptOrigin,point); dc.FillRect(&rect,&brush); */ ////////////////////////////////////////////////////////////////////////// /*CBitmap bitmap; bitmap.LoadBitmap(IDB_HUA); CBrush brush(&bitmap); CClientDC dc(this); CRect rect(m_ptOrigin,point); dc.FillRect(&rect,&brush);*/ /* CBrush *brush; brush = CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH)); ///不覆盖之前的图形 CClientDC dc(this); CBrush *pOldBrush =dc.SelectObject(brush) ; dc.Rectangle(CRect(m_ptOrigin,point)); dc.SelectObject(pOldBrush) ; */ CView::OnLButtonUp(nFlags, point); } void CHelloMFCView::OnMouseMove(UINT nFlags, CPoint point) { if(MK_LBUTTON == nFlags){ //左键按下 CClientDC dc(this); dc.MoveTo(m_ptStart); dc.LineTo(point); m_ptStart=point; } CView::OnMouseMove(nFlags, point); }