using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class CustomForm : Form
{
[DllImport("user32.dll")]
public static extern IntPtr GetWindowDC(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
[DllImport("gdi32.dll")]
public static extern int SetWindowRgn(IntPtr hWnd, IntPtr hRgn, bool bRedraw);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateSolidBrush(int crColor);
[DllImport("gdi32.dll")]
public static extern int FillRect(IntPtr hDC, ref Rectangle lprc, IntPtr hbr);
private const int WM_NCPAINT = 0x85;
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
IntPtr hDC = GetWindowDC(Handle);
Rectangle bounds = new Rectangle(0, 0, Width, 30); // Adjust the height to match your title bar height
IntPtr hBrush = CreateSolidBrush(ColorTranslator.ToWin32(Color.Green));
FillRect(hDC, ref bounds, hBrush);
ReleaseDC(Handle, hDC);
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_NCPAINT)
{
IntPtr hDC = GetWindowDC(Handle);
Rectangle bounds = new Rectangle(0, 0, Width, 30); // Adjust the height to match your title bar height
IntPtr hBrush = CreateSolidBrush(ColorTranslator.ToWin32(Color.Green));
FillRect(hDC, ref bounds, hBrush);
ReleaseDC(Handle, hDC);
}
}
}
You can then use the CustomForm in your application instead of the default Form class. Please note that this code only changes the color of the title bar when it is painted. If you resize the form, the color may not be updated immediately.