[英]Change ComboBox Border Color in Windows Forms
In My Application i have added Combobox as shown in below picture在我的应用程序中,我添加了组合框,如下图所示
i have set the combobox property as我已将组合框属性设置为
cmbDatefilter.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
And now my question is how to set border style to combobox so that it will look nice.现在我的问题是如何将边框样式设置为组合框,以便它看起来不错。
I verified in below link我在下面的链接中验证
My question is different from below link's.我的问题与以下链接不同。
Generic ComboBox in Windows Forms Application Windows 窗体应用程序中的通用组合框
How to override UserControl class to draw a custom border? 如何覆盖 UserControl 类来绘制自定义边框?
You can inherit from ComboBox
and override WndProc
and handle WM_PAINT
message and draw border for your combo box:您可以从
ComboBox
继承并覆盖WndProc
并处理WM_PAINT
消息并为您的组合框绘制边框:
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:笔记:
BorderColor
property or use another color.BorderColor
属性或使用其他颜色。DrawLine
method.DrawLine
方法。RightToLeft
from (0, buttonWidth)
to (Height, buttonWidth)
RightToLeft
从(0, buttonWidth)
到(Height, buttonWidth)
时需要画线ComboBox.FlatComboAdapter
class of.Net Framework.ComboBox.FlatComboAdapter
类的源代码。 Flat ComboBox平面组合框
You may also like Flat ComboBox :您可能也喜欢平面组合框:
CodingGorilla has the right answer, derive your own control from ComboBox and then paint the border yourself. CodingGorilla 的答案是正确的,从 ComboBox 派生你自己的控件,然后自己绘制边框。
Here's a working example that paints a 1 pixel wide dark gray border:这是一个绘制 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);
}
}
}
Another option is to draw the border yourself in the Parent control's Paint Event:另一种选择是在 Parent 控件的 Paint Event 中自己绘制边框:
Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs) Handles Panel1.Paint
Panel1.CreateGraphics.DrawRectangle(Pens.Black, ComboBox1.Left - 1, ComboBox1.Top - 1, ComboBox1.Width + 1, ComboBox1.Height + 1)
End Sub
-OO- -OO-
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.