C# winform combobox控件中子项加删除按钮(原创)
效果如下图,本人网上搜索资料加上自己的研究终于实现了在combobox子项中加上删除按钮。
一、窗体中的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing.Drawing2D; namespace ComboBoxEx
{
public partial class Form1 : Form
{
[DllImport("user32")]
private static extern int GetComboBoxInfo(IntPtr hwnd, out COMBOBOXINFO comboInfo);
struct RECT
{
public int left, top, right, bottom;
}
struct COMBOBOXINFO
{
public int cbSize;
public RECT rcItem;
public RECT rcButton;
public int stateButton;
public IntPtr hwndCombo;
public IntPtr hwndItem;
public IntPtr hwndList;
}
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.DrawMode = DrawMode.OwnerDrawFixed;
comboBox1.Items.Add(new comboItem("sgset", pictureBox1.Image));
comboBox1.Items.Add(new comboItem("sdgsdg", pictureBox1.Image));
comboBox1.Items.Add(new comboItem("sdgsdg", pictureBox1.Image));
}
public Form1()
{
InitializeComponent();
comboBox1.HandleCreated += (s, e) =>
{
COMBOBOXINFO combo = new COMBOBOXINFO();
combo.cbSize = Marshal.SizeOf(combo);
GetComboBoxInfo(comboBox1.Handle, out combo);
hwnd = combo.hwndList;
init = false;
};
}
bool init;
IntPtr hwnd;
NativeCombo nativeCombo = new NativeCombo();
//This is to store the Rectangle info of your Icons
//Key: the Item index
//Value: the Rectangle of the Icon of the item (not the Rectangle of the item)
Dictionary<int, Rectangle> dict = new Dictionary<int, Rectangle>();
public class NativeCombo : NativeWindow
{
//this is custom MouseDown event to hook into later
public event MouseEventHandler MouseDown;
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x201)//WM_LBUTTONDOWN = 0x201
{
int x = m.LParam.ToInt32() & 0x00ff;
int y = m.LParam.ToInt32() >> ;
if (MouseDown != null) MouseDown(null, new MouseEventArgs(MouseButtons.Left, , x, y, ));
}
base.WndProc(ref m);
}
}
private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{ if ((e.State & DrawItemState.Selected) != )//鼠标选中在这个项上
{
//渐变画刷
LinearGradientBrush brush = new LinearGradientBrush(e.Bounds, Color.FromArgb(, , ),
Color.FromArgb(, , ), LinearGradientMode.Vertical);
//填充区域
Rectangle borderRect = new Rectangle(, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height - );
e.Graphics.FillRectangle(brush, borderRect);
//画边框
Pen pen = new Pen(Color.FromArgb(, , ));
e.Graphics.DrawRectangle(pen, borderRect);
}
else
{
SolidBrush brush = new SolidBrush(Color.FromArgb(, , ));
e.Graphics.FillRectangle(brush, e.Bounds);
}
//获得项图片,绘制图片
comboItem item = (comboItem)comboBox1.Items[e.Index];
Image img = item.Img; //图片绘制的区域
Rectangle imgRect = new Rectangle(e.Bounds.Width - , e.Bounds.Y, , );
dict[e.Index] = imgRect;
if (img != null && (e.State & DrawItemState.Selected) != )
{
e.Graphics.DrawImage(img, imgRect);
}
Rectangle textRect =
new Rectangle(, imgRect.Y, e.Bounds.Width - imgRect.Width, e.Bounds.Height + );
string itemText = comboBox1.Items[e.Index].ToString();
StringFormat strFormat = new StringFormat();
strFormat.LineAlignment = StringAlignment.Center;
e.Graphics.DrawString(itemText, new Font("宋体", ), Brushes.Black, textRect, strFormat); } private void comboBox1_DropDown(object sender, EventArgs e)
{
if (!init)
{
//Register the MouseDown event handler <--- THIS is WHAT you want.
nativeCombo.MouseDown += comboListMouseDown;
nativeCombo.AssignHandle(hwnd);
init = true;
}
} //This is the MouseDown event handler to handle the clicked icon
private void comboListMouseDown(object sender, MouseEventArgs e)
{
foreach (var kv in dict)
{
if (kv.Value.Contains(e.Location))
{
//Show the item index whose the corresponding icon was held down
MessageBox.Show(kv.Key.ToString());
return;
}
}
}
}
}
二、子项辅助类
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing; namespace ComboBoxEx
{
public class comboItem : object
{
private Image img = null;
private string text = null; public comboItem()
{ } public comboItem(string text)
{
Text = text;
} public comboItem(string text,Image img)
{
Text = text;
Img = img;
} // item Img
public Image Img
{
get
{
return img;
}
set
{
img = value;
}
} // item text
public string Text
{
get
{
return text;
}
set
{
text = value;
}
} // ToString() should return item text
public override string ToString()
{
return text;
}
}
}
C# winform combobox控件中子项加删除按钮(原创)的更多相关文章
- GridView控件中插入自定义删除按钮并弹出确认框
GridView控件中插入自定义删除按钮,要实现这个功能其实有多种方法,这里先记下我使用的方法,以后再添加其他方法. 一.实现步骤 1.在GridView中添加模板列(TemplateField). ...
- winform combobox控件绑定 分类: WinForm 2014-04-17 14:34 118人阅读 评论(0) 收藏
想要达到的效果:把数据库中的一列数据绑定到combobox控件中. 数据库表:T_Task//任务表 列名:Task_Name//名称 主键:Task_ID combobox控件名称:cbName 解 ...
- 向combobox控件中添加元素
函数定义: bool FillComboBox(CComboBox* pc, CStringList& slValues, bool bOnlyUniqueValues = false); 函 ...
- winform ComboBox控件反选
winform ComboBox控件反选:int index = comboBox1.FindString(textBox2.Text); comboBox1.SelectedIndex = inde ...
- Winform ComboBox控件高亮显示
//重绘下拉表单窗口,需要在窗口设计代码中加入下面这一句 this.cmdChannelName.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawF ...
- 02、获取 WebView 控件中,加载的 HTML 网页内容
在开发 app 的时候,WebView 是经常使用的控件.而且有时需要向 WebView 中的 html 内容 注入额外的 js 进行操作.这里记录一下在当前 WebView 控件中,获取 html ...
- 将数据表中的数据添加到ComboBox控件中
实现效果: 知识运用: ComboBox控件的DataSource 属性 //获取或设置ComboBox的数据源 public Object DataResouce{get;set;} //属性值:任 ...
- (Winform)控件中添加GIF图片以及运用双缓冲使其不闪烁以及背景是gif时使控件(如panel)变透明
Image img = Image.FromFile(@"C:\Users\joeymary\Desktop\3.gif"); pictureBox1.Image =img.Clo ...
- winform WebBrowser控件中,cs后台代码执行动态生成的js
很多文章都是好介绍C# 后台cs和js如何交互,cs调用js方法(js方法必须是页面上存在的,已经定义好的),js调用cs方法, 但如果想用cs里面执行动态生成的js代码,如何实现呢? 思路大致是这样 ...
随机推荐
- Mac 下用 go 开发Android应用环境设置
需要的工具 设置代理 请参考:http://www.cnblogs.com/ghj1976/p/5087049.html Mac 下命令行设置代理: export http_proxy=http:// ...
- Eclipse 常用设置
1. eclipse中的汉字横着显示怎么解决 同一种字体有两种显示方式,比如Fixedsys Excelsior 3.01和@Fixedsys Excelsior 3.01,前一种汉字是竖着显示,后一 ...
- CodeForces 617C【序枚举】
题意: 有两个点喷水,有很多个点有花,给出坐标. 求使得每个花都可以被喷到,两个喷水的半径的平方的和最小是多少. 思路: 枚举其中一个喷水的最大半径. 坑: 这题我贪心的思路有很大问题.一开始也是想这 ...
- [HDU 5135] Little Zu Chongzhi's Triangles (dfs暴搜)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5135 题目大意:给你n条边,选出若干条边,组成若干个三角形,使得面积和最大.输出最大的面积和. 先将边 ...
- The Ninth Hunan Collegiate Programming Contest (2013) Problem J
Problem J Joking with Fermat's Last Theorem Fermat's Last Theorem: no three positive integers a, b, ...
- 使用ImageNet在faster-rcnn上训练自己的分类网络
具体代码见https://github.com/zhiyishou/py-faster-rcnn 这是我对cup, glasses训练的识别 faster-rcnn在fast-rcnn的基础上加了rp ...
- Type-base dispatch
In the previous section we added two Time objects, but you also might want to add an integer to a Ti ...
- OSGI.NET 插件启动方法
在使用OSGI.NET框架来开发插件过程中,有时为了测试一个插件,或运行一个插件,需要启动主个插件,如果没有主窗口程序,那么该 如何启动一个插件,而不是再开发一个主窗口程序(那样是不是太麻烦,仅仅是为 ...
- log4net按等级多种方式记录日志
log4net.config <?xml version="1.0"?> <configuration> <configSections> &l ...
- 洛谷P1206 [USACO1.2]回文平方数 Palindromic Squares
P1206 [USACO1.2]回文平方数 Palindromic Squares 271通过 501提交 题目提供者该用户不存在 标签USACO 难度普及- 提交 讨论 题解 最新讨论 暂时没有 ...