Din*_*lla 9 .net c# combobox winforms
在我的应用程序中,我添加了Combobox,如下图所示
我已将组合框属性设置为
cmbDatefilter.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
Run Code Online (Sandbox Code Playgroud)
而现在我的问题是如何将边框样式设置为组合框以使其看起来不错.
我在下面的链接验证
我的问题与以下链接不同.
您可以继承ComboBox
并覆盖WndProc
和处理WM_PAINT
消息,并为组合框绘制边框:
public class FlatCombo:ComboBox
{
private const int WM_PAINT = 0xF;
private int buttonWidth= SystemInformation.HorizontalScrollBarArrowWidth;
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_PAINT)
{
using (var g = Graphics.FromHwnd(Handle))
{
using (var p = new Pen(this.ForeColor))
{
g.DrawRectangle(p, 0, 0, Width - 1, Height - 1);
g.DrawLine(p, Width - buttonWidth, 0, Width - buttonWidth, Height);
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
注意:
BorderColor
属性或使用其他颜色。DrawLine
方法进行注释。RightToLeft
从(0, buttonWidth)
到时,您需要画线(Height, buttonWidth)
ComboBox.FlatComboAdapter
.Net Framework 内部类的源代码。CodingGorilla 有正确的答案,从 ComboBox 派生您自己的控件,然后自己绘制边框。
下面是一个绘制 1 像素宽的深灰色边框的工作示例:
class ColoredCombo : ComboBox
{
protected override void OnPaintBackground(PaintEventArgs e)
{
base.OnPaintBackground(e);
using (var brush = new SolidBrush(BackColor))
{
e.Graphics.FillRectangle(brush, ClientRectangle);
e.Graphics.DrawRectangle(Pens.DarkGray, 0, 0, ClientSize.Width - 1, ClientSize.Height - 1);
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
11238 次 |
最近记录: |