简体   繁体   English

C#-存储由graphicsObject绘制的对象

[英]C# - storing objects drawn by graphicsObject

Im using System.Drawing.Graphics graphicsObj for drawing objects on System.Windows.Forms.Panel . 我使用System.Drawing.Graphics graphicsObjSystem.Windows.Forms.Panel上绘制对象。 What is wise method to store informations about old drawings? 存储有关旧图纸信息的明智方法是什么? To remove last drawing during MouseMove event? 要在MouseMove事件期间删​​除最后的图形?

Do i have to remember Pen settings and cords? 我需要记住笔设置和电源线吗?

Thanks :) 谢谢 :)

// edit. //编辑。 I think my question is not clear. 我认为我的问题不清楚。 I have a Panel. 我有一个小组。 I treat it like a container for drawing. 我把它当作绘图容器。

I can draw multiple lines, circles, rectangles. 我可以画多条线,圆,矩形。

Every time i press the mouse (MouseDown), im want to start drawing new object. 每次按下鼠标(MouseDown)时,我都想开始绘制新对象。 When i move mouse (MouseMove), object currently drawed has to change (for example line, like in paint). 当我移动鼠标(MouseMove)时,当前绘制的对象必须更改(例如画线,例如在绘画中)。 When i release the mouse (MouseUp), i want object i drawed to became constant. 当我释放鼠标(MouseUp)时,我希望绘制的对象保持不变。

Now, i tried to invoke graphicsObj.Clear(); 现在,我尝试调用graphicsObj.Clear(); method on MouseMove event, but it clears all drawed objects. MouseMove事件上的方法,但会清除所有绘制的对象。

So i deduced, that i have to store informations about old drawed objects. 因此我推断,我必须存储有关旧绘制对象的信息。 And now, i have to make lists (vectors or so) and store informations about that objects? 现在,我必须列出(矢量左右)并存储有关该对象的信息? Or there is a method to avoid it? 还是有一种避免的方法?

You should handle all painting in the Panel 's Paint event, that is exactly what it's there for. 您应该在PanelPaint事件中处理所有绘画,这正是它的用途。

The Paint event gives you in its PaintEventArgs the Graphics object you should use. Paint事件在其PaintEventArgs为您提供了您应该使用的Graphics对象。 This will make sure your instructions are painted on every refresh, not just once. 这样可以确保您的说明在每次刷新时都会绘制,而不仅仅是一次。

If you are deriving from Panel , override the OnPaint method instead. 如果您是从Panel派生的,请改写OnPaint方法。

Edit after original question has been expanded: 扩展原始问题后进行编辑:

You should separate the logic of storing the data from the logic of displaying your data. 您应该将存储数据的逻辑与显示数据的逻辑分开。 The Graphics object's purpose is to display existing data, not to store data. Graphics对象的目的是显示现有数据,而不是存储数据。

I think making List s or other collection of the objects you are drawing is the best solution. 我认为制作List或要绘制的对象的其他集合是最好的解决方案。 You will also need flags to handle all the logic of what is a "live" object, etc. 您还将需要标志来处理“活动”对象等的所有逻辑。

我认为您可以将最后的x,y存储到Points对象数组中作为历史记录。

Not sure I get what you are after here, but if you want some sort of memory about what paint drew. 不知道我在这里能得到什么,但是如果您想对绘画的内容有某种记忆的话。 Draw to a collections of bitmaps, then have your paint routine draw one of the bitmaps on to the control. 绘制到位图的集合,然后让绘制例程将位图之一绘制到控件上。

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

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