简体   繁体   English

子类内置WinForms控件?

[英]Subclass built-in WinForms control?

I've come across the feature in Visual studio to auto-generate a subclass of a custom control using Add New Inherited User Control. 我遇到过Visual Studio中的功能,可以使用“添加新的继承的用户控件”自动生成自定义控件的子类。

But I haven't found a clear description on how to eg create a subclass of Button for instance. 但是我还没有找到关于如何例如创建Button的子类的清晰描述。 Apart from the actual way to do it, I'm also interested if VS provides helpful code-generation for this? 除了实际的实现方式,我还对VS是否为此提供有用的代码生成感兴趣?

You just create your own class that inherits the Control, that you would like to subclass. 您只需要创建自己的类即可继承Control,就可以对其进行子类化。 For instance: 例如:

class BetterButton : Button { ...}

That is the easy part. 那是容易的部分。 Now you have the option to override various methods or properties, depending on what you want to achieve with your new Control. 现在,您可以选择覆盖各种方法或属性,具体取决于要使用新Control实现的功能。 It could be anything, really. 真的可以是任何东西。 One thing I often see used is overriding OnPaint to get the control drawn in a custom way; 我经常看到的一件事是重写OnPaint以自定义方式绘制控件。 and still getting the behaviour of the original control. 仍然可以得到原始控件的行为。

In terms of UserControls, I often see that a "parent" UserControl contains some UI logic and basic UI elements, while the subclassed controls are refinements of the parent for specific use. 在UserControl方面,我经常看到“父” UserControl包含一些UI逻辑和基本UI元素,而子类控件是对父级的细化以用于特定用途。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Silverlight中是否有这样的内置控件? - Is there such built-in control in silverlight? PropertyGrid控件的默认内置编辑器 - Default built-in editors for the PropertyGrid control 如何拦截单击内置的Office功能区控件 - How to intercept clicking of a built-in Office Ribbon control 查询属性和处理事件以用于Outlook加载项中的内置控件 - Querying properties and handling events for built-in control in Outlook Add-in 如何模拟Windows内置的WinForms C#App管理员角色? - How to impersonate a Windows built-in Administrator role for a WinForms, C# App? 在运行 Windows 10 Professional x64 的移动设备中使用内置 FlashLight 的 C# WinForms - C# WinForms using FlashLight built-in into mobile device running Windows 10 Professional x64 WPF 用户控件 - 如果未明确指定,则默认为另一个内置属性的控件属性 - WPF User Control - control property that defaults to another built-in property if not specified explicitly 如何使用从主窗体上的内置控件继承的自定义控件类? - How can I use a custom control class that inherits from a built-in control on my main form? 内置数据库 - Built-In Databases 有内置工厂的字典? - A dictionary with a built-in factory?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM