一种方法是直接使用图像作为表单BackgroundImage.
如果要在程序上实现此功能(更灵活),可以使用OnPaintBackground以下方法手动绘制表单的背景:
protected override void OnPaintBackground(PaintEventArgs e)
{
using (var brush = new LinearGradientBrush
(DisplayRectangle, Color.Black, Color.DarkGray, LinearGradientMode.Vertical))
{
e.Graphics.FillRectangle(brush, DisplayRectangle);
}
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
Invalidate(); // Force repainting on resize
}
Run Code Online (Sandbox Code Playgroud)
结果:
