用户登录
用户注册

分享至

更改 WinForm 中标题栏的颜色

  • 作者: 双面先生
  • 来源: 51数据库
  • 2023-02-08

问题描述

是否可以在 C# 中更改 WinForm 标题栏的颜色?

Is it possible to change the color of the title bar of a WinForm in C#?

          __________________________
         [Form1_______________-|[]|X] <- I want to change the color of this
         |                          |
         |                          |
         |                          |
         |__________________________|

推荐答案

我解决了这个问题.这是代码:

I solved this problem. This is the code:

[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);

[DllImport("User32.dll")]
private static extern IntPtr GetWindowDC(IntPtr hWnd);

protected override void WndProc(ref Message m)
{
    base.WndProc(ref m);
    const int WM_NCPAINT = 0x85;
    if (m.Msg == WM_NCPAINT)
    {
        IntPtr hdc = GetWindowDC(m.HWnd);
        if ((int)hdc != 0)
        {
            Graphics g = Graphics.FromHdc(hdc);
            g.FillRectangle(Brushes.Green, new Rectangle(0, 0, 4800, 23));
            g.Flush();
            ReleaseDC(m.HWnd, hdc);
        }
    }
}
软件
前端设计
程序设计
Java相关