码迷,mamicode.com
首页 > Windows程序 > 详细

Winform ComboBox 下拉列表多显示

时间:2020-12-30 11:21:26      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:size   ext   背景   dbr   man   prope   forms   长度   reg   

参考于

https://www.cnblogs.com/axing/archive/2013/06/07/3123123.html

代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TTTT
{
public partial class ComboBoxA : ComboBox
{
#region 属性设置
/// <summary>
/// 鼠标移动上的背景颜色
/// </summary>
public Color MouseOverBackground { get; set; } = Color.Gray;
/// <summary>
/// 鼠标移动上的字体颜色
/// </summary>
public Color MouseOverForeground { get; set; } = Color.Orange;
/// <summary>
/// 边框颜色
/// </summary>
public Color BorderBrush { get; set; } = Color.Black;
/// <summary>
/// 边框大小
/// </summary>
public int BorderSize { get; set; } = 0;
/// <summary>
/// 单元格大小
/// </summary>
public int ColumnWidth { get; set; } = 100;
#endregion
private int OldItemHeight = 0;//用于记录加载时的ItemHeight

public ComboBoxA()
{
InitializeComponent();
DrawMode = DrawMode.OwnerDrawFixed;
}
protected override void OnDropDown(EventArgs e)
{
DropDownWidth = (DataManager.Count - 1) * ColumnWidth;//根据字段数量与每个字段的长度进行设置最大长度
OldItemHeight = ItemHeight;//保存加载时的Item高度
ItemHeight = 24;//Item高度变高 不然居中就出界了
base.OnDropDown(e);
}
protected override void OnDropDownClosed(EventArgs e)
{
ItemHeight = OldItemHeight;//将高度变回去
base.OnDropDownClosed(e);
}
protected override void OnDrawItem(DrawItemEventArgs e)
{
Pen BorderPen = new Pen(BorderBrush, BorderSize);//边框画笔
e.Graphics.FillRectangle(new SolidBrush(BackColor), e.Bounds);//正常背景
if (e.State.ToString().IndexOf(DrawItemState.Selected.ToString())!=-1)//选中背景
{
e.Graphics.FillRectangle(new SolidBrush(MouseOverBackground), e.Bounds);
}
if (BorderSize > 0)//因为即使BorderSize也会画出来 所以直接判断
e.Graphics.DrawRectangle(BorderPen, e.Bounds);//绘制边框
PropertyDescriptorCollection ps = DataManager.GetItemProperties();//所有的标题/列名
for (int f = 0; f < ps.Count; f++)
{
Font ValueMemberFont = new Font("", Font.Size + 2);//Value的字体
ValueMemberFont = new Font(ValueMemberFont, FontStyle.Underline);
String column = FilterItemOnProperty(Items[e.Index], ps[f].DisplayName).ToString();//获取一个这个位置的字段
e.Graphics.DrawString(column, f == 0 ? ValueMemberFont : Font, e.State == DrawItemState.Selected ? new SolidBrush(MouseOverForeground) : new SolidBrush(ForeColor), f * ColumnWidth, e.Bounds.Y + Font.Size / 2);//尝试绘制一个上下居中文本
if (BorderSize > 0)
e.Graphics.DrawLine(BorderPen, ColumnWidth * (f + 1), 0, ColumnWidth * (f + 1), ItemHeight * Items.Count);//竖线
}
base.OnDrawItem(e);
}
}
}

Winform ComboBox 下拉列表多显示

标签:size   ext   背景   dbr   man   prope   forms   长度   reg   

原文地址:https://www.cnblogs.com/QuXingBai-Blog/p/14189195.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!