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.
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:
Enabled comboBox:
Disabled comboBox:
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.
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: