如何在c#中自定义winform的控件箱

时间:2013-03-13 05:40:42

标签: c# .net winforms controlbox

我想自定义winform的控件箱,其背景和不同的按钮图像。我怎么能这样做?有没有办法用usercontrol或类似的东西制作自定义控件箱,然后将其添加到winform?

3 个答案:

答案 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 { ... }
  • 将Controlbox属性的值更改为false。
  • 更改您想要自定义的任何属性。
  • 创建新表单。
  • 更改表单的类型。

变化:

   public partial class MyForm : Form { ... }

有关:

public partial class MyForm : MyCustomControl { ... }

现在您的MyForm就像您的MyCustomControl,您可以在所有项目中重复使用。