简体   繁体   English

MFC画一条线

[英]MFC drawing a line

I need help with making a certain MFC program. 我在制作某些MFC程序时需要帮助。 I need to make a program that will draw a line in the following way: the user chooses the starting point by left clicking, and the final point by left clicking the second time, after which the points are connected and the line is drawn. 我需要制作一个程序,该程序将通过以下方式绘制一条线:用户第二次单击鼠标左键选择起点,第二次单击鼠标左键选择终点,然后将这些点连接起来并绘制直线。 I've managed to get the coordinates of the first one with this: 我已经设法得到第一个的坐标:

void CsemView::OnLButtonDown(UINT nFlags, CPoint point)
{
    CsemDoc* pDoc= GetDocument();
    // TODO: Add your message handler code here and/or call default 
    pDoc->a_pos=point;
    Invalidate();
    CView::OnLButtonDown(nFlags, point);
}

The problem is, i don't know how to get the coordinates of the second one with the second left click. 问题是,我不知道如何用第二个左键单击来获得第二个的坐标。 I've managed to do it by using the on double left click function( and putting pDoc->b_pos=point; in it), but that's not really what I was supposed to do. 我已经设法通过使用on double左键单击功能(并将pDoc-> b_pos = point;放入其中)来做到这一点,但这并不是我应该做的。 (I was putting in the coordinates of the first one into MoveTo and the second one into LineTo). (我将第一个坐标放入MoveTo,第二个坐标放入LineTo)。 I would appreciate if someone could help me (I'm suspecting there's perhaps a different, simpler way of doing this). 如果有人可以帮助我,我将不胜感激(我怀疑这样做可能有其他更简单的方法)。 Thanks in advance. 提前致谢。

If you want to get two results from a same event, you have to keep a state variable to track wht click is it. 如果您想从同一事件中获得两个结果,则必须保留一个状态变量以跟踪点击是否成功。

On other words, your CsemDoc should have an a_pos and b_pos members, and CsemView a bool is_b , initialized as false. 换句话说,您的CsemDoc应该具有a_posb_pos成员,而CsemViewbool is_b初始化为false。

The OnLButtonDow method should: be something like OnLButtonDow方法应:类似于

if(!is_b)
{ set the a_pos; is_b = true; }
else
{ set the b_pos; is_b = false; invalidate the draw; }

You can push the mouse co-ordinates on each LButtonDown to a vector and draw the lines between P[i] and P[i+1] and upon a RButtonDown you can stop recording of the points after that and no more extra lines will be drawn. 您可以将每个LButtonDown上的鼠标坐标推到一个向量上,并绘制P [i]和P [i + 1]之间的线;在RButtonDown上,您可以停止记录点,之后将不再有多余的线画。 Have a separate button like any drawing toolbox to start the line drawing so that any LButtonDown events after that will be pushed to the vector. 像任何绘图工具箱一样,有一个单独的按钮来开始绘制线条,以便将其后的所有LButtonDown事件推送到向量中。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM