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代码,如何实现呢? 思路大致是这样 ...
随机推荐
- Java SE 第二十三讲----static关键字and final关键字
1.static关键字 [在二十二讲视频中30分钟开始讲授] 2.static修饰属性:无论一个类生成了多少个对象,所有这些对象共同使用唯一一份静态的成员变量:一个对象对该静态成员变量进行了修改,其他 ...
- C++ 之旅:前言
日前,拿起了C++教材开始学习. 在大学二年级的时候,其实C++已经是我们的必修课程.然而,那时的我刚从C语言的噩梦中逃出来,对C++也不甚喜爱.刚接触编程的我当时实在无法理解譬如下面这段 int x ...
- JAVA的JDBC连接与sql操作
一.前言 本文主要介绍怎样连接数据库.即JDBC的操作.以MySQL为例子. 前提是首先要将驱动jar包放入对应路径中. 二.过程说明 1.加载jdbc驱动程序 <span style=&quo ...
- VC 获 取 当前程序运行路径的几种方法
1.使用APi函数GetModuleFileName char path[MAX_PATH]; GetModuleFileName(NULL, path, MAX_PATH); //获取 ...
- android menu 开发
menu 分类: 选项菜单(OptionsMenu) 上下文菜单(ContextMenu) 子菜单(SubMenu) 弹出菜单(Popup) 首先说 选项菜单(OptionsMenu) 一.方法介 ...
- [HTML5] document.hidden
特殊说明: 通过document.hidden属性,可判断页面是否可见. 如果不可见,则document.hidden为true. 如果可见, 则为false. 但是, 如果该页面只是被其它窗口挡住, ...
- 【Unity Shaders】学习笔记——SurfaceShader(二)两个结构体和CG类型
[Unity Shaders]学习笔记——SurfaceShader(二)两个结构体和CG类型 转载请注明出处:http://www.cnblogs.com/-867259206/p/5596698. ...
- MPI初学-安装及OpenMPI函数说明
一.Mac下OpenMPI的安装 所用电脑:MacBook Pro,OSX 10.11.2 从openmpi官网下载相应版本:OpenMPI 1.8下载 解压文件 双击解压或者tar zxvf ope ...
- iOS检测网络连接状态
官方Demo下载地址:https://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip 将Reachab ...
- Ajax.BeginForm 上传文件
在 Mvc 中上传文件时通常使用 Html.BeginForm 标签,同时对Form 添加属性 enctype = "multipart/form-data",前端代码如下: @H ...