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代码,如何实现呢? 思路大致是这样 ...
随机推荐
- js中表单提交后按钮变灰色的功能
表单提交后按钮变成灰色 http://www.111cn.net/wy/js-ajax/45299.htm
- window上Python环境的搭建
python下载地址:https://www.python.org/ 下载安装 安装完成后配置环境变量,在我的电脑右键属性点高级设置 双击 环境变量 里面第二框找到 path双击 在pytho ...
- HDU 3068 [最长回文子串]
#include<iostream> #include<string> #include<string.h> #include<algorithm> # ...
- ios7 Cocos2dx 隐藏状态栏设置
环境: cocos2d-x 2.1.5 ios7.0 在info.plist 添加 UIViewControllerBasedStatusBarAppearance(View controll ...
- iOS 模态视图
模态视图不是专门的某个类,而是通过视图控制器的presentViewController方法弹出的视图,我们称为模态视图. 模态视图出现的场景一般是临时弹出的窗口,譬如:登录窗口: 模态视图弹出时通过 ...
- FAQ: Automatic Statistics Collection (文档 ID 1233203.1)
In this Document Purpose Questions and Answers What kind of statistics do the Automated tasks ...
- 欧拉路径Hrbust1351
http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1351 这道题先利用并查集的知识点, ...
- 转载KMP
出处: http://www.ruanyifeng.com/blog/2013/05/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm.html 在假期之前,断 ...
- C# OpenFileDialog
OpenFileDialog 用于浏览并打开文件,在Windows Forms中使用,表现为标准的Windows对话框. 实例: 1.新建Windows Form Application 2.添加Op ...
- HTTP权威指南----缓存
缓存的处理步骤: 1.接收----缓存从网络中读取抵达的请求报文2.解析----缓存对报文进行解析,提取出URL和各种首部3.查询----缓存查看是否有本地副本可用,如果没有,就获取一份副本(并将其保 ...