如何在MFC中用鼠标画直线

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在MFC中用鼠标画直线相关的知识,希望对你有一定的参考价值。

双缓存?
这个我不清楚,原来还有这样的命名,呵呵~
首先,鼠标左键点击按下的消息响应on_wm_rbuttondown(),这时候确立第一个点。
其次,鼠标移动的消息响应on_wm_mousemove(),这是关键。这里因为你要线随鼠标移动,如果你直接把当前点与第一步所得的点连接的话,你会发现整张图都是线。所以这里就需要你把原图复制一张出来并显示这张图(替换原图显示),然后每次响应鼠标移动的时候将当前点与第一步所得的点连接并画在这张复制出来的图上。
最后,鼠标左键点击放开的消息响应on_wm_lbuttonup()。这时候是确立最终点的。你只需画在原图上,并显示该图,并释放你复制出来的那张图就行了。
这是鼠标按下到放开形成一条直线的,你要2次点击也可以,就是在on_wm_rbuttondown()做个前后点击的判断取代on_wm_rbuttonup()。代码自己根据实际应用写吧。
参考技术A 用鼠标划线,用Device
context
,即设备上下文,用来在画布上划线和作图的。里面有很多工具,自己去查资料吧。用moveto(x,y)和Lineto(x,y);函数就可以了。
用鼠标控制也可以实现,也就是在mousemove中加点代码,让线随着鼠标做运动。动脑子好好想想吧,代码就不贴了。好久没用过VC了,呵呵

在 OpenGL 中用鼠标绘制多条线

【中文标题】在 OpenGL 中用鼠标绘制多条线【英文标题】:Drawing Multiple Lines with Mouse in OpenGL 【发布时间】:2015-11-06 17:08:27 【问题描述】:

我试图在我的 OpenGL 和 C++ 程序中用鼠标绘制多个线段。现在我可以画一个,一旦我开始画另一个,前一个就消失了。

以下是我与鼠标绘图相关的代码。关于如何绘制多条线的任何建议?

LineSegment seg;

void mouse(int button, int state, int x, int y) 

    if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)                  // if left button is clicked, that is the start point
        seg.x1 = seg.x2 = x;
        seg.y1 = seg.y2 = y;
    

    if (button == GLUT_LEFT_BUTTON && state == GLUT_UP)                    // once they lift the mouse, thats the finish point
        seg.x2 = x;
        seg.y2 = y;
    



void motion(int x, int y) 
    seg.x2 = x;
    seg.y2 = y;

    glutPostRedisplay();                                                    // refresh the screen showing the motion of the line


void display(void) 
    glClear(GL_COLOR_BUFFER_BIT);                                           // clear the screen

    glBegin(GL_LINES);                                                          // draw lines
    glVertex2f(seg.x1, seg.y1);
    glVertex2f(seg.x2, seg.y2);

    glEnd();

    glutSwapBuffers();

【问题讨论】:

【参考方案1】:
void display(void) 
    glClear(GL_COLOR_BUFFER_BIT);                                           // clear the screen

    glBegin(GL_LINES);                                                          // draw lines
    glVertex2f(seg.x1, seg.y1);
    glVertex2f(seg.x2, seg.y2);

    glEnd();

    glutSwapBuffers();

您需要将先前的线段保存在数据结构中,并在您单击鼠标时将其添加到其中。然后drawloop需要遍历该数据结构并绘制每个保存的线段。

std::vector<LineSegment> segmentvector;
//Each time you release the mouse button, add the current line segment to this vector
/*...*/

glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINES);
for(const LineSegment & seg : segmentvector) 
    glVertex2f(seg.x1, seg.y1);
    glVertex2f(seg.x2, seg.y2);


glVertex2f(currseg.x1, currseg.y1);
glVertex2f(currseg.x2, currseg.y2);
glEnd();

我还强烈建议在学习 OpenGL 时不要使用 Fixed-Function-Pipeline 函数。 There are lots of tutorials online for learning modern OpenGL.

【讨论】:

我同意强烈建议; OpenGL 石器时代的大量 tuts 显然已被弃用。 是的。我已经习惯了 Java 中的 for-each 循环,所以这样的怪癖对我来说还不是很明显。 当您开始学习现代版本时,学习旧的 openGL 会派上用场。 @Xirema 我已经合并了向量,这是有道理的。现在问题是我的运动功能。当我绘制它们时,这些线条不再显示。我需要为此创建一个单独的向量吗?我试过这样做,但它似乎不起作用。【参考方案2】:

你需要设置一些逻辑来保存你已经绘制的线条的状态。目前,您永远不会开始绘制 另一条 线,您只需重置 当前 线的开始位置。

这是您正在寻找的可能的解决方案:

std::vector<LineSegment> segs;
LineSegment currentSeg;

void mouse(int button, int state, int x, int y) 

    if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)                  
        LineSegment newSeg; // Make a new segment
        newSeg.x1 = newSeg.x2 = x;
        newSeg.y1 = newSeg.y2 = y;
        segs.insert(newSeg); // Insert segment into segment vector
        currentSeg = newSeg;
    

    if (button == GLUT_LEFT_BUTTON && state == GLUT_UP)   
        currentSeg.x2 = x;
        currentSeg.y2 = y;
    


void motion(int x, int y) 
    currentSeg.x2 = x;
    currentSeg.y2 = y;

    glutPostRedisplay();                          


void display(void) 
    glClear(GL_COLOR_BUFFER_BIT);  

    glBegin(GL_LINES);                                                          

    // Iterate through segments in your vector and draw each
    for(const auto& seg : segs) 
      glVertex2f(seg.x1, seg.y1);
      glVertex2f(seg.x2, seg.y2);
    

    glEnd();

    glutSwapBuffers();

【讨论】:

以上是关于如何在MFC中用鼠标画直线的主要内容,如果未能解决你的问题,请参考以下文章

OpenCV利用鼠标在如下图所示的MFC视频图像上画直线?

你好 你的这个问题 “ html5 canvas 中用鼠标画(拉出)直线的问题! ”

怎么用C++中的MFC随便画一条直线?老师说修改一下代码就行了,在哪里修改?怎么修改啊?大神,求帮啊。

MFC中如何画带实心箭头的直线

MFC绘制直线

MFC 动态绘制直线,圆弧段(连续)如何实现