简体   繁体   English

如何用鼠标画图?

[英]How to draw with mouse?

I want to make a simple mspaint. 我想做一个简单的mspaint。 Firstly, I draw lines in the mouse event. 首先,我在鼠标事件中画线。

private void pnl_Draw_MouseMove(object sender, MouseEventArgs e)
{
    if(startPaint)
    {
        g = pnl_Draw.CreateGraphics();
        g.DrawLine(p, new Point(initX ?? e.X, initY ?? e.Y), new Point(e.X, e.Y));
        initX = e.X;
        initY = e.Y;
    }
}

And then, I realized that all would be resolved if the form was minimized. 然后,我意识到,如果将表单最小化,则所有问题都将得到解决。 However, How do I use the onpaint event to do it? 但是,如何使用onpaint事件来做到这一点? Should I store all the points in a list when the user click and move, and then paint and clear them in the onpaint event? 用户单击并移动,然后在onpaint事件中绘制并清除它们时,是否应该将所有点存储在列表中?

You need to keep a copy of the last image as a bitmap object. 您需要保留最后一个图像的副本作为位图对象。 Everything user does should update this object in the memory. 用户所做的一切都应在内存中更新该对象。 You can also handle OnPaint event of your canvas to display this bitmap object as a background image. 您还可以处理画布的OnPaint事件,以将该位图对象显示为背景图像。

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

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