![]() |
Draw line sample
1.DrawLineView.cpp
// DrawLineView.cpp : implementation of the CDrawLineView class // #include "stdafx.h" #include "DrawLine.h" #include "DrawLineDoc.h" #include "DrawLineView.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CDrawLineView IMPLEMENT_DYNCREATE(CDrawLineView, CView) BEGIN_MESSAGE_MAP(CDrawLineView, CView) //{{AFX_MSG_MAP(CDrawLineView) ON_WM_LBUTTONDOWN() ON_WM_MOUSEMOVE() ON_WM_LBUTTONUP() //}}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() ///////////////////////////////////////////////////////////////////////////// // CDrawLineView construction/destruction CDrawLineView::CDrawLineView() { // TODO: add construction code here m_Drag = 0; m_HCursor = AfxGetApp()->LoadStandardCursor(IDC_CROSS); } CDrawLineView::~CDrawLineView() { } BOOL CDrawLineView::PreCreateWindow(CREATESTRUCT& cs) { // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW,0, (HBRUSH)::GetStockObject(WHITE_BRUSH),0); return CView::PreCreateWindow(cs); } ///////////////////////////////////////////////////////////////////////////// // CDrawLineView drawing void CDrawLineView::OnDraw(CDC* pDC) { CDrawLineDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // TODO: add draw code for native data here COLORREF cr0; cr0 = RGB(255,0,0); CPen *pen=new CPen(PS_SOLID,1,cr0); CBrush *brush=new CBrush(cr0); CPen *oldpen=pDC->SelectObject(pen); CBrush *oldBrush=pDC->SelectObject(brush); pDC->MoveTo(50,50); pDC->LineTo(750,50); pDC->LineTo(750,550); pDC->LineTo(50,550); pDC->LineTo(50,50); pDC->SelectObject(oldpen); pDC->SelectObject(oldBrush); delete pen; delete brush; } ///////////////////////////////////////////////////////////////////////////// // CDrawLineView printing BOOL CDrawLineView::OnPreparePrinting(CPrintInfo* pInfo) { // default preparation return DoPreparePrinting(pInfo); } void CDrawLineView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add extra initialization before printing } void CDrawLineView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add cleanup after printing } ///////////////////////////////////////////////////////////////////////////// // CDrawLineView diagnostics #ifdef _DEBUG void CDrawLineView::AssertValid() const { CView::AssertValid(); } void CDrawLineView::Dump(CDumpContext& dc) const { CView::Dump(dc); } CDrawLineDoc* CDrawLineView::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDrawLineDoc))); return (CDrawLineDoc*)m_pDocument; } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CDrawLineView message handlers void CDrawLineView::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default m_pPrev = point; //当前位置 m_pOrigin = point; //起点位置 SetCapture(); //保证鼠标信息发送到视窗 m_Drag = 1; //拖动状态标识 RECT rect; GetClientRect(&rect); //获取客户区坐标 ClientToScreen(&rect); //客户区坐标转换为屏幕坐标 ClipCursor(&rect); //光标限定在客户区内 // CView::OnLButtonDown(nFlags, point); } void CDrawLineView::OnMouseMove(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default SetCursor(m_HCursor); if(m_Drag) { CClientDC dc(this); dc.SetROP2(R2_NOT); //设置异或绘图模式 dc.MoveTo(m_pOrigin); dc.LineTo(m_pPrev); dc.MoveTo(m_pOrigin); dc.LineTo(point); m_pPrev = point; } // CView::OnMouseMove(nFlags, point); } void CDrawLineView::OnLButtonUp(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default if(m_Drag) { m_Drag = 0; //设置非拖动状态 ReleaseCapture(); //释放鼠标恢复正常 ClipCursor(NULL); //让鼠标任意移动 } // CView::OnLButtonUp(nFlags, point); } |
回复: Draw line sample
2.DrawLineView.h
// DrawLineView.h : interface of the CDrawLineView class // ///////////////////////////////////////////////////////////////////////////// #if !defined(AFX_DRAWLINEVIEW_H__92BA1170_1137_4E72_A93C_9FBD819536CA__INCLUDED_) #define AFX_DRAWLINEVIEW_H__92BA1170_1137_4E72_A93C_9FBD819536CA__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 class CDrawLineView : public CView { protected: // create from serialization only int m_Drag; HCURSOR m_HCursor; CPoint m_pPrev; CPoint m_pOrigin; CDrawLineView(); DECLARE_DYNCREATE(CDrawLineView) // Attributes public: CDrawLineDoc* GetDocument(); // Operations public: // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CDrawLineView) public: virtual void OnDraw(CDC* pDC); // overridden to draw this view 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); //}}AFX_VIRTUAL // Implementation public: virtual ~CDrawLineView(); #ifdef _DEBUG virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const; #endif protected: // Generated message map functions protected: //{{AFX_MSG(CDrawLineView) afx_msg void OnLButtonDown(UINT nFlags, CPoint point); afx_msg void OnMouseMove(UINT nFlags, CPoint point); afx_msg void OnLButtonUp(UINT nFlags, CPoint point); //}}AFX_MSG DECLARE_MESSAGE_MAP() }; #ifndef _DEBUG // debug version in DrawLineView.cpp inline CDrawLineDoc* CDrawLineView::GetDocument() { return (CDrawLineDoc*)m_pDocument; } #endif ///////////////////////////////////////////////////////////////////////////// //{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ will insert additional declarations immediately before the previous line. #endif // !defined(AFX_DRAWLINEVIEW_H__92BA1170_1137_4E72_A93C_9FBD819536CA__INCLUDED_) |
所有的时间均为北京时间。 现在的时间是 08:49 AM. |