c# – 如何在WIndows表单应用程序中将border应用于组合框?
发布时间:2020-12-15 08:12:27 所属栏目:百科 来源:网络整理
导读:在我的应用程序中,我添加了Combobox,如下图所示 我已将组合框属性设置为 cmbDatefilter.FlatStyle = System.Windows.Forms.FlatStyle.Flat; 而现在我的问题是如何将边框样式设置为组合框以使其看起来不错. 我在下面的链接验证 Flat style Combo box 我的问题
在我的应用程序中,我添加了Combobox,如下图所示
我已将组合框属性设置为 cmbDatefilter.FlatStyle = System.Windows.Forms.FlatStyle.Flat; 而现在我的问题是如何将边框样式设置为组合框以使其看起来不错. 我在下面的链接验证 Flat style Combo box 我的问题与以下链接不同. Generic ComboBox in Windows Forms Application How to override UserControl class to draw a custom border? 解决方法
您可以从ComboBox继承并覆盖WndProc并处理WM_PAINT消息并为组合框绘制边框:
public class FlatCombo: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)) { using (var p = new Pen(this.ForeColor)) { g.DrawRectangle(p,Width - 1,Height - 1); g.DrawLine(p,Width - buttonWidth,Height); } } } } } 注意: >在上面的示例中,我使用前景颜色作为边框,您可以添加BorderColor属性或使用其他颜色. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |