[英]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.