什么是最“轻量级”的winforms控件?

时间:2022-03-25 15:50:46

(This is probably a dumb question AND a WTF combined, but here goes nothing)

(这可能是一个愚蠢的问题和WTF的结合,但这里什么都没有)

I want to 'draw' lines or areas in my interface - typically separators inside a TableLayoutPanel. Currently what I do is just dump a Panel in the row and set it to Dock=Fill and give it the background required colour.

我想在我的界面中“绘制”线条或区域 - 通常是TableLayoutPanel内的分隔符。目前我所做的只是将一个Panel转储到行中并将其设置为Dock = Fill并为其提供所需的背景颜色。

Just before doing the same thing, I wondered if another control (Label?) might be more lightweight. Or maybe I should just roll my own by inheriting from Control?

在做同样的事情之前,我想知道另一个控件(Label?)是否可能更轻量级。或者也许我应该通过继承自己来推动自己?

Any other solutions?

还有其他方法吗?

4 个解决方案

#1


I would inherit from Control and create your own as all other controls including Label and Panel directly or indirectly inherit from Control. This also allows you to encapsulate the functionality separate from the others.

我将从Control继承并创建您自己的所有其他控件,包括Label和Panel直接或间接从Control继承。这也允许您将功能与其他功能分开封装。

I also think you could also override the Paint event for your TableLayoutPanel, or inherit from this, make it your own and draw the lines.

我还认为你也可以覆盖TableLayoutPanel的Paint事件,或者继承它,使它成为你自己的并绘制线条。

#2


I would have a look at the ControlPaint class which has several methods to draw lines etc. that are specifically targetted to draw system-alike lines. Override the paint event or inherit it as Daniel says and draw the lines in there.

我将看一下ControlPaint类,它有几种绘制线条等方法,专门用于绘制系统相似的线条。覆盖paint事件或者像Daniel所说的那样继承它并在那里绘制线条。

#3


Here's an out-of-the-box solution from Microsoft.

这是微软的开箱即用解决方案。

Check out Visual Basic PowerPack 3.0. Don't worry too much about the name - these are components from the Visual Basic Team, but they work just fine for C# developers too.

查看Visual Basic PowerPack 3.0。不要过分担心名称 - 这些是Visual Basic团队的组件,但它们也适用于C#开发人员。

One of the components included is LineShape, which does exactly what you'd expect from the name.

其中一个组件是LineShape,它完全符合您对该名称的期望。

If you're targeting earlier versions of the framework, you could include the assembly with your application. But, if you're targeting 3.5 SP1 or higher, the powerpack is included.

如果您的目标是早期版本的框架,则可以在应用程序中包含程序集。但是,如果您的目标是3.5 SP1或更高,则包含powerpack。

#4


Why do you want to write your own? This seems like premature optimization to me. Also you need to define what you mean by lightweight, memory usage, processor cycles, etc. If you’re really that concerned about performance I’d inherit from control but I wouldn’t go down this route unless testing shows your current solution doesn’t work for some reason.

你为什么要自己写?这对我来说似乎是不成熟的优化。你还需要通过轻量级,内存使用,处理器周期等来定义你的意思。如果你真的那么关心性能我会从控制中继承但我不会沿着这条路走下去,除非测试显示你当前的解决方案没有因某种原因无法工作。

#1


I would inherit from Control and create your own as all other controls including Label and Panel directly or indirectly inherit from Control. This also allows you to encapsulate the functionality separate from the others.

我将从Control继承并创建您自己的所有其他控件,包括Label和Panel直接或间接从Control继承。这也允许您将功能与其他功能分开封装。

I also think you could also override the Paint event for your TableLayoutPanel, or inherit from this, make it your own and draw the lines.

我还认为你也可以覆盖TableLayoutPanel的Paint事件,或者继承它,使它成为你自己的并绘制线条。

#2


I would have a look at the ControlPaint class which has several methods to draw lines etc. that are specifically targetted to draw system-alike lines. Override the paint event or inherit it as Daniel says and draw the lines in there.

我将看一下ControlPaint类,它有几种绘制线条等方法,专门用于绘制系统相似的线条。覆盖paint事件或者像Daniel所说的那样继承它并在那里绘制线条。

#3


Here's an out-of-the-box solution from Microsoft.

这是微软的开箱即用解决方案。

Check out Visual Basic PowerPack 3.0. Don't worry too much about the name - these are components from the Visual Basic Team, but they work just fine for C# developers too.

查看Visual Basic PowerPack 3.0。不要过分担心名称 - 这些是Visual Basic团队的组件,但它们也适用于C#开发人员。

One of the components included is LineShape, which does exactly what you'd expect from the name.

其中一个组件是LineShape,它完全符合您对该名称的期望。

If you're targeting earlier versions of the framework, you could include the assembly with your application. But, if you're targeting 3.5 SP1 or higher, the powerpack is included.

如果您的目标是早期版本的框架,则可以在应用程序中包含程序集。但是,如果您的目标是3.5 SP1或更高,则包含powerpack。

#4


Why do you want to write your own? This seems like premature optimization to me. Also you need to define what you mean by lightweight, memory usage, processor cycles, etc. If you’re really that concerned about performance I’d inherit from control but I wouldn’t go down this route unless testing shows your current solution doesn’t work for some reason.

你为什么要自己写?这对我来说似乎是不成熟的优化。你还需要通过轻量级,内存使用,处理器周期等来定义你的意思。如果你真的那么关心性能我会从控制中继承但我不会沿着这条路走下去,除非测试显示你当前的解决方案没有因某种原因无法工作。