110 likes | 258 Views
GDI 画图进阶. CMemDC. Memdc.h. Application to use CMemDC: 1. Add the file memdc.h in your project. 2. Add the line #include " memdc.h " to stdafx.h. 3. Add a windows message handler for WM_ERASEBKGND . BOOL CExampleView::OnEraseBkgnd(CDC* pDC) { //return CView::OnEraseBkgnd(pDC);
E N D
CMemDC • Memdc.h
Application to use CMemDC: • 1. Add the file memdc.h in your project. • 2. Add the line #include "memdc.h" to stdafx.h. • 3. Add a windows message handler for WM_ERASEBKGND. • BOOL CExampleView::OnEraseBkgnd(CDC* pDC) • { • //return CView::OnEraseBkgnd(pDC); • return FALSE; • } • 4. Change your OnDraw code to the following: • void CExampleView::OnDraw(CDC* dc) • { • CMemDC pDC(dc); • CExampleDoc* pDoc = GetDocument(); • ASSERT_VALID(pDoc); • // TODO: add draw code for native data here - use pDC • //as the device context to draw to • }
刷新窗口 • 发出消息WM_PAINT,激发OnPaint() and OnDraw() • Invalidate();
GDIUtils.h and GDIUtils.cpp • 封装好的GDI画图类
定义CPen • Cpen pen( PS_SOLID, 2, RGB(0,0,255) ); • CGDISelectObject<CPen> tmpPen(pDC, &pen); • Or • Cpen pen(PS_NULL, 2, RGB(0,0,255) ); • CGDISelectObject<CPen> tmpPen(pDC, &pen);
定义CBrush • CBrush brush(RGB( 255,0,0 ) ); • CGDISelectObject<CBrush> tmpBrush(pDC, &brush); • Or • CBrush brush(HS_CROSS, RGB( 255,0,0 ) ); • CGDISelectObject<CBrush> tmpBrush(pDC, &brush); • Or • pDC->SelectStockObject( NULL_BRUSH );
CColorDialog • void CMiniDrawView::OnButtonFilledColor() • { • CColorDialog dlg; • if (dlg.DoModal() != IDOK) • return; • m_SelectedPolygons.SetFilledColor( dlg.GetColor() ); • Invalidate(); • }
CFileDialog • void CMiniDrawView::OnButtonSaveToFile() • { • char szFilter[] = "WF Files (*.ply)|*.ply||"; • CFileDialog dlg(false,"ply","*.ply", • OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilter,this); • if(dlg.DoModal()==IDOK) • { • m_SelectedPolygons.SaveToFile(dlg.GetFileName().GetBuffer(128)); • } • }
自定义Dialog • Cedit • …