Control的轻量级winform图形元素替代品 [英] Light-weight winform graphic element substitute of Control

查看:176
本文介绍了Control的轻量级winform图形元素替代品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有兴趣创建超轻量级的绘图元素。这样,我可以创建数百万个这些对象,而不需要与 System.Windows.Forms.Control 类关联的over-head。

I am interested in creating a drawing element that is super-light weight. Such that I can create millions of these objects without the over-head that is associated with the System.Windows.Forms.Control class.

有问题的类将继承 System.ComponentModel.Component 类,并且只有一个构造函数和一个 paint(object sender,PaintEventArgs e)方法。但是,我不知道, System.Windows.Forms.Control 类如何执行或简化实际的屏幕绘图。

The class in question would inherit from the System.ComponentModel.Component class, and have (I am guessing) only a constructor and a paint(object sender, PaintEventArgs e) method. However, I don't know, how the System.Windows.Forms.Control class performs or facilitates the actual onscreen drawing.

如何使用有效的绘图上下文复制绘图/绘图功能,类似于 System.Windows.Forms.Control class?

How could I replicate the paint/drawing function with a valid drawing context, similar to the System.Windows.Forms.Control class?

推荐答案

成千上万的控件可能会成为一个记忆猪,但是您的控件的重量可能很轻。在这种情况下我会做的是创建一个控制执行绘图操作(并继承自 System.Windows.Forms.Control )和一个类MyDrawingObject这些数百万个对象的每个实例的必要数据。绘图类将有一个MyDrawingObjects的集合(List,Array或其他东西),并且可以随时绘制它们。

Millions of controls would be a memory-hog, however light-weight your control may be. What I'd do in this situation is to create a single control that performs drawing operations (and inherits from System.Windows.Forms.Control) and a class say MyDrawingObject that keeps necessary data for those each instance of those millions of objects. The drawing class will then have a collection (List, Array or something) of those MyDrawingObjects and would draw them on the fly.

假设绘图对象是半径不同的球体,颜色,位置和重量。我的球类会像这样:

Suppose your drawing objects are balls with different radius, color, position and weight. My Ball class would be something like:

class Ball
{
    public float Radius {get; set;}
    public int Color {get; set;}
    public float Weight {get; set;}
    public Point Position {get; set;}

    ...
}

现在我的控制会是这样的:

Now my Control would be something like:

class MyDrawingBoard : System.Windows.Forms.Control
{
    List<Ball> MyBalls = new List<Ball>();

    override void OnPaint(object sender, PaintEventArgs e)
    {
         foreach(var b in MyBalls)
         {
             e.Graphics.DrawEllipse()...
         }
    }
}

您可以进一步通过仅绘制与 ClipRectangle 相交的对象来提高性能,或者甚至进入管理多个 List s的范围针对不同种类的物体。这一切都取决于您的需求和您的应用程序的目标。

You can further improve performance by drawing only those objects that intersect with the ClipRectangle, or even going to the extent of managing multiple Lists for different kinds of objects. That all depends upon your needs and the objectives of your application.

这篇关于Control的轻量级winform图形元素替代品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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