绘图基础--橡皮筋画线

绘图基础--橡皮筋画线

橡皮筋画线:用户点击鼠标左键定下一个起点,然后把鼠标拖到目标终点,这时程序就会在起始点间画线。


// rubber.cpp

#include <afxwin.h>

// Define the application class
class CApp : public CWinApp
{
public:
	virtual BOOL InitInstance();
};

CApp App;  

// Define the window class
class CWindow : public CFrameWnd
{ 
	HCURSOR cross;
	HCURSOR arrow;
	CPoint start, old;
	BOOL started;
public:
	CWindow(); 
	afx_msg void OnMouseMove(UINT,CPoint);
	afx_msg void OnLButtonDown(UINT, CPoint);
	afx_msg void OnLButtonUp(UINT, CPoint);
	DECLARE_MESSAGE_MAP()
};

// The window constructor
CWindow::CWindow()
{ 
	// Load two cursors
	cross = AfxGetApp()->LoadStandardCursor(IDC_CROSS);
	arrow = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
	started=FALSE;
	
	// Create a custom window class
	const char* pszWndClass = AfxRegisterWndClass(
		CS_HREDRAW | CS_VREDRAW, NULL,
		(HBRUSH)(COLOR_WINDOW+1), NULL);
	
	// Create the window
	Create(pszWndClass, "Drawing Tests", 
		WS_OVERLAPPEDWINDOW,
		CRect(0,0,250,250)); 
}

// The message map
BEGIN_MESSAGE_MAP( CWindow, CFrameWnd )
	ON_WM_MOUSEMOVE()	
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
END_MESSAGE_MAP()

// Start a new line when the user clicks
// the mouse button down
void CWindow::OnLButtonDown(UINT flag,
	CPoint mousePos)
{
	started = TRUE;
	::SetCursor(cross);
	// save the starting position of the line
	start = old = mousePos;
	CClientDC dc(this);
	dc.SetROP2(R2_NOT);
	dc.MoveTo(start);
	dc.LineTo(old);
}

// Complete the line when the user releases
// the mouse button
void CWindow::OnLButtonUp(UINT flag,
	CPoint mousePos)
{
	if (started)
	{
		started = FALSE;
		::SetCursor(arrow);
		CClientDC dc(this);
		dc.MoveTo(start);
		dc.LineTo(old);
	}
}

// Handle dragging
void CWindow::OnMouseMove(UINT flag,
	CPoint mousePos)
{
	// If the mouse button is down and there
	// is a line in progress, rubber band
	if ((flag == MK_LBUTTON) && started)
	{
		::SetCursor(cross);
		CClientDC dc(this);
		dc.SetROP2(R2_NOT);
		// Undraw the old line
		dc.MoveTo(start);
		dc.LineTo(old);
		// Draw the new line
		dc.MoveTo(start);
		dc.LineTo(mousePos);
		old=mousePos;
	}
	else
		::SetCursor(arrow);
}

// Init the application
BOOL CApp::InitInstance()
{
	m_pMainWnd = new CWindow();
	m_pMainWnd->ShowWindow(m_nCmdShow);
	m_pMainWnd->UpdateWindow();
	return TRUE;
}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值