MFC初步教程(二)

绘制简单的图形几乎是所有的VC教程中的必须内容,这里面涉及到几个概念,设备(device context),画刷(Pen)等。CDC是所有设备的父类,其子类包括CClientDC(仅在客户区绘制), CPaintDC, CWindowDC(可以在客户区和窗口非客户区绘制)等。所有继承于CWnd类的子类均可以构造设备类。

#include <afxwin.h>

class MFC_Tutorial_Window: public CFrameWnd
{
	CPoint m_startPoint;
	CPoint m_endPoint;
public:
	MFC_Tutorial_Window()
	{
		Create(NULL, "MFC Tutorial");
	}

	void OnLButtonDown(UINT nFlags, CPoint point);
/*	{
		CFrameWnd::OnLButtonDown(nFlags,point);

		m_startPoint = point;	
	}*/
	
	void OnLButtonUp(UINT nFlags, CPoint point);
/*	{
		CFrameWnd::OnLButtonUp(nFlags, point);
		m_endPoint = point;

		CClientDC dc(this);
		dc.MoveTo(m_startPoint);
		dc.LineTo(m_endPoint);
	}*/

	DECLARE_MESSAGE_MAP()
};

BEGIN_MESSAGE_MAP( MFC_Tutorial_Window, CFrameWnd)
ON_WM_LBUTTONDOWN() //Macro to map the left button click to the handler
ON_WM_LBUTTONUP() //Macro to map the left button click to the handler
END_MESSAGE_MAP()

void MFC_Tutorial_Window::OnLButtonDown(UINT nFlags, CPoint point)
{	
	m_startPoint = point;
	
	CFrameWnd::OnLButtonDown(nFlags,point);
}

void MFC_Tutorial_Window::OnLButtonUp(UINT nFlags, CPoint point)
{	
	m_endPoint = point;

	CPen pen(PS_SOLID, 1, RGB(255,0,0));
	
	CWindowDC dc(this);      // 构造dc时必须传入一个CWnd类或其子类对象
	CPen* pOldPen = dc.SelectObject(&pen);  
	dc.MoveTo(m_startPoint);
	dc.LineTo(m_endPoint);
	dc.SelectObject(pOldPen);

	CFrameWnd::OnLButtonUp(nFlags, point);
}

class MyApp: public CWinApp
{
	MFC_Tutorial_Window *wnd;
public:
	BOOL InitInstance()
	{
		wnd = new MFC_Tutorial_Window();
		m_pMainWnd = wnd;
		m_pMainWnd->ShowWindow(1);
		return 1;
	}
};

MyApp theApp;

 

转载于:https://www.cnblogs.com/YukiJohnson/archive/2013/01/05/2846814.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值