现象:Combox在设置FlatStyle.Flat时边框不见了
效果:
解决问题思路封装新控件:
public class DBorderComboBox : ComboBox
{private const int WM_PAINT = 0xF;[Browsable(true)][Category("Appearance")][Description("边框颜色")]public Color BorderColor { get; set; } = Color.FromArgb(204, 204, 204); // 默认边框颜色protected override void WndProc(ref Message m){base.WndProc(ref m);if (m.Msg == WM_PAINT){using (Graphics g = Graphics.FromHwnd(this.Handle)){// 获取客户区的边界Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);// 绘制边框g.DrawRectangle(new Pen(this.BorderColor), rect.X, rect.Y, rect.Width - 1, rect.Height - 1);}}}public DBorderComboBox() : base(){}protected override void OnPaint(PaintEventArgs e){base.OnPaint(e);// 绘制边框颜色e.Graphics.DrawRectangle(new Pen(this.BorderColor), 0, 0, this.Width - 1, this.Height - 1);}
}