几何尺寸与公差论坛

 找回密码
 注册
查看: 2123|回复: 1

Draw line sample

[复制链接]
发表于 2007-7-22 16:26:17 | 显示全部楼层 |阅读模式
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:reCreateWindow(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:reCreateWindow(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:ump(CDumpContext& dc) const
{
CView:ump(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);
}
 楼主| 发表于 2007-7-22 16:27:59 | 显示全部楼层

回复: 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_)
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|小黑屋|几何尺寸与公差论坛

GMT+8, 2024-12-22 12:33 , Processed in 0.036868 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表