Actual text color on disabled button

这一生的挚爱 提交于 2019-12-24 01:08:35

问题


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.

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?


回答1:


After writing a very clunky pixel peeping program, it seems that the forecolor is 160,160,160 and the backcolor is 204,204,204. The windows ClearType font smoothing made things a bit difficult, life is never simple eh?

so you would need to set the colours something like this

    Me.ForeColor = Color.FromArgb(160, 160, 160)
    Me.BackColor = Color.FromArgb(204, 204, 204)

or at least thats what they are on my Windows 10 pc with the standard theme anyways.




回答2:


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
    • 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.
  • System.Windows.Forms.ButtonInternal.ButtonBaseAdapter.Paint
  • System.Windows.Forms.ButtonInternal.ButtonStandardAdapter.PaintUp
  • System.Windows.Forms.ButtonInternal.ButtonStandardAdapter.PaintWorker
  • System.Windows.Forms.ButtonInternal.ButtonBaseAdapter.PaintField
  • System.Windows.Forms.ButtonInternal.ButtonBaseAdapter.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);
    }
}

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


来源:https://stackoverflow.com/questions/37596515/actual-text-color-on-disabled-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!