覆盖禁用的 FlatStyle ComboBox 的边框颜色 [英] Override border color for disabled FlatStyle ComboBox

查看:24
本文介绍了覆盖禁用的 FlatStyle ComboBox 的边框颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 C# WinForms 应用程序中有一个自定义 ComboBox,我成功地在启用和禁用时覆盖了背景颜色,并在启用时覆盖了边框,但是当控件被禁用时我不知道如何更改其边框的颜色.

I have a custom ComboBox in a C# WinForms application, and I have been successful in overriding the background color when its both enabled and disabled, and overriding the border when its enabled, but when the control is disabled I cannot figure out how to change the color of its border.

目前,我正在捕获发送到自定义控件的 WndProc 消息,如下所示:

Currently, I am catching the WndProc messages being sent to the custom control, something like this:

protected override void WndProc (ref Message m)
{
    base.WndProc(ref m);

    if (m.Msg == WM_PAINT)
    {
        // set the enabled border color here, and it works
        using (var g = Graphics.FromHwnd(Handle)
        {
            using (var p = new Pen(Color.Black))
            {
                g.DrawRectangle(p, 0, 0, Width - 1, Height - 1);
            }
        }
    }
    if (m.Msg == WM_CTLCOLORSTATIC)
    {
        // set the disabled background color here
    ]
    if (m.Msg == WM_NCPAINT)
    {
        // try to set the disabled border color here, but its not working
        using (var g = Graphics.FromHwnd(Handle)
        {
            using (var p = new Pen(Color.Black))
            {
                g.DrawRectangle(p, 0, 0, Width - 1, Height - 1);
            }
        }
    }
}

这里有一些图片可以帮助理解问题,我选择了专门突出问题区域的颜色,这些不是我会使用的实际颜色:

Here are some images to help understand the problem, I have chosen colors that specifically highlight the problem area, these are not the actual colors I would use:

启用组合框:

禁用组合框:

请注意禁用组合框时应用的粗 SystemGrey 边框.这是我想要删除的内容,它在较旧的 Windows 系统上看起来更糟,这些屏幕截图是在 Windows 10 上拍摄的,但我的目标是 Windows Server 2012,它会产生一种奇怪的光环"类型效果,超出控制范围.

Notice the thick SystemGrey border being applied when the comboBox is disabled. This is what I want to remove, it looks worse on older windows systems, these screenshots were taken on Windows 10 but I'm targeting Windows Server 2012 where it produces a strange "halo" type effect that extends outside the control.

查看 MSDN,似乎 WM_NCPAINT 是我想要的消息,但是单步执行代码,此时边界似乎已经绘制.我还尝试查看 WM_CTLCOLORSTATIC 的 MSDN,因为该名称看起来很有希望,但它似乎不会触发除背景颜色之外的任何内容.

Looking at the MSDN, it seems like WM_NCPAINT is the message I want, but stepping through the code the border appears to have already been drawn at this point. I also tried looking at the MSDN for WM_CTLCOLORSTATIC, since the name seems promising, but it doesnt seem like it triggers anything but background colors to be set.

是否还有其他消息我应该查看,或者我是否以错误的方式处理这个问题?我尝试逐步查看每条消息,但我无法分辨是哪个消息触发了设置边框的调用.

Is there another message I should be looking at, or am I approaching this the wrong way? I tried stepping through and looking at each message, but I just cant tell which one is triggering the call for the border to be set.

编辑:请参阅下面的解决方案,这是我想要实现的快速而肮脏的示例,以及解决方案代码的作用:

Edit: See below for the solution, this is a quick and dirty example of what I wanted to achieve, and what the solution code will do:

推荐答案

Edit:由于似乎对为什么其他各种答案不合适存在一些困惑,我将解释该问题.当组合框被禁用时,框架会在它周围生成一个 3 像素宽的边框,使用灰色"的默认窗口背景色.如果您使用默认的窗体背景颜色,您将永远不会看到此边框,只需在 1 像素宽的黑色边框"(它实际上不是边框,它是控制区域的一部分)上绘制即可解决问题.但是,如果您的表单背景是,例如,黑色,您将看到此默认表单颜色边框.这就是以下解决方案解决的问题,我希望这对某人有所帮助.

Edit: since there seems to be some confusion about why the various other answers out there are not appropriate, I will explain the issue. When a combobox is disabled, the framework generates a 3 pixel wide border around it, using the default windows background color of "Grey". If you are using the default windows form background color, you will never see this border, and simply drawing over the 1 pixel wide black "border" (its not actually the border, its part of the control area) will solve the problem. However, if your form background is, for example, Black, you will see this default form color border. This is what the below solution solves, and I hope this helps someone.

结果有时你只需要离开 10 分钟,我想出了如何做到这一点.它可能并不理想,也绝对不优雅,但它确实有效.

Turns out sometimes you just need to step away for 10 minutes, I figured out how to do this. Its probably not ideal, and its definitely not graceful, but it works.

可以实际上在处理 WM_CTLCOLORSTATIC 消息时绘制一个新的边框,我以为你不能,因为矩形太小了,但实际上它确实被绘制了,虽然这绝对不是处理原始边框绘制的消息,它出现在它之后,因此您可以在顶部绘制.

You can in fact draw a new border while handling the WM_CTLCOLORSTATIC message, i thought you couldnt because the rectangle is so small, but it does in fact get drawn, and while this is definitely not the message thats handling the original border drawing, this comes after it so you can draw right over top.

此代码将绘制带有深灰色边框的现有浅灰色边框:

This code will draw over top of the existing light grey border with a dark grey border:

if (m.Msg == WM_CTLCOLORSTATIC)
{
    // set your other colors for disabled controls

    using (var g = Graphics.FromHwnd(Handle)
    {
        using (var p = new Pen(Color.FromArgb(93, 100, 103))
        {
            // its a fat border so we need to draw 3 rectangles to cover it
            g.DrawRectangle(p, 0, 0, Width - 1, Height - 1);
            g.DrawRectangle(p, 1, 1, Width - 3, Height - 3);
            g.DrawRectangle(p, 2, 2, Width - 5, Height - 5);
        }
    }
]

我知道这很恶心,但我在第 4 小时试图在控件周围绘制边界......所以如果有人有更简洁的解决方案,我非常愿意接受它.

I know this is disgusting, but I am on hour 4 of trying to draw a border around a control... so if anyone has a cleaner solution, I am more than open to accepting it.

这篇关于覆盖禁用的 FlatStyle ComboBox 的边框颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆