VC++简单实现工具软件随鼠标移动画线的功能

592 篇文章 7 订阅 ¥99.90 ¥99.00
241 篇文章 13 订阅

一些绘图或仿真软件都有这功能,随鼠标移动,始终有一条线从起点跟随到鼠标当前位置;这功能不只是随鼠标移动画线;实际要实现2个功能:擦除前一条线,再画一条从起点到当前鼠标位置的线;

环境Win10,VC6;新建一个单文档工程;

为视类添加鼠标移动消息处理函数;在其中画线;

为简单起见,设起点为(0,0),始终画一条线指向鼠标当前位置;

如果不进行擦除,效果是这样的;

视类头文件添加成员变量;pt1是起点,ptold为前一条线的终点;

CPoint ptold, pt1;

视类CPP文件构造函数中初始化;

    ptold.x=0;
    ptold.y=0;
    pt1.x=0;
    pt1.y=0;

视类鼠标移动消息处理函数代码:

void CLine1View::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CDC* pDC=GetDC();

	//屏幕色取反,擦
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是在VC++6.0 MFC中使用鼠标画椭圆的实现过程: 1. 创建一个新的MFC应用程序项目。 2. 在资源视图中添加一个菜单资源,例如IDR_MAINFRAME。 3. 打开应用程序类的头文件,添加以下代码: ```cpp class CMyApp : public CWinApp { public: virtual BOOL InitInstance(); }; class CMyWnd : public CFrameWnd { public: CMyWnd(); protected: afx_msg void OnPaint(); afx_msg void OnLButtonDown(UINT nFlags, CPoint point); afx_msg void OnLButtonUp(UINT nFlags, CPoint point); afx_msg void OnMouseMove(UINT nFlags, CPoint point); DECLARE_MESSAGE_MAP() private: BOOL m_bDrawing; CRect m_rectEllipse; }; BOOL CMyApp::InitInstance() { m_pMainWnd = new CMyWnd; m_pMainWnd->ShowWindow(SW_SHOW); m_pMainWnd->UpdateWindow(); return TRUE; } CMyWnd::CMyWnd() { Create(NULL, _T("My Oval Drawing Program")); m_bDrawing = FALSE; } void CMyWnd::OnPaint() { CPaintDC dc(this); if (m_bDrawing) { dc.SelectStockObject(NULL_BRUSH); dc.Ellipse(m_rectEllipse); } } void CMyWnd::OnLButtonDown(UINT nFlags, CPoint point) { m_bDrawing = TRUE; m_rectEllipse.left = point.x; m_rectEllipse.top = point.y; m_rectEllipse.right = point.x; m_rectEllipse.bottom = point.y; SetCapture(); } void CMyWnd::OnLButtonUp(UINT nFlags, CPoint point) { m_bDrawing = FALSE; ReleaseCapture(); Invalidate(); } void CMyWnd::OnMouseMove(UINT nFlags, CPoint point) { if (m_bDrawing) { CDC* pDC = GetDC(); pDC->SelectStockObject(NULL_BRUSH); pDC->SetROP2(R2_NOTXORPEN); pDC->Ellipse(m_rectEllipse); m_rectEllipse.right = point.x; m_rectEllipse.bottom = point.y; pDC->Ellipse(m_rectEllipse); ReleaseDC(pDC); } } BEGIN_MESSAGE_MAP(CMyWnd, CFrameWnd) ON_WM_PAINT() ON_WM_LBUTTONDOWN() ON_WM_LBUTTONUP() ON_WM_MOUSEMOVE() END_MESSAGE_MAP() CMyApp theApp; ``` 4. 编译并运行应用程序。当您单击菜单中的“画椭圆”按钮时,应用程序将允许您使用鼠标在窗口中绘制椭圆形。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值