表格背景颜色设置为淡色

Rup*_*esh 3 .net background colors winforms

如何设置附加图像中指定的表单的背景颜色?

在此输入图像描述

Ani*_*Ani 5

一种方法是直接使用图像作为表单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)

结果:

梯度