在C#中用鼠标画线的正确方法是什么
这是我的绘图代码,用于用鼠标在图表上绘制自定义线条。你能帮我以正确的方式做吗?
namespace Grafi
{
public partial class Form1 : Form
{
bool isDrawing = false;
Point prevPoint;
public Form1()
{
InitializeComponent();
}
private void chartTemperature_MouseDown(object sender, MouseEventArgs e)
{
isDrawing = true;
prevPoint = e.Location;
}
private void chartTemperature_MouseMove(object sender, MouseEventArgs e)
{
Pen p = new Pen(Color.Red, 2);
if (isDrawing)
{
Graphics g = chartTemperature.CreateGraphics();
g.DrawLine(p, prevPoint, e.Location);
prevPoint = e.Location;
numOfMouseEvents = 0;
}
p.Dispose();
}
private void chartTemperature_MouseUp(object sender, MouseEventArgs e)
{
isDrawing = false;
}
}
}
问题是,当我调整表格大小时,我的线条消失了。每当引发 onPaint 事件时它就会消失。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
试试这个...这是一种笔画绘制方法,实现非常简单并且尽可能接近您自己的代码。斯托克斯是鼠标运动的单独集合。鼠标在向下和向上之间的每次移动都会记录为一次笔划,所有笔划都会被收集,然后在触发绘画事件时重新绘制。这个例子很简单,但可能是一个很好的起点。
请注意,您必须为图表对象添加绘制处理程序。
Try this... It is a stroke drawing method, implemented very simply and as close to your own code as possible. Stokes are individual collections of mouse movements. Every mouse move between down and up is recorded as a stroke, all the strokes are collected and then redrawn whenever the paint event is fired. This example is simple but could be a good starting point.
Note that you will have to add the paint handler for your chart object.
您当前的实施有什么问题吗?它是否有效,或者您只是想让代码更好地适应已经工作的功能。
我觉得你的逻辑看起来很好。但是,我会像这样向 Pen 添加一个 using 子句:
这样,即使在创建 Pen 和调用
Dispose
后发生任何异常,您的 Pen 也会被释放。但是,您也可以考虑将
Pen
设为类变量,这样您就不必在每次移动鼠标时创建和处置它。Do you have any problems with your current implementation? Does it work, or do you just want to make the code better for an already working function.
I think you logic looks just fine. However, I would add a using clause to the Pen like this:
This way your Pen will be disposed even in case of any exceptions occuring after it's creation and your call to
Dispose
.However, you can also think of making the
Pen
a class variable so you don't have to create and dispose it each time you move the mouse.您需要将线路存储在某个地方。
您需要采取的步骤是:
ArrayList>
- 其中每个ArrayList
包含一行中的点列表。new ArrayList
)paint
中,迭代所有线,并绘制数组中每条线的每个点。如果您不将线路存储在某个地方,它们就会丢失。这有道理吗?
存储线条的另一种方法是使用 Canvas 对象,其中所绘制内容的像素图会被记住并自动绘制。如果您不介意不将线数据作为矢量点,并且您可能还想使用图像或颜色,那么这可能是更好的方法。
You need to store your line somewhere.
The steps you need to take are:
ArrayList<ArrayList<Point>>
- where eachArrayList<Point>
contains the list of points in one line.new ArrayList<Point>
) at the end of the list of linespaint
, iterate through all lines, and draw each point of each line in the array.If you don't store your lines somewhere, they will be lost. Does this make sense?
The other way of storing lines is by using a
Canvas
object, where the pixel-map of what is drawn is remembered and automatically drawn. If you don't mind not having your line data as vector points, and you might also want to use images or colours, then this might be a better approach.我不久前发布了一个解决方案,介绍了如何使用鼠标移动画一条线。这应该对你有用。
基本上你能做的就是每次鼠标移动时画一条线。如果有上一条线并且您仍在移动鼠标,请擦除该线并绘制新线。请注意,此示例基于特定
Panel
(本示例中为 myPanel1)进行偏移。相应地进行调整。如果调整控件的大小,则需要使用锚点上一个点重新绘制线条。I posted a solution a while back on how to draw a line using mouse movements. This should work for you.
Basically what you can do is draw a line every time the mouse moves. If there was a previous line and youre still moving the mouse, erase the line and draw the new one. Note that this example offsets based on a specific
Panel
(myPanel1 in this example). Adjust accordingly. If you resize the control, you will need to redraw the line using the anchor point previous point.