且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何更改禁用组合框的字体颜色?

更新时间:2023-01-26 18:38:57

到目前为止,我制作了自己的组合框,当禁用时具有白色背景(明显使用互联网:)) 。现在我需要添加代码,将字体颜色从灰色更改为黑色。



So far i made own combobox that have white back color when is disabled (using internet obviously:)). Now I need add code, that change font color from gray to black.

public partial class OwnComboBox : ComboBox
{
    [DllImport("gdi32.dll")]
    internal static extern IntPtr CreateSolidBrush(int color);

    [DllImport("gdi32.dll")]
    internal static extern int SetBkColor(IntPtr hdc, int color);

    protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);
        IntPtr brush;
        switch (m.Msg)
        {

            case (int)312:
                SetBkColor(m.WParam, ColorTranslator.ToWin32(this.BackColor));
                brush = CreateSolidBrush(ColorTranslator.ToWin32(this.BackColor));
                m.Result = brush;

                break;
            default:
                break;
        }
    }

}


我一直在寻找有关此事的信息,以及据我所知,***的解决方案是将组合框的DrawMode更改为OwnerDrawFixed或OwnerDrawVariable,然后在组合框的DrawItem事件中编写您自己的绘图代码。



我发现这篇文章详细介绍了它。希望它有所帮助。
I have searched around for information in the past about this, and as far as I can tell, the best solution is to change the DrawMode of the combo box to OwnerDrawFixed or OwnerDrawVariable and then write your own drawing code in the DrawItem event of the combo box.

I found this article that goes into much more detail about it. Hope it helps.