跟随鼠标移动的线

zeallen1 2009-10-28 02:59:16
本人利用了网上的一个类进行波形的显示,类名为COScopeCtrl,派生于CStatic,现在需要当鼠标移动到波形显示框区域(CStatic)的时候有个线跟随者鼠标移动,而且当鼠标按下时画出一条线,现在的问题时,我在COScopeCtrl类里面建立了映射OnMouseMove,而且在里面利用一变量保存了鼠标的点,m_PointMouse为成员变量

// OScopeCtrl.h : header file
//

#ifndef __OScopeCtrl_H__
#define __OScopeCtrl_H__

/////////////////////////////////////////////////////////////////////////////
// COScopeCtrl window

class COScopeCtrl : public CStatic
{
// Construction
public:
COScopeCtrl();

// Attributes
CDC m_dcGrid;
CDC m_dcPlot;
CDC m_dcCrossline;

CBitmap *m_pbitmapOldGrid;
CBitmap *m_pbitmapOldPlot;
CBitmap *m_pbitmapOldCrossline;

CBitmap m_bitmapGrid;
CBitmap m_bitmapPlot;

// CBitmap *m_pbitmapOldCrossline;
CBitmap m_bitmapCrossline;

public:
void DrawCrossline();
......


public:
CPoint m_PointMouse;
COLORREF m_crBackColor; // background color
COLORREF m_crGridColor; // grid color
COLORREF m_crPlotColor; // data color
COLORREF m_crCrosslineColor; //crossline color
......

// OScopeCtrl.cpp : implementation file//

void COScopeCtrl::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_PointMouse=point;
CStatic::OnMouseMove(nFlags, point);
}
自己编写的画线的函数
void COScopeCtrl::DrawCrossline()
{
int x,CPen *oldPen ;
CPoint w_s,w_e;
x=m_PointMouse.x;
//x=(m_rectClient.left+m_rectClient.right)/2;
w_s.x=x;
w_s.y=m_rectClient.bottom;
w_e.x=x;
w_e.y=m_rectClient.top;

if (m_dcCrossline.GetSafeHdc() != NULL)
{
// fill the cleanup area with the background
m_dcCrossline.FillRect(rectCleanUp, &m_brushBack) ;
oldPen = m_dcCrossline.SelectObject(&m_penCrossline) ;
m_dcCrossline.MoveTo (w_s) ;
m_dcCrossline.LineTo (w_e) ;
// restore the pen
m_dcCrossline.SelectObject(oldPen) ;
}
在void COScopeCtrl::OnPaint()
{
CPaintDC dc(this) ; // device context for painting
CDC memDC ;
CBitmap memBitmap ;
CBitmap* oldBitmap ; // bitmap originally found in CMemDC

// no real plotting work is performed here,
// just putting the existing bitmaps on the client

// to avoid flicker, establish a memory dc, draw to it
// and then BitBlt it to the client
memDC.CreateCompatibleDC(&dc) ;
memBitmap.CreateCompatibleBitmap(&dc, m_nClientWidth, m_nClientHeight) ;
oldBitmap = (CBitmap *)memDC.SelectObject(&memBitmap) ;


//DrawCrossline();
if (memDC.GetSafeHdc() != NULL)
{
// first drop the grid on the memory dc
memDC.BitBlt(0, 0, m_nClientWidth, m_nClientHeight,
&m_dcGrid, 0, 0, SRCCOPY) ;
// now add the plot on top as a "pattern" via SRCPAINT.
// works well with dark background and a light plot
memDC.BitBlt(0, 0, m_nClientWidth, m_nClientHeight,
&m_dcPlot, 0, 0, SRCPAINT) ; //SRCPAINT
// memDC.BitBlt(0, 0, m_nClientWidth, m_nClientHeight,
// &m_dcCrossline, 0, 0, SRCPAINT) ;
memDC.MoveTo(m_PointMouse.x,m_rectPlot.top);
memDC.LineTo(m_PointMouse.x,m_rectPlot.bottom);

memDC.BitBlt(0, 0, m_nClientWidth, m_nClientHeight,
&m_dcCrossline, 0, 0, SRCPAINT) ; //SRCPAINT

// finally send the result to the display
dc.BitBlt(0, 0, m_nClientWidth, m_nClientHeight,
&memDC, 0, 0, SRCCOPY) ;

}

memDC.SelectObject(oldBitmap) ;

} //
memDC.MoveTo(m_PointMouse.x,m_rectPlot.top);
memDC.LineTo(m_PointMouse.x,m_rectPlot.bottom);是用来尝试的划线的,主要用来检测鼠标的点值是否传递回来,当采用固定的点x为上下边框的一半时,可以画出来一条直线,现在是感觉在mousemove函数里面的值并没有传递m_PointMouse,用单步调试时在onmuosemove里面加入MessageBox("是否真的结束?","message",MB_YESNO);也有对话框弹出,将断点直接设置 m_PointMouse=point;时也有值,而画出的线还是m_PointMouse=0;时的值(m_PointMouse在构造函数被初始化为0),感觉onmuosemove的执行在划线之后,
请各位大侠帮忙看看什么问题,怎么解决,小弟非常感谢啊!
...全文
295 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
zeallen1 2009-10-30
  • 打赏
  • 举报
回复
谢谢huangxiaohu_coder,我已经该出了我想要的结果!!!!
zeallen1 2009-10-30
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 huangxiaohu_coder 的回复:]
改了下,不知道是不是你要的效果

void CLineTraceView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
//用于控制绘图是否完成
m_isDraw=(m_isDraw+1)%4;
CView::OnLButtonUp(nFlags, point);
}

void CLineTraceView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_isDraw=(m_isDraw+1)%4;

m_startPoint.x=point.x;
m_startPoint.y=0;

m_oldStartPoint.x=point.x;
m_oldStartPoint.y=0;

CDC* pDC=GetDC();
//绘制第一条线,在MOUSEMOVE时候将其擦除
if(m_isDraw)
{
pDC->MoveTo(m_oldStartPoint);
pDC->LineTo(m_oldPoint);
}
CView::OnLButtonDown(nFlags, point);
}

void CLineTraceView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
int oldRop2;

m_endPoint=point;

m_startPoint.x=point.x;
m_startPoint.y=0;
CDC* pDC=GetDC();
if (m_isDraw)
{
//设置绘图模式,设置为与背景色反色,用于擦除
oldRop2=pDC->SetROP2(R2_NOT);
//对前一个点擦除
pDC->MoveTo(m_oldStartPoint);
pDC->LineTo(m_oldPoint);
//画出新的点
pDC->MoveTo(m_startPoint);
pDC->LineTo(m_endPoint);
//返回原来的绘图模式
pDC->SetROP2(oldRop2);
}
m_oldPoint=point;

m_oldStartPoint.x=point.x;
m_oldStartPoint.y=0;

CView::OnMouseMove(nFlags, point);
}
[/Quote]
虽然不是我要的效果,但是还是很感谢您,我要的是线是从屏幕上方到下方的一条竖线,只是鼠标移动时改变下线的位置。使得这条从上到下的竖线左右移动。只能左右移动的。
zeallen1 2009-10-29
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 tadican 的回复:]
C/C++ code
w_s.x=x;
w_s.y=m_rectClient.bottom;
w_e.x=x;
w_e.y=m_rectClient.top;

C/C++ code
m_dcCrossline.MoveTo (w_s) ;
m_dcCrossline.LineTo (w_e) ;//不的w_s, w_e好像是固定的啊
[/Quote]
w_s 不是固定的,w_e是固定的,x为鼠标传过来的鼠标的x坐标。
huangxiaohu_coder 2009-10-29
  • 打赏
  • 举报
回复
改了下,不知道是不是你要的效果

void CLineTraceView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
//用于控制绘图是否完成
m_isDraw=(m_isDraw+1)%4;
CView::OnLButtonUp(nFlags, point);
}

void CLineTraceView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_isDraw=(m_isDraw+1)%4;

m_startPoint.x=point.x;
m_startPoint.y=0;

m_oldStartPoint.x=point.x;
m_oldStartPoint.y=0;

CDC* pDC=GetDC();
//绘制第一条线,在MOUSEMOVE时候将其擦除
if(m_isDraw)
{
pDC->MoveTo(m_oldStartPoint);
pDC->LineTo(m_oldPoint);
}
CView::OnLButtonDown(nFlags, point);
}

void CLineTraceView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
int oldRop2;

m_endPoint=point;

m_startPoint.x=point.x;
m_startPoint.y=0;
CDC* pDC=GetDC();
if (m_isDraw)
{
//设置绘图模式,设置为与背景色反色,用于擦除
oldRop2=pDC->SetROP2(R2_NOT);
//对前一个点擦除
pDC->MoveTo(m_oldStartPoint);
pDC->LineTo(m_oldPoint);
//画出新的点
pDC->MoveTo(m_startPoint);
pDC->LineTo(m_endPoint);
//返回原来的绘图模式
pDC->SetROP2(oldRop2);
}
m_oldPoint=point;

m_oldStartPoint.x=point.x;
m_oldStartPoint.y=0;

CView::OnMouseMove(nFlags, point);
}
zeallen1 2009-10-29
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 huangxiaohu_coder 的回复:]
到底要什么功能哦,没理解到呢?是只要画随鼠标移动的轨迹线,还是要其他功能哦?
[/Quote]
你给的代码是画点,我想要的是鼠标移动的时候有一条直线,就是鼠标不断移动是,每一时刻都有一条以鼠标x为x轴从区域最上面到最下面的的线,就是要连续画线。就像连续的十字光标一样,我这只需要一条竖线即可。就是连续的竖线。
TADICAN 2009-10-28
  • 打赏
  • 举报
回复

w_s.x=x;
w_s.y=m_rectClient.bottom;
w_e.x=x;
w_e.y=m_rectClient.top;




m_dcCrossline.MoveTo (w_s) ;
m_dcCrossline.LineTo (w_e) ;//不的w_s, w_e好像是固定的啊
Hiiishe 2009-10-28
  • 打赏
  • 举报
回复
通知刷新。绘制函数里面TRACE一下看看数据。大致看了下代码在改点后没有调用刷新
lsvine 2009-10-28
  • 打赏
  • 举报
回复
void COScopeCtrl::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_PointMouse=point;

Invalidate();//通知dc重绘 大概是这个问题 你自己试一下

CStatic::OnMouseMove(nFlags, point);
}

huangxiaohu_coder 2009-10-28
  • 打赏
  • 举报
回复
单画轨迹线的画这样就可以了
void CLineTraceView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
//用于控制绘图是否完成
m_isDraw=(m_isDraw+1)%4;
CView::OnLButtonUp(nFlags, point);
}

void CLineTraceView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_isDraw=(m_isDraw+1)%4;
m_startPoint=point;
CView::OnLButtonDown(nFlags, point);
}

void CLineTraceView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
int oldRop2;

m_endPoint=point;
CDC* pDC=GetDC();
if (m_isDraw)
{
//设置绘图模式,设置为与背景色反色,用于擦除
oldRop2=pDC->SetROP2(R2_NOT);
//对前一个点擦出
pDC->MoveTo(m_startPoint);
pDC->LineTo(m_oldPoint);
//画出新的点
pDC->MoveTo(m_startPoint);
pDC->LineTo(m_endPoint);
//返回原来的绘图模式
pDC->SetROP2(oldRop2);
}
m_oldPoint=point;
m_startPoint=point;
CView::OnMouseMove(nFlags, point);
}
huangxiaohu_coder 2009-10-28
  • 打赏
  • 举报
回复
到底要什么功能哦,没理解到呢?是只要画随鼠标移动的轨迹线,还是要其他功能哦?
delphiwcdj 2009-10-28
  • 打赏
  • 举报
回复
up

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧