[英]Change ComboBox Border Color in Windows Forms
在我的應用程序中,我添加了組合框,如下圖所示
我已將組合框屬性設置為
cmbDatefilter.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
現在我的問題是如何將邊框樣式設置為組合框,以便它看起來不錯。
我在下面的鏈接中驗證
我的問題與以下鏈接不同。
您可以從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);
}
}
}
}
}
筆記:
BorderColor
屬性或使用其他顏色。DrawLine
方法。RightToLeft
從(0, buttonWidth)
到(Height, buttonWidth)
時需要畫線ComboBox.FlatComboAdapter
類的源代碼。平面組合框
您可能也喜歡平面組合框:
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);
}
}
}
另一種選擇是在 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-
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.