是否可以在c#中更改组合框下拉列表的边框颜色?
我想将白色边框更改为较深的阴影以匹配深色方案.我搜索了.net文档,找到了DropDownList.BorderStyle属性.但是,我不确定这是否行得通.我正在使用WinForms.
这是我用于组合框的类:
public class FlattenCombo : ComboBox
{
private Brush BorderBrush = new SolidBrush(SystemColors.WindowFrame);
private Brush ArrowBrush = new SolidBrush(SystemColors.ControlText);
private Brush DropButtonBrush = new SolidBrush(SystemColors.Control);
private Color _borderColor = Color.Black;
private ButtonBorderStyle _borderStyle = ButtonBorderStyle.Solid;
private static int WM_PAINT = 0x000F;
private Color _ButtonColor = SystemColors.Control;
public Color ButtonColor
{
get { return _ButtonColor; }
set
{
_ButtonColor = value;
DropButtonBrush = new SolidBrush(this.ButtonColor);
this.Invalidate();
}
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
switch (m.Msg)
{
case 0xf:
Graphics g = this.CreateGraphics();
Pen p = new Pen(Color.Black);
g.FillRectangle(BorderBrush, this.ClientRectangle);
//Draw the background of the dropdown button
Rectangle rect = new Rectangle(this.Width - 17, 0, 17, this.Height);
g.FillRectangle(DropButtonBrush, rect);
//Create the path for the arrow
System.Drawing.Drawing2D.GraphicsPath pth = new System.Drawing.Drawing2D.GraphicsPath();
PointF TopLeft = new PointF(this.Width - 13, (this.Height - 5) / 2);
PointF TopRight = new PointF(this.Width - 6, (this.Height - 5) / 2);
PointF Bottom = new PointF(this.Width - 9, (this.Height + 2) / 2);
pth.AddLine(TopLeft, TopRight);
pth.AddLine(TopRight, Bottom);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//Determine the arrow's color.
if (this.DroppedDown)
{
ArrowBrush = new SolidBrush(SystemColors.HighlightText);
}
else
{
ArrowBrush = new SolidBrush(SystemColors.ControlText);
}
//Draw the arrow
g.FillPath(ArrowBrush, pth);
break;
default:
break;
}
}
[Category("Appearance")]
public Color BorderColor
{
get { return _borderColor; }
set
{
_borderColor = value;
Invalidate(); // causes control to be redrawn
}
}
[Category("Appearance")]
public ButtonBorderStyle BorderStyle
{
get { return _borderStyle; }
set
{
_borderStyle = value;
Invalidate();
}
}
protected override void OnLostFocus(System.EventArgs e)
{
base.OnLostFocus(e);
this.Invalidate();
}
protected override void OnGotFocus(System.EventArgs e)
{
base.OnGotFocus(e);
this.Invalidate();
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
this.Invalidate();
}
}
解决方法:
我为此花了太长时间了.我从上一个问题中看到,您询问您是否已从以下代码中获取代码:http://www.codeproject.com/Articles/2433/Flatten-that-Combobox并设置了BackColor:
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
switch (m.Msg)
{
case 0xf:
base.BackColor = Color.Black;
使用WndProc,这是我得到的最接近的东西,但没有为下拉列表的Popup / ItemSelection边框上色:
…
if (this.DroppedDown)
{
ArrowBrush = new SolidBrush(SystemColors.HighlightText);
Rectangle dropDownBounds = new Rectangle(0, 0, Width,Height + DropDownHeight );
//ControlPaint.DrawBorder(g, dropDownBounds, _borderColor, _borderStyle);
ControlPaint.DrawBorder(g, dropDownBounds, _borderColor,1, ButtonBorderStyle.Dotted ,Color.GreenYellow,1,ButtonBorderStyle.Solid ,Color.Gold,1,ButtonBorderStyle.Dashed,Color.HotPink,1,ButtonBorderStyle.Solid);
}
事实证明,执行此操作所需的类FlatComboBoxAdapter是私有的,建议使用WPF:
ComboBox DropDown-Area Border Color
更多信息:Combobox borderstyle-但是即使有LarsTech和Hans的建议(使用非客户喷漆信息),它仍然不起作用,并且闪烁得可怕.
除了WPF以外的其他建议,请重写Combobox .Net Framework代码:http://www.dotnetframework.org/default.aspx/FX-1434/FX-1434/1@0/untmp/whidbey/REDBITS/ndp/fx/src/WinForms/Managed/System/WinForms/ComboBox@cs/2/ComboBox@cs
标签:combobox,c,winforms 来源: https://codeday.me/bug/20191030/1964762.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。