C# 更改 WinForm 中标题栏的颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11862315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 19:33:22  来源:igfitidea点击:

Changing the color of the title bar in WinForm

c#.netwinformsbackgroundtitlebar

提问by Aravind

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

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

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

采纳答案by Aravind

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);
        }
    }
}

回答by Asif Mushtaq

What you can do is set the FormBorderStyleproperty to Noneand do what ever you want with the form using GDI.

您可以做的是将FormBorderStyle属性设置为None并使用 GDI 对表单执行任何您想要的操作。

回答by Cody Gray

This is easy to do:

这很容易做到:

  1. Right-click on the desktop, and select "Personalize".

  2. Click on the "Window Color" tile at the bottom of the screen.

  3. Choose your new color.

    If your computer is configured to use the Aero theme, you can choose from one of the standard colors or mix one of your own.

    If you're using the Classic theme, you'll see a "Window Color and Appearance" dialog you can use to set colors. Click on the title bar the sample desktop, the one called "Active Window", and then use the "Color 1" and "Color 2" drop-down boxes to pick a new color.

  1. 右键单击桌面,然后选择“个性化”。

  2. 单击屏幕底部的“窗口颜色”图块。

  3. 选择你的新颜色。

    如果您的计算机配置为使用 Aero 主题,您可以选择一种标准颜色或混合一种您自己的颜色。

    如果您使用的是经典主题,您将看到一个“窗口颜色和外观”对话框,您可以使用它来设置颜色。单击示例桌面的标题栏,名为“活动窗口”,然后使用“颜色 1”和“颜色 2”下拉框选择一种新颜色。

I can only assume this is what you meant, because there is absolutely no excuseto change only the color of your application's title bar. There's a reason that this is a system-wide setting.

我只能假设这就是您的意思,因为绝对没有理由只更改应用程序标题栏的颜色。这是一个系统范围的设置是有原因的。

Always obey the user's preferences. If they wanted your title bar to be a different color, they would choose a different color.

始终服从用户的喜好。如果他们希望您的标题栏采用不同的颜色,他们会选择不同的颜色。

回答by Aravind

If you want to have a title bar different from others, consider getting a Community license for this

如果您想拥有与其他人不同的标题栏,请考虑为此获得社区许可证

I tried once and it worked. I just had to install it and change Formto SfForm

我试过一次,它奏效了。我只需要安装它并更改FormSfForm

This is the using statement

这是使用语句

using Syncfusion.WinForms.Controls;

The references for WinForms are Syncfusion.Core.WinFormsand Syncfusion.Shared.Base

WinForms 的参考是Syncfusion.Core.WinFormsSyncfusion.Shared.Base