使用图形工具动态创建树 [英] Create trees dynamically by using graphic tools

查看:69
本文介绍了使用图形工具动态创建树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用图形工具在表单中显示树?

How can i display a tree in form by using graphic tools?

推荐答案

步骤包括:

  • 创建一些控件来保存图形,从System.Windows.Forms.ControlSystem.Windows.Forms.Control派生您的类.您甚至可以使用Form,但是就我的口味而言,它还不够灵活.
  • 在图形控件中,使用SetStyle并添加样式System.Windows.Forms.AllPaintingInWmPaint | System.Windows.Forms.OptimizedDoubleBuffer.这将帮助您避免闪烁.这是使用控件类的子类的原因之一,因为System.Windows.Forms.Control.SetStyle受保护.
    请参见 http://msdn.microsoft.com/en-us/library/ms733127.aspx [ ^ ],
    如何在表单中使用XML File Streamwriter和Reader应用程序? [正在创建属性文件... [ ^ ],
    反序列化json字符串数组 [ ^ ].

    使用数据协定序列化程序(请参见上面的参考),您可以将持久数据用作XML或JSON.
  • 使用System.Drawing.Graphics类的实例创建一种呈现数据模型实例的方法,该方法使用此类的方法,其实例应为该方法的参数. 参数化Graphics的实例非常重要,因为它不仅可以是图形控件之一,而且还可以是其他任何东西,例如打印机,位图等之一.
  • 覆盖图形控件的方法OnPaint.它应该简单地调用上一步开发的方法.
  • 开发修改图形数据模型的方法:添加和删除顶点和圆弧,重新路由圆弧等.添加更改图形细节的方法,查看选项,所有这些都取决于上述数据模型的三个部分或各个方面.请记住:永远不要修改图形本身(请参阅下一项),只需修改模型.
  • 开发使图形无效的方法,该方法应触发所有场景或部分场景的重新绘制它.在所有情况下,这最终都调用方法System.Windows.Forms.Control.Invalidate之一.没有参数的方法会使整个控件无效,带有参数(RectangleRegion)的方法允许无效,因此仅重绘部分图形,以提高性能(请确保您确实需要它,因为它取决于
  • 开发响应键盘/鼠标事件而更改模型的方法.它应包括选择,拖动,上下文菜单及其操作(删除,移动,重命名所选元素等).每次进行此类修改后,请调用上一项中所述的适当的无效方法.
  • 利润!
The steps include:

  • Create some control to hold the graphics, derive your class from System.Windows.Forms.Control or System.Windows.Forms.Control. You can even use your Form, but to my taste, it won''t be flexible enough.
  • In your graphic control, use SetStyle and add the styles System.Windows.Forms.AllPaintingInWmPaint | System.Windows.Forms.OptimizedDoubleBuffer. It will help you to avoid flicker. This is one reason of using subclassing of the control class, as System.Windows.Forms.Control.SetStyle is protected.
    See http://msdn.microsoft.com/en-us/library/system.windows.forms.control.aspx[^].
  • Create a data model of the graph. In the design of data structure, try to isolate 1) pure logical graph data: a graph is a relationship on the set of graph vertices, include just noted and relationship information, 2) graphical data — position of notes, form of arches, etc., 3) view options, such as zoom, shift, color, etc.
  • Make model persist using Data Contract, see:
    http://msdn.microsoft.com/en-us/library/ms733127.aspx[^],
    How can I utilize XML File streamwriter and reader in my form application?[^],
    Creating property files...[^],
    deseralize a json string array[^].

    With the data contract serializers (see the references above) you can use persist data as XML or JSON.
  • Create a method of rendering of the instance of the data model using an instance of the class System.Drawing.Graphics using methods of this class, and instance of which should be a parameter of this method. It is very important to parametrize the instance of the Graphics, because it could be not only the one of your graphics control, but anything else, for example, the one of the printer, bitmap, etc.
  • Override the method OnPaint of your graphics control. It should simply call the method developed on the previous step.
  • Develop methods of modification of the data model of graph: adding and removing vertices and arches, re-routing arches, etc. Add methods of changing graphics detail, view options, all according three parts or aspects of the data model described above. Remember: you should never modify graphics itself (see next item), you only need to modify the model.
  • Develop the method of invalidation of the graphics which should trigger re-painting of all the scene or part of it. In all cases, this ultimately calls one of the methods System.Windows.Forms.Control.Invalidate. A method without parameters invalidates whole control, methods with parameters (Rectangle or Region) allows to invalidate and hence redraw only a part of graphics, in order to improve performance (make sure you really need it, as it depends on the volume of you graphics; do some performance tests).
  • Develop methods of changing the model in response to keyboard/mouse events. It should include selection, dragging, context menu with operations (remove, move, rename of selected element, etc.). After each such modification, call appropriate invalidation method described in the previous item.
  • PROFIT!


这篇关于使用图形工具动态创建树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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