书写/绘图自己的ListBox / ListView控件从头控制自己的、控件、ListBox、ListView

2023-09-04 05:44:55 作者:暮色上浓妆

我怎么会去找出如何在Windows窗体控制写了?我想创建一个控制从头开始。 preferably一个列表框,甚至更好,一个的ListView 控制,但我不知道从哪里开始。

我已经在过去遇到的一些建议已经:

使用一个面板 控制和动态添加标签控制将其与相应的造型,和; 在扩展或子类中的的ListView / 列表框 控制 S,并设置的OwnerDraw ,并做您的自定义图形中的的OnPaint 事件。

不过,我想不止于此控制。我不只是要一个的ListView 控制,我不希望使用第三方控件是(再好[对象的ListView]是 1 。我想我的的的ListView 控制。我不在乎它有多难,但是这有可能在Windows窗体?我应该从哪里开始呢?

请问我需要使用 GDI / GDI + 绘制一切吗?我将开始与一个空面板 控制,然后用系统手动绘制每个列表项.Drawing 命名空间?

解决方案   

我怎么会去找出一个Windows的窗体控件写了?

简单,每一个控制是一个的使用的 CreateWindowEx函数法(通过的WinForms内部完成的)。

在的WinForms的观点:控制对于所有的Windows的基类。有已被写入非托管code像的ListView 列表框等,对于他们,你可以某些控件看不见的油漆code。在.NET。它是在操作系统本身(不知道哪一个DLL他们生活)来实现。的WinForms只是提供了一个包装过的非托管控制。

但是,也有纯粹的管理C#编写的控制。例如: DataGridView的。你可以通过在code。这里的主人是的OnPaint 受保护的方法。这就是你需要编写所有的风俗画的逻辑与提供的图形实例的地方。

关键是你要创建一个数据结构网络化,它拥有所有必要的项目吸引你的控制。比方说ItemRectangle,文字,颜色,字体,等等。然后使用它们放在一起作画的的OnPaint 方法自定义控件。

  

,我需要使用GDI / GDI +绘制的一切吗?

您将使用 System.Drawing中 System.Drawing.Drawing2D 命名空间来绘制你的控制。如果一些.NET不提供你的P / Invoke GDI / GDI +

建议可供选择的基类:如果你的控制需求是滚动(的ListView 样的控制可能需要它)。所以,你可以选择 ScrollableControl 或的面板作为其支持滚动基类。否则,你可以从继承控制类。

vba里面对多列的listbox赋值 用list1 1,1 ,的方式赋值提示错误

开发自定义Windows窗体控件的。 .NET框架

所有最好的:)

How would I go about finding out how a Windows Forms Control was made? I want to create a Control from scratch. Preferably a ListBox or even better, a ListView Control, but I have no idea where to start.

Some suggestions I've come across in the past have been:

Use a Panel Control and dynamically add Label controls to it with appropriate styling, and; Extend or Subclass the ListView/ListBox Controls, and set OwnerDraw to true, and do your custom drawing in the OnPaint event.

But I want more control than that. I don't just want a ListView Control, I don't want to use a third-party control either (no matter how good [Object ListView] is1. I want my own ListView Control. I don't care how hard it is, but is this possible in Windows Forms? Where should I start?

Would I need to use GDI/GDI+ to draw everything? Would I start with an empty Panel Control and then manually draw each List Item using the System.Drawing namespace?

解决方案

How would I go about finding out how a Windows Forms Control was made?

Simple, Every control is a Window created using CreateWindowEx method (done internally by the Winforms).

In winforms point of view: Control is the base class for all Windows. There are some controls which has been written in unmanaged code like ListView, ListBox etc. For them you can't see the paint code in .net. It is implemented in OS itself(not sure which dll they live). Winforms just provides a wrapper over those unmanaged controls.

But, there are purely managed controls written in c#. Example: DataGridView. You can go through the code. Here master is OnPaint protected method. That is the place where you need to write all your custom painting logic with the Graphics instance provided.

Key is you'll create a "Datastructure" which holds all the necessary items to draw your control. Lets say ItemRectangle, Text, Color, Font, etc.. Then you use them all together to paint your custom control in OnPaint method.

Would I need to use GDI/GDI+ to draw everything?

You'll use System.Drawing and System.Drawing.Drawing2D namespaces to draw your control. If something that .net doesn't provide you'll p/invoke Gdi/Gdi+

Advice for choosing base class: If your control needs to be scrollable(ListView kind of controls likely need it). So you can choose ScrollableControl or Panel as base class which supports scrolling. Otherwise you can inherit from Control class.

Developing Custom Windows Forms Controls with the .NET Framework

All the best :)