如何制作渐变背景子窗体c#

时间:2016-08-18 16:27:09

标签: c# gradient transparent

我想做点像

enter image description here

它是一个带有字符串的栏,这个栏是游戏的孩子

我做了孩子父母的事,但我不能使这个酒吧的设计正确

我试过了:

private void label1_Paint(object sender, PaintEventArgs e)
    { 
        Rectangle rec = new Rectangle(label1.Location.X, label1.Location.Y, label1.Width / 3, label1.Height);
        using (LinearGradientBrush brush = new LinearGradientBrush(rec, Color.Transparent, Color.Blue, 45F))
        {
            e.Graphics.FillRectangle(brush, rec);
        }

        Rectangle rec3 = new Rectangle(label1.Width / 3 - 1, label1.Location.Y, label1.Width / 3, label1.Height);
        using (LinearGradientBrush brush = new LinearGradientBrush(rec3, Color.Blue, Color.Blue, 45F))
        {
            e.Graphics.FillRectangle(brush, rec3);
        }

        Rectangle rec2 = new Rectangle(2 * label1.Width / 3 - 1, label1.Location.Y, label1.Width / 3, label1.Height);
        using (LinearGradientBrush brush = new LinearGradientBrush(rec2, Color.Blue, Color.Transparent, -45F))
        {
            e.Graphics.FillRectangle(brush, rec2);
        }

        Font font = new Font("Tahoma", 10f, FontStyle.Bold);
        LinearGradientBrush brush2 = new LinearGradientBrush(new Rectangle(0, 0, Width, Height + 5), Color.White, Color.White, LinearGradientMode.Vertical);
        e.Graphics.DrawString("Hello", font, brush2, label1.Width / 2, label1.Height / 2);
    }
另一种方式:

 private void button2_Click(object sender, EventArgs e)
    {
        Graphics g = label1.CreateGraphics();
        Pen p = new Pen(Brushes.Orange);
        Point p1 = Point.Empty;
        Point p2 = Point.Empty;
        int x_start = label1.Width / 2, y_start = label1.Location.Y;
        int x_size = label1.Width, y_size = 35;
        double increment = 0;
        double current = 255;

        p1.Y = y_start;
        p2.Y = y_size;
        increment = 255 / (double)x_size;

        for (int i = x_start; i < x_start + x_size; i++)
        {
            p1.X = i;
            p2.X = i;
            g.DrawLine(p, p1, p2);
            p.Color = Color.FromArgb((int)current, p.Color);
            current -= increment;
        }

        current = 255;

        for (int j = x_start; j >= label1.Location.X; j--)
        {
            p1.X = j;
            p2.X = j;
            g.DrawLine(p, p1, p2);
            p.Color = Color.FromArgb((int)current, p.Color);
            current -= increment;
        }

        Font font = new Font("Tahoma", 10f, FontStyle.Regular);
        LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(label1.Location.X, label1.Location.Y, label1.Width, label1.Height), Color.White, Color.White, LinearGradientMode.Horizontal);

        using (StringFormat string_format = new StringFormat())
        {
            string_format.Alignment = StringAlignment.Center;
            string_format.LineAlignment = StringAlignment.Center;
            g.DrawString("Hello", font, brush, label1.ClientSize.Width / 2, label1.ClientSize.Height / 2, string_format);
        }
    }

1 个答案:

答案 0 :(得分:0)

以下是带有图片的Label label1Label label2 嵌套的示例:

enter image description here

private void label1_Paint(object sender, PaintEventArgs e)
{
    Rectangle rec = e.ClipRectangle;

    using (LinearGradientBrush brush = 
       new LinearGradientBrush(rec, Color.Transparent, Color.Blue, 0f))
    {

        ColorBlend cblend = new ColorBlend(4);
        cblend.Colors = new Color[4] { Color.Transparent, Color.DarkSlateGray,
                                       Color.DarkSlateGray, Color.Transparent };
        cblend.Positions = new float[4] { 0f, 0.15f, 0.85f, 1f };
        brush.InterpolationColors = cblend;
        e.Graphics.FillRectangle(brush, rec);
    }

    Font font = new Font("Tahoma", 10f, FontStyle.Bold);
    TextFormatFlags fmt = new TextFormatFlags();
    fmt = TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter;

    TextRenderer.DrawText(e.Graphics, "Hello", font, rec, 
                          Color.White, Color.Transparent, fmt);

}

请注意,我使用中心颜色两次,因此我可以将其位置设置在边缘附近的两个端。并且LinearGradientBrush's construcor中的两种颜色从未使用过。

另请注意我如何使用格式标记居中文本而无需任何额外的努力。