You can inherit from ComboBox
and override WndProc
and handle WM_PAINT
message and draw border for your combo box:
using System;
using System.Drawing;
using System.Windows.Forms;
public class FlatCombo : ComboBox
{
private const int WM_PAINT = 0xF;
private int buttonWidth = SystemInformation.HorizontalScrollBarArrowWidth;
Color borderColor = Color.Blue;
public Color BorderColor
{
get { return borderColor; }
set { borderColor = value; Invalidate(); }
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_PAINT && DropDownStyle != ComboBoxStyle.Simple)
{
using (var g = Graphics.FromHwnd(Handle))
{
using (var p = new Pen(BorderColor))
{
g.DrawRectangle(p, 0, 0, Width - 1, Height - 1);
var d = FlatStyle == FlatStyle.Popup ? 1 : 0;
g.DrawLine(p, Width - buttonWidth - d,
0, Width - buttonWidth - d, Height);
}
}
}
}
}
Note:
- In the above example I used fore color for border, you can add a
BorderColor
property or use another color.
- If you don't like the left border of dropdown button, you can comment that
DrawLine
method.
- You need to draw line when the control is
RightToLeft
from (0, buttonWidth)
to (Height, buttonWidth)
- To learn more about how to render a flat combo box, you can take a look at source code of internal
ComboBox.FlatComboAdapter
class of .Net Framework.
Flat ComboBox
You may also like Flat ComboBox:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…