I have the following simple code that should draw a gradient button, added as gradbtn.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace CustomControls.GradientControls
{
public class Gradbtns : Button
{
protected override void OnPaint(PaintEventArgs pevent)
{
base.OnPaint(pevent);
pevent.Graphics.FillRectangle(new LinearGradientBrush(
new PointF(0, this.Height / 2), new PointF(this.Width, this.Height / 2),
Color.AliceBlue, Color.BurlyWood), this.ClientRectangle);
}
}
}
The problem drawing with the above code is that it does not enable the "Button" click action on the Form rather it appears jus as if its drawn on the form.
But using the "OnPaintBackground" event does enable the button but the Colors aren't seen, See Below image
Best Answer
You need to use the OnClick method to implement the function of clicking the button, the following is a validated code example, which has made some optimization of the implementation of gradient background and settings of the control appearance, it should meet your need:
Add code like this to enable the click animation: