效果如下图,本人网上搜索资料加上自己的研究终于实现了在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控件中子项加删除按钮(原创)的更多相关文章

  1. GridView控件中插入自定义删除按钮并弹出确认框

    GridView控件中插入自定义删除按钮,要实现这个功能其实有多种方法,这里先记下我使用的方法,以后再添加其他方法. 一.实现步骤 1.在GridView中添加模板列(TemplateField). ...

  2. winform combobox控件绑定 分类: WinForm 2014-04-17 14:34 118人阅读 评论(0) 收藏

    想要达到的效果:把数据库中的一列数据绑定到combobox控件中. 数据库表:T_Task//任务表 列名:Task_Name//名称 主键:Task_ID combobox控件名称:cbName 解 ...

  3. 向combobox控件中添加元素

    函数定义: bool FillComboBox(CComboBox* pc, CStringList& slValues, bool bOnlyUniqueValues = false); 函 ...

  4. winform ComboBox控件反选

    winform ComboBox控件反选:int index = comboBox1.FindString(textBox2.Text); comboBox1.SelectedIndex = inde ...

  5. Winform ComboBox控件高亮显示

    //重绘下拉表单窗口,需要在窗口设计代码中加入下面这一句 this.cmdChannelName.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawF ...

  6. 02、获取 WebView 控件中,加载的 HTML 网页内容

    在开发 app 的时候,WebView 是经常使用的控件.而且有时需要向 WebView 中的 html 内容 注入额外的 js 进行操作.这里记录一下在当前 WebView 控件中,获取 html ...

  7. 将数据表中的数据添加到ComboBox控件中

    实现效果: 知识运用: ComboBox控件的DataSource 属性 //获取或设置ComboBox的数据源 public Object DataResouce{get;set;} //属性值:任 ...

  8. (Winform)控件中添加GIF图片以及运用双缓冲使其不闪烁以及背景是gif时使控件(如panel)变透明

    Image img = Image.FromFile(@"C:\Users\joeymary\Desktop\3.gif"); pictureBox1.Image =img.Clo ...

  9. winform WebBrowser控件中,cs后台代码执行动态生成的js

    很多文章都是好介绍C# 后台cs和js如何交互,cs调用js方法(js方法必须是页面上存在的,已经定义好的),js调用cs方法, 但如果想用cs里面执行动态生成的js代码,如何实现呢? 思路大致是这样 ...

随机推荐

  1. JAVA中集合类的使用

    总的说来常用的集合类有两大类:Collection 和 Map 1) Collection接口有List和Set两大类子接口,List有ArrayList.LinkedList.Vector子类,Se ...

  2. jquery mobile转场时加载js失效(转)

    jquery mobile拦截了所有的http请求,并使用ajax请求取代传统的http.请求发出后,框架会将请求的内容插入到页面中data- role="page"的部分,取代原 ...

  3. python wechat_sdk间接性的出现错误OfficialAPIError: 40001,说access_token已过期或者不是最新的。

    原因是部署django时使用了多进程,每个进程都会去请求access_token,只有最新的那个有效

  4. Mysql 学习笔记 20140219

    1. Mysql常用命令:每个命令以分号结束. create database name;          创建数据库 use databasename;              选择数据库 dr ...

  5. android依赖工程,子工程覆盖父工程的相同属性

    当业务复杂,开始拆分工程后,就会遇到如下问题: 工程A为lib父工程, 工程B为子工程.  两个工程中均有资源文件(比如strings.xml),且资源文件中有相同的key, 那么值到底使用谁的? 实 ...

  6. jdbc调用sparksql

    将hive-site.xml拷贝到spark目录下conf文件夹 local模式 spark-sql --driver-class-path /usr/local/hive-1.2.1/lib/mys ...

  7. [ CodeVS冲杯之路 ] P3955

    不充钱,你怎么AC? 题目:http://codevs.cn/problem/3955/ 最长上升子序列的加强版,n 有1000000,n 方的 DP 肯定会 TLE,那么用二分栈维护 二分栈我讲不好 ...

  8. Modifiers

    Sometimes it is useful for a function to modify the objects it gets as parameters. In that case, the ...

  9. USACO Section 4.4 追查坏牛奶Pollutant Control

    http://www.luogu.org/problem/show?pid=1344 题目描述 你第一天接手三鹿牛奶公司就发生了一件倒霉的事情:公司不小心发送了一批有三聚氰胺的牛奶.很不幸,你发现这件 ...

  10. 洛谷P2734 游戏 A Game

    P2734 游戏 A Game 27通过 60提交 题目提供者该用户不存在 标签USACO 难度普及+/提高 提交  讨论  题解 最新讨论 暂时没有讨论 题目背景 有如下一个双人游戏:N(2 < ...