本文介绍了禁用按钮上的实际文本颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

VB2012:我正在创建一个按钮控件并从.NET按钮继承.从这里获取基础 https://blogs.msdn .microsoft.com/jfoscoding/2005/11/10/building-a-splitbutton/由于我要重新绘制按钮,因此我必须引入一些代码来在禁用按钮时更改按钮文本.

VB2012: I am creating a button control and inheriting from the .NET button. Taking the base from here https://blogs.msdn.microsoft.com/jfoscoding/2005/11/10/building-a-splitbutton/ Since I am repainting the button I have to introduce some code to change the button text when disabled.

因此,根据过去的经验,我立即选择了SystemColors.GrayText.但是与普通的.NET按钮相比,禁用时的颜色似乎有点偏离.经过实验之后,非常接近的是SystemColors.ControlDark.我在任何地方都找不到此文档.我是在做这个吗?

So from past experience I went right away with SystemColors.GrayText. But comparing that to a normal .NET button, the color when disabled seems a bit off. After experimenting the one that comes pretty close is SystemColors.ControlDark. I can't find this documented anywhere. Am I doing this poroperly?

推荐答案

对于类似的内容,参考源是你的朋友.如果继续进行这些调用,您会看到System.Windows.Forms.Button类最终会调用ButtonBaseAdapter.DrawText方法来执行按钮上文本的实际绘制.调用堆栈如下所示:

For something like this, Reference Source is your friend. If you follow down through the calls, you'll see that the System.Windows.Forms.Button class eventually calls the ButtonBaseAdapter.DrawText method to perform the actual drawing of the text on the button. The call stack looks like this:

  • System.Windows.Forms.ButtonBase.OnPaint
  • System.Windows.Forms.ButtonBase.PaintControl
    • PaintControl方法调用Adapter.Paint. Adapter是一个属性,它返回当前按钮的图形适配器.它返回的适配器取决于样式(即扁平,弹出,标准).如果它是一个标准样式按钮,则它将调用Button.CreateStandardAdapter方法,该方法将返回一个ButtonStandardAdapter,因此,这就是PaintControl方法将使用的方法.但是,ButtonStandardAdapter.Paint方法是由其基类实现的,因此它就是被调用的方法.
    • System.Windows.Forms.ButtonBase.OnPaint
    • System.Windows.Forms.ButtonBase.PaintControl
      • The PaintControl method calls Adapter.Paint. Adapter is a property which returns the current button's drawing adapter. The adapter it returns depends on the style (viz. flat, popup, standard). If it's a standard style button, then it calls the Button.CreateStandardAdapter method which returns a ButtonStandardAdapter, so that's the one which will be used by the PaintControl method. The ButtonStandardAdapter.Paint method is implemented by its base class, though, so that's the one that gets called.

      在该DrawText方法中,您将看到它绘制了禁用的文本,如下所示:

      In that DrawText method, you'll see that it draws the disabled text like this:

      if (disabledText3D && !Control.Enabled) {
          r.Offset(1, 1);
          using (SolidBrush brush = new SolidBrush(colors.highlight)) {
              g.DrawString(Control.Text, Control.Font, brush, r, stringFormat);
              r.Offset(-1, -1);
              brush.Color = colors.buttonShadow;
              g.DrawString(Control.Text, Control.Font, brush, r, stringFormat);
          }
      }
      

      因此,首先使用colors.highlight绘制偏移量为(1, 1)的文本,然后再次绘制,但这次使用colors.buttonShadow偏移量为(-1, -1). colors变量是对ColorData对象的引用.如果您遵循该代码,则会看到ColorData是通过System.Windows.Forms.ButtonInternal.ButtonBaseAdapter.ColorOptions.Calculate方法创建的.它返回的颜色取决于某些系统设置,例如正常与高对比度模式.但是,在标准情况下,返回的颜色看起来是:

      So, first, it draws the text, offset (1, 1), with colors.highlight, and then it draws it again, but this time offset (-1, -1) with colors.buttonShadow. The colors variable is a reference to a ColorData object. If you follow the code, you'll see that the ColorData is being created by the System.Windows.Forms.ButtonInternal.ButtonBaseAdapter.ColorOptions.Calculate method. The colors it returns are dependent on some system settings, such normal vs. high-contrast mode. But, for a standard situation, it looks like the colors it returns are:

      ColorData.highlight = SystemColors.ControlLightLight
      ColorData.buttonShadow = SystemColors.ControlDark
      

      这篇关于禁用按钮上的实际文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 02:20