Change WinForms Title Bar Color

In WinForms, custom drawing techniques are usually needed to set the color of the title bar. Here is a basic method.

  1. Create a custom form: Develop a custom form class that inherits from Form, such as CustomForm.
  2. Rewrite the OnPaint method: By overriding the OnPaint method in a custom form class, you can customize the drawing of the form’s title bar and other parts.
  3. Customize the title bar color: Use a Graphics object in the OnPaint method to draw the title bar of the form and set the desired color.

The following is a simple example code demonstrating how to customize the color of the title bar in WinForms.

using System;
using System.Drawing;
using System.Windows.Forms;

public class CustomForm : Form
{
    public CustomForm()
    {
        this.FormBorderStyle = FormBorderStyle.None;
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        // 绘制标题栏背景
        using (Brush brush = new SolidBrush(Color.Blue))
        {
            e.Graphics.FillRectangle(brush, 0, 0, this.Width, 30);
        }

        // 绘制标题文字
        using (Brush brush = new SolidBrush(Color.White))
        {
            e.Graphics.DrawString(this.Text, this.Font, brush, 10, 5);
        }
    }
}

// 在程序入口处创建并显示自定义窗体
static class Program
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        CustomForm form = new CustomForm();
        form.Text = "Custom Title Bar";
        Application.Run(form);
    }
}

Please note, this is just a simple example and you may need to customize the drawing further based on your needs.

bannerAds