110,538
社区成员
发帖
与我相关
我的任务
分享
public partial class banner : Panel
{
public banner()
{
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
// SetStyle(ControlStyles.DoubleBuffer, true);
InitializeComponent();
}
public banner(IContainer container)
{
container.Add(this);
InitializeComponent();
}
public struct colorStruct
{
public Color color;
public int position;
public colorStruct(Color color, int postition)
{
this.color = color;
this.position = postition;
}
}
public List<colorStruct> colorList = new List<colorStruct>();
List<Rectangle> rectangle = new List<Rectangle>();
public event EventHandler CreateList;
//需要的时候修改这里即可
void createList()
{
if (CreateList == null)
{
colorList.Add(new colorStruct(Color.FromArgb(116, 161, 203), 0));
colorList.Add(new colorStruct(Color.FromArgb(107, 168, 215), 50));
colorList.Add(new colorStruct(Color.FromArgb(84, 132, 194), 100));
if (image == null)
{
image = new Bitmap(Width, Height);
}
}
else
{
CreateList(this, null);
}
}
Image image;
Bitmap bitmap = new Bitmap("banner.png");
void createRectangles()
{
GraphicsPath pt = new GraphicsPath();
LinearGradientBrush br;
rectangle.Clear();
if (image != null) { image.Dispose(); }
image = new Bitmap(Width, Height);
Graphics g = Graphics.FromImage(image);
int itemHight = 0;
for (int i = 0; i < colorList.Count - 1; i++)
{
rectangle.Add(new Rectangle(0, itemHight, Width, (colorList[i + 1].position - colorList[i].position) * Height / 100));
itemHight = rectangle[i].Height - 1;
br = new LinearGradientBrush(rectangle[i], colorList[i].color, colorList[i + 1].color, LinearGradientMode.Vertical);
pt.Reset();
pt.AddRectangle(rectangle[i]);
g.FillPath(br, pt);
}
g.DrawImage(bitmap, this.Width - bitmap.Width, this.Height - bitmap.Height);
}
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
createRectangles();
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias;
g.CompositingMode = CompositingMode.SourceOver;
if (colorList.Count == 0)
{
createList();
createRectangles();
}
g.DrawImage(image, new Point(0, 0));
}
}
//构造中添加一个,在缓冲区绘制
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
if (Width == 0 || Height == 0)
{
//所在窗体最小化
return;
}
createRectangles();
this.Invalidate();
}