在我的应用程序中,我添加了组合框,如下图所示
我已将组合框属性设置为
cmbDatefilter.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
现在我的问题是如何设置组合框的边框样式,使其看起来不错。
我在下面的链接中验证了
平面样式组合框 http://www.nullskull.com/q/10433090/flat-style-combo-box.aspx
我的问题与下面链接的问题不同。
Windows 窗体应用程序中的通用组合框 https://stackoverflow.com/questions/25832626/generic-combobox-in-windows-forms-application
如何重写 UserControl 类来绘制自定义边框? https://stackoverflow.com/questions/3908384/how-to-override-usercontrol-class-to-draw-a-custom-border
您可以继承自ComboBox
并覆盖并处理WM_PAINT https://learn.microsoft.com/en-us/windows/win32/gdi/wm-paint?WT.mc_id=DT-MVP-5003235消息并为组合框绘制边框:
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
属性或使用其他颜色。
- 如果您不喜欢下拉按钮的左边框,可以评论
DrawLine
method.
- 控制时需要画线
RightToLeft
from (0, buttonWidth)
to (Height, buttonWidth)
- 要了解有关如何渲染平面组合框的更多信息,您可以查看内部的源代码ComboBox.FlatComboAdapter http://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/ComboBox.cs,79ca61e52b2766da.Net Framework 的类。
平面组合框
你也许也喜欢平面组合框 https://stackoverflow.com/a/65976649/3110834:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)