首页 文章

什么是默认GroupBox ForeColor的RGB?

提问于
浏览
1

在Visual Studio(WinForms)中,当您将GroupBox控件拖到Form上时, Headers 标签将获得默认颜色(蓝色) . 现在我知道Forecolor属性显示为“{Name = ControlText,ARGB =(255,0,0,0)}”但是Name = ControlText对我来说没用,ARGB也是如此,它恰好是黑色的 .

它是什么颜色的蓝色?

我希望能够写作

myLabel.ForeColor = Color.FromArgb(?, ?, ?, ?);

并获得完全相同的蓝色阴影 .

(当然这不是火箭科学,但它并不像它看起来那么容易) .

编辑#1 . 基于一些答案,我将澄清并重新解释我的问题 . 我知道它与主题相关联,而GroupBox的ControlText与Label的ControlText不同 . 我在WindowsXP上使用“Windows XP”主题 . 然后,当我使用Windows XP主题时,如何找到应用于GroupBox的蓝色阴影(就RGB而言)?即什么? ? ? ?值 .

2 回答

  • 3

    这完全取决于用户当前的主题 . Windows Classic主题中的组框 Headers 使用了不同的颜色,所有3个标准Windows XP Luna主题,Aero主题,以及Windows 8中可能完全出现的其他内容 . 更不用说它的用户可自定义,这意味着他们甚至可能没有使用标准主题颜色 .

    因此,根据计算机上显示的内容对RGB值进行硬编码并不是一个好计划 . 那是's a good way to make sure that your app will stick out like a sore thumb. Thus, the strategy suggested in the other answers to this question of taking a screenshot and sampling the pixel color is right out. It will be wrong more often than it'对了 .

    相反,您需要向系统询问此值以确保它与用户的当前配置相匹配 . 这可以分为两种一般可能性:

    • 用户已打开Visual Styles(主题)服务,这意味着他们正在使用Luna或Aero等内容 .

    在这种情况下,您使用System.Windows.Forms.VisualStyles命名空间中提供的托管包装器是微不足道的 . 例如,您可以编写以下代码(在C#中任意):

    using System.Windows.Drawing;
    using System.Windows.Forms.VisualStyles;
    // ...
    var vsr = new VisualStyleRenderer(VisualStyleElement.Button.GroupBox.Normal);
    var groupBoxCaptionColor = vsr.GetColor(ColorProperty.TextColor);
    
    • 用户关闭了Visual Styles服务或它不可用(XP之前的Windows版本),这意味着他们正在使用"Windows Classic"主题 .

    在这种情况下,组框使用标准的3D(控制)颜色作为其 Headers ,因此您可以从System.Drawing.SystemColors类中获取该颜色 . 您正在寻找的属性称为 ControlText

    using System.Windows.Drawing;
    // ...
    var groupBoxCaptionColor = SystemColors.ControlText;
    

    In a real application, you'll have to put these two cases together in order to handle all possible client configurations . 如果关闭视觉样式服务,第一条路线将会爆炸,因此您需要首先检查(您可以通过查询Application.RenderWithVisualStyles属性来执行此操作,如果它已关闭,则回退到第二种方法 . 类似于:

    using System.Windows.Drawing;
    using System.Windows.Forms.VisualStyles;
    
    // ...
    
    public Color GroupBoxCaptionColor
    {
      get
      {
        // Test to see if Visual Styles are enabled.
        if (Application.RenderWithVisualStyles())
        {
          // If Visual Styles are enabled, use that color.
          var vsr = new VisualStyleRenderer(VisualStyleElement.Button.GroupBox.Normal);
          return vsr.GetColor(ColorProperty.TextColor);
        }
        else
        {
          // Otherwise, fall back to the Classic theme.
          return SystemColors.ControlText;  
        }
      }
    }
    

    GroupBoxCaptionColor 属性将返回与用于组框控件 Headers 的当前颜色相对应的 Color 对象 . 这个 Color 对象在技术上具有RGB值,但你必须经历所有这首歌曲和舞蹈,以确保你的应用程序使用的颜色始终与当前主题颜色同步 .

    它完全结束了 . 您必须考虑是否要在应用程序运行时处理用户更改系统主题的可能性 . 在这种情况下,实际组框控件的 Headers 颜色会发生变化,但是您的编程确定的 Headers 颜色将过时,与旧主题相匹配而不是新主题 .

    该修复程序正在监视SystemEvents.UserPreferenceChanged事件,该事件在用户在“控制面板”中更改其主题时引发 . 在该事件的处理程序方法中,您需要再次获取组框 Headers 颜色,并更新可能正在使用它的任何UI元素 .

    值得特别注意的是,正如上面链接的文档所指出的,这是一个静态事件,这意味着您必须在应用程序关闭时分离事件处理程序,否则您可能会泄漏内存 .

  • 0

    检查当前为GroupBox设置的 ForeColor 是什么,如果它像 ControlText 那样所有这些颜色来自windows主题 . 您可以使用 SystemColors 获取颜色,如下所示,

    myLabel.ForeColor = SystemColors.ControlText;
    

    请检查您是否可以从以下颜色找到它 .

    如果你想得到金色的话: Color.Gold

    enter image description here

    来自here的图片

相关问题