我想自定义winform的控件箱,其背景和不同的按钮图像。我怎么能这样做?有没有办法用usercontrol或类似的东西制作自定义控件箱,然后将其添加到winform?
答案 0 :(得分:0)
您可以通过继承UserControl
这样的
class MyControl : System.Windows.Forms.Button //this could also have been System.Windows.Forms.UserControl or any other existing control type as a template
{
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
//Paint whatever you wish on this buttons graphics using e.Graphics
}
}
编写自定义控件有很多功能。要在这里回答很多。一个很好的参考是:http://msdn.microsoft.com/en-us/library/6hws6h2t.aspx
您可以创建自己的Forms控件并隐藏父控件。
或者也许你可以从System.Windows.Form
继承并创建一个自定义表单。但我自己从未尝试过。
对于使用OnPaint,如果你关心性能和/或闪烁,你必须记住一些规则:What is the right way to use OnPaint in .Net applications?
答案 1 :(得分:0)
使用.NET框架意味着你没有影响力。您必须为表单的非客户区域实现自定义绘图。以下内容可能对您有所帮助:http://www.codeplex.com/wikipage?ProjectName=CustomerBorderForm&title=Painting%20NonClient%20Area
答案 2 :(得分:-1)
我不知道我是否理解正确。你想创建自己的控件吗? 如果是,试试这个:
变化:
public partial class MyCustomControl : Control { ... }
有关:
public partial class MyCustomControl : Form { ... }
变化:
public partial class MyForm : Form { ... }
有关:
public partial class MyForm : MyCustomControl { ... }
现在您的MyForm就像您的MyCustomControl,您可以在所有项目中重复使用。