绘制,绘画,子MDI和文本文件 [英] Draw, Paint, child MDI and a text file

查看:83
本文介绍了绘制,绘画,子MDI和文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从现有的文本文件中读取鼠标坐标,并在每个点(坐标)之间画线到子MDI表单上?请帮助

How read mouse coordinates from an existing text file and draw line between each of the points(coodinates) onto a child MDI Form? Please help

推荐答案

与MDE无关.您可以使用任何控件进行操作,包括System.Windows.Form.Form.

首先,获取鼠标坐标,处理适当的鼠标事件;请参阅:
http://msdn.microsoft.com/en-us/library/system. windows.forms.control.aspx [ ^ ].

另外,可以将该类作为子类,以创建自己的控件类,并覆盖相应的虚方法,例如OnMouseUpOnMouseDownOnMouseMove.这样比较好,因为很可能需要将其子类化以消除闪烁.使用双缓冲功能需要子类.

现在的问题是:您在哪里借鉴?如果您尝试仅在控件上绘制,它将无法正常工作.您将需要绘制在数据模型上".真的.这是初学者难以理解的地方. System.Windows.Forms中的图形渲染不是在控件上放置某些东西时发生的,而是在Windows消息WM_PAINT上放置时发生的.发送控制无效.结果,当您在控件上绘制某些内容时,然后以z顺序在顶部放置一些其他窗口,然后再次显示您的窗口,所有图形都会消失.因此,您应该在事件Paint的句柄中进行绘制,或者最好在覆盖的虚拟方法OnPaint中进行绘制(更好的做法是,再次,因为您很可能仍将控件子类化,如果这是一种形式,您始终是子类).

要处理事件,您需要在控件的字段中保留一个数据模型,并在事件的响应中,每次从模型到System.Drawing.Graphics的实例重新渲染它.您永远不要创建此实例,而应始终从事件处理程序(或OnPaint方法)的事件参数中获取一个.使用实例进行绘图,请参见:
http://msdn.microsoft.com/en-us/library/system.drawing. graphics.aspx [^ ].

每次更改图形数据时(例如,通过鼠标事件),都应调用Graphics.Invalidate.

有关更多详细信息,请参阅我过去的答案:
Paint是一种哪种好玩的方法? (DataGridViewImageCell.Paint(...)) [在mdi子表单之间画线 [在面板上捕获图形 [ http://en.wikipedia.org/wiki/Multiple_document_interface#Disadvantages [在WPF中使用MDI窗口的问题 [ ^ ],
MDIContainer提供错误 [如何在WPF中创建MDI父窗口? [ ^ ].

—SA
It has nothing to do with MDE. You can do it with any control, including System.Windows.Form.Form.

First of all, to get mouse coordinates, handle appropriate mouse events; please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.aspx[^].

Alternatively, subclass this class to make your own control class and override appropriate virtual methods like OnMouseUp, OnMouseDown, OnMouseMove. This is better, because most likely, you will need to subclass it to eliminate flicker. Using double buffering feature needs subclassing.

Now, the problem is: where do you draw on? If you try to draw just on the control, it won''t work. You will need to draw "on data model". Really. This is something where the beginner have trouble understanding. Graphics rendering in System.Windows.Forms happens not when you put something on the control, but on the windows message WM_PAINT. It is send on control invalidation. As a result, when you draw something on control, then put some other window on top in z order and then show your window again, all your graphics disappears. Therefore, you should do drawing in the handle of the event Paint or, better yes, in the overridden virtual method OnPaint (better, again, because you will most likely subclass the control anyway, and if this is a form, you always subclass).

To process the event probably, you need to keep a data model in the fields of your control, and in the response of the event, re-render it every time, from model to the instance of System.Drawing.Graphics. You should never create this instance, but always take one from the event arguments of the event handler (or OnPaint method). Use the instance for drawing, see:
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.aspx[^].

Every time you change the drawing data (for example, via your mouse events), you should call Graphics.Invalidate.

For further detail, please see my past answers:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
Drawing Lines between mdi child forms[^],
capture the drawing on a panel[^].

Now, to eliminate flicker, you will need to use double buffering through Control.SetStyle. This method is protected. That''s why I told above that you will need subclassing anyway (you don''t need this with forms as it has a public property Form.DoubleBuffered, but a form is always subclasses, practically!). You will need to add the following styles:
ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer.

That''s it.

Now, (sigh) — about MDI.

Here is the idea: who needs MDI, ever? Why torturing yourself and scaring off your users?
Do yourself a great favor: do not use MDI at all. You can do much easier to implement design without it, with much better quality. MDI is highly discouraged even by Microsoft, in fact, Microsoft dropped it out of WPF and will hardly support it. More importantly, you will scare off all your users if you use MDI. Just don''t. I can explain what to do instead.

Please see:
http://en.wikipedia.org/wiki/Multiple_document_interface#Disadvantages[^].

In my past answers:
Question on using MDI windows in WPF[^],
MDIContainer giving error[^].

See also:
How to Create MDI Parent Window in WPF?[^].

—SA


这篇关于绘制,绘画,子MDI和文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆