在vc2010内使用GDI绘制直线

程序源代码:

步骤:

1、 现在视图类中定义一个点类的变量m_point,用来保存当鼠标左键按下时的坐标
2、 在视图类的构造函数中将m_point初始化为0
3、定义鼠标左键按下的消息:将鼠标左键按下时的点左边赋值给m_point
4、定义鼠标左键弹起来的消息响应函数:此是主要代码。

开始怎么写都没有出结果,后来才知道加载消息事件代码,需要使用“Add class”实现,自己写代码不行的。


// lessonMyLineView.cpp : implementation of the ClessonMyLineView 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 "lessonMyLine.h"
#endif

#include "lessonMyLineDoc.h"
#include "lessonMyLineView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// ClessonMyLineView

IMPLEMENT_DYNCREATE(ClessonMyLineView, CView)

BEGIN_MESSAGE_MAP(ClessonMyLineView, 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, &ClessonMyLineView::OnFilePrintPreview)
	ON_WM_CONTEXTMENU()
	ON_WM_RBUTTONUP()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
END_MESSAGE_MAP()

// ClessonMyLineView construction/destruction

ClessonMyLineView::ClessonMyLineView()
	: m_point(0)
{
	// TODO: add construction code here

}

ClessonMyLineView::~ClessonMyLineView()
{
}

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

	return CView::PreCreateWindow(cs);
}

// ClessonMyLineView drawing

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

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


// ClessonMyLineView printing


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

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

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

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

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

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


// ClessonMyLineView diagnostics

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

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

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


// ClessonMyLineView message handlers


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


void ClessonMyLineView::OnLButtonUp(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	/*   //111111111111使用MFC的CDC实现绘画
	CDC *pcdc=GetDC();
		pcdc->MoveTo(m_point);
		pcdc->LineTo(point);
		ReleaseDC(pcdc);
		*/
		/*  //22222222222222222222使用MFC的CClentDC实现绘画
		CClientDC ccdc(this);
		ccdc.MoveTo(m_point);
		ccdc.LineTo(point);
		*/
	/*   //333333333333333333
	//使用MFC的CWindowDC实现
	//注意此时不区分客户区和功能区
	//CWindowDC cwdc(GetParent()); 
CWindowDC cwdc(this);
		cwdc.MoveTo(m_point);
		cwdc.LineTo(point);    
		*/
	/*CPen pen(PS_SOLID,10,RGB(255,0,0));
		
		CClientDC ccdc(this);
		CPen *pOpen=ccdc.SelectObject(&pen);

		ccdc.MoveTo(m_point);
		ccdc.LineTo(point);

		ccdc.SelectObject(pOpen);*/
	CView::OnLButtonUp(nFlags, point);
}

效果如图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

superdont

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值