[英]Change ComboBox border outline color
我正在嘗試管理 ComboBox 的顏色。 雖然可以更改背景顏色,但我找不到邊框輪廓的屬性。
由於箭頭的原因,在深色主題中僅繪制一個正方形作為邊框是行不通的。 這使我得出結論,該邊框可能是一個實際的圖像文件。
這個可以換嗎?
更新:我已經實施了@AhmedAbdelhameed 的解決方案 - 現在看起來好多了。 但是對於平面樣式,我必須像下面這樣調整矩形:
using (var p = new Pen(this.BorderColor, 1))
{
g.DrawRectangle(p, 0, 0, Width - buttonWidth - 1, Height - 1);
}
我還交換了 'BorderColor' 以匹配我的 UI 的其余部分:
public CustomComboBox()
{
BorderColor = Color.Gray;
}
我現在想要做的是僅在黑暗主題中更改實際的下拉按鈕(可能帶有覆蓋 png)
更新:我已經能夠使用以下代碼向自定義控件添加一個 pricturebox:
using (var g = Graphics.FromHwnd(Handle))
{
using (var p = new Pen(this.BorderColor, 1))
{
g.DrawRectangle(p, 0, 0, Width - buttonWidth - 1, Height - 1);
}
if (Properties.Settings.Default.Theme == "Dark")
{
g.DrawImageUnscaled(Properties.Resources.dropdown, new Point(Width - buttonWidth - 1));
}
}
看起來棒極了! 或多或少是我不明白的巧合,當我在主題組合框中更改主題時,深色下拉按鈕甚至消失了。
在這個答案的幫助下,我能夠得出以下結論:
首先,將以下內容添加到表單中以避免閃爍:
protected override CreateParams CreateParams
{
get
{
CreateParams handleParam = base.CreateParams;
handleParam.ExStyle |= 0x02000000; // WS_EX_COMPOSITED
return handleParam;
}
}
現在,將以下類添加到項目中:
public class CustomComboBox : 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))
{
// Uncomment this if you don't want the "highlight border".
/*
using (var p = new Pen(this.BorderColor, 1))
{
g.DrawRectangle(p, 0, 0, Width - 1, Height - 1);
}*/
using (var p = new Pen(this.BorderColor, 2))
{
g.DrawRectangle(p, 2, 2, Width - buttonWidth - 4, Height - 4);
}
}
}
}
public CustomComboBox()
{
BorderColor = Color.DimGray;
}
[Browsable(true)]
[Category("Appearance")]
[DefaultValue(typeof(Color), "DimGray")]
public Color BorderColor { get; set; }
}
重建項目,用新的CustomComboBox
替換ComboBox控件,將BorderColor
屬性設置為您選擇的顏色,然后你就可以了。
結果:
更新:
使用以下值似乎可以提供更好的結果(特別是在單擊下拉按鈕時) ,但您仍可能需要繪制第一個矩形(上面注釋的那個)以避免僅顯示按鈕周圍的“高亮邊框”:
using (var p = new Pen(this.BorderColor, 3))
{
g.DrawRectangle(p, 1, 1, Width - buttonWidth - 3, Height - 3);
}
要為組合框的整個寬度添加邊框,您必須從繪圖中刪除按鈕寬度。 就我而言,我只是想更改邊框的顏色,因此 1 像素就足夠了。
using (var p = new Pen(this.BorderColor, 1))
{
g.DrawRectangle(p, 0, 0, Width - 1, Height - 1);
}
這意味着自定義組合框的行為與通用組合框完全一樣,例如懸停等
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.