您的位置:首页 > 其它

How to make gradient colored Controls in Windows Forms

2010-10-28 10:32 435 查看
本文转自http://www.codeproject.com/Tips/104986/How-to-make-gradient-colored-Controls-in-Windows-F.aspx

Create a class library project, add a customControl item to the solution, then make that class inherits from Panel instead of Control and override the OnPaint method to be like this:


Collapse | Copy Code
protected override void OnPaint(PaintEventArgs e)
{
if (this.ClientRectangle.Height == 0 this.ClientRectangle.Width == 0) return;

//get the graphics object of the control
Graphics g = e.Graphics;

//The drawing gradient brush
LinearGradientBrush brush = new LinearGradientBrush(this.ClientRectangle, BackColor1, BackColor2, FillMode);

//Fill the client area with the gradient brush using the control's graphics object
g.FillRectangle(brush, this.ClientRectangle);
}


build and get the dll, add it to your toolbox and use it instead of the normal Panel, by the way you can do the same thing to all controls to have a better UI
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: