我想使用此处找到的 ListView 闪烁“较少”控件 Link
直接在我的 C# 项目中。 我不想制作自定义用户控制项目,将其构建为 dll,然后将其导入我的项目。我只想在我正在制作的 C# 程序中使用这一切。
我想我必须在我的项目中添加一个类,并添加代码,但我现在如何直接在我的项目中使用该控件?
最佳答案
在 Visual Studio 中,右键单击您的项目,然后单击 ADD |用户控制
。将新控件命名为 ListViewNF
并单击 ADD
。
查看新类的代码。更改此行:
public partial class ListViewNF : UserControl
为此:
public partial class ListViewNF : ListView
和重建。您将收到有关 AutoScaleMode
的编译器错误 - 只需删除 InitializeComponent
中导致错误的行:
// delete this line:
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
到目前为止,您的代码将如下所示:
public partial class ListViewNF : ListView
{
public ListViewNF()
{
InitializeComponent();
}
}
改成这样:
public partial class ListViewNF : ListView
{
public ListViewNF()
{
InitializeComponent();
//Activate double buffering
this.SetStyle(ControlStyles.OptimizedDoubleBuffer |
ControlStyles.AllPaintingInWmPaint, true);
//Enable the OnNotifyMessage event so we get a chance to filter out
// Windows messages before they get to the form's WndProc
this.SetStyle(ControlStyles.EnableNotifyMessage, true);
}
protected override void OnNotifyMessage(Message m)
{
//Filter out the WM_ERASEBKGND message
if (m.Msg != 0x14)
{
base.OnNotifyMessage(m);
}
}
}
重建您的项目,您现在应该在左侧(右上角)的控件工具箱中看到 ListViewNF
。您可以将此控件拖到设计器中的窗体上,就像普通的 ListView
一样。
关于c# - 直接在 Visual Studio 项目中使用自定义控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2574021/