项目中遇到需要下拉框列表内容为线类型或点类型图形的需求,想到可以使用手绘线条和图形的方式来实现下拉列表内容自定义,记录下来供大家参考学习之用。
在项目中添加一个组件


添加完之后会显示如下设计界面

这里直接点击切换到代码视图即可
注意:新建的组件默认继承Component,我们这里是要自定义一个ComboBox下拉框,所以要继承自ComboBox,此类位于using System.Windows.Forms命名空间下

线的类型列表

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CustomComponents
{
/// <summary>
/// 填充线型
/// </summary>
public partial class FillLineTypeCbo : ComboBox
{
public FillLineTypeCbo()
{
//初始化组件
InitializeComponent();
}
public FillLineTypeCbo(IContainer container)
{
container.Add(this);
InitializeComponent();
InitItems();
}
private void InitItems()
{
this.DrawMode = DrawMode.OwnerDrawFixed;//手动绘制所有元素
this.DropDownStyle = ComboBoxStyle.DropDownList;//下拉框样式设置为不能编辑
this.Items.Clear();//清空原有项
}
public enum LineType
{
DoubleHorizontalLine = 0,//双横线
VerticalLine,//垂直线
LeftSlash,//左斜线
RightSlash,//右斜线
VerticalGridlines,//垂直网格线
CrossGridlines,//交叉网格线
SolidBrush,//实心刷
HollowBrush,//空心刷
}
//protected
protected override void OnDrawItem(DrawItemEventArgs e)
{
if (e.Index >= 0)//判断是否需要重绘
{
int typeId = int.Parse(this.Items[e.Index].ToString());//获取选项id
Font font = new Font("宋体", 9);//定义字体
Rectangle rect = e.Bounds;
//rect.Inflate(-2, -2);//缩放一定大小
rect.Inflate(5, -2);//缩放一定大小
Pen pen = null;
SolidBrush solidBrush = new SolidBrush(Color.Black);
float offSet = rect.Height / 2;
float x = rect.Width / 10;
float y = rect.Top + offSet;//如果设置成e.ItemHeigh*e.Index +offSet,则在选中节点后,由于Item位置固定,因此Item不能显示出来。
switch (typeId)
{
case (int)LineType.DoubleHorizontalLine:
pen = new Pen(Color.Black, 1);
e.Graphics.DrawLine(pen, new PointF(x, y), new PointF(8 * x, y));//绘制水平线
e.Graphics.DrawLine(pen, new PointF(x, y + 5), new PointF(8 * x, y + 5));
//e.Graphics.DrawLine(pen, new PointF(x, y+2), new PointF(x, y+2));
break;
case (int)LineType.VerticalLine:
pen = new Pen(Color.Black, 1);
//绘制垂直线
int xNum = 0;
for (int i = 0; i < 10; i++)
{
e.Graphics.DrawLine(pen, new PointF(x + xNum, y - 4), new PointF(1 * x + xNum, y + 6));
xNum += 7;
}
break;
case (int)LineType.LeftSlash:
pen = new Pen(Color.Black, 1);
//绘制左斜线
int xNumLeftSlash = 0;
for (int i = 0; i < 10; i++)
{
e.Graphics.DrawLine(pen, new PointF(x + xNumLeftSlash, y - 4), new PointF(x + 5 + xNumLeftSlash, y + 7));
xNumLeftSlash += 7;
}
break;
case (int)LineType.RightSlash:
pen = new Pen(Color.Black, 1);
//绘制右斜线
int xNumRightSlash = 0;
for (int i = 0; i < 10; i++)
{
e.Graphics.DrawLine(pen, new PointF(x + xNumRightSlash + 7, y - 4), new PointF(x + xNumRightSlash, y + 7));
xNumRightSlash += 7;
}
break;
case (int)LineType.VerticalGridlines:
pen = new Pen(Color.Black, 1);
//绘制垂直网格线
xNum = 0;
for (int i = 0; i < 10; i++)//绘制竖线
{
e.Graphics.DrawLine(pen, new PointF(x + xNum, y - 4), new PointF(1 * x + xNum, y + 6));
xNum += 7;
自定义图形下拉框

本文介绍如何在项目中创建自定义的ComboBox下拉框,包括线类型、点类型及带图片的列表,通过手绘线条和图形实现下拉列表内容的个性化展示。
最低0.47元/天 解锁文章
1万+

被折叠的 条评论
为什么被折叠?



