效果如下图,本人网上搜索资料加上自己的研究终于实现了在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. struts2访问servlet API

    搭建环境: 引入jar包,src下建立struts.xml文件 项目配置文件web.xml. web.xml: <?xml version="1.0" encoding=&q ...

  2. ArcGIS上根据经纬度求地球表面两点间距离的实现

    ArcGIS上根据经纬度求地球表面两点间距离的实现 以米为单位..Net2.0,C#实现.        public static double DistanceOfTwoPoints(double ...

  3. EntityFramework 4使用存储过程分页

    CREATE PROC usp_OrgPage_SQL @pageIndex INT, @pageSize INT, @totalCount INT OUTPUT AS BEGIN SET @tota ...

  4. C和C++头文件的不同

    #include <IOSTREAM.h>void main(){    std::cout<<"Hello,World!"<<std::end ...

  5. Win8系统 Python安装 - 入门

    原文:http://www.blogbus.com/hx1987-logs/271955446.html 安装python (1)下载python安装包,下载网站https://www.python. ...

  6. Linux神器之Strace的实践(Ubuntu上服务幽灵般的消失)

    不论是运维,还是开发,面对Linux系统,时常会因为配置参数或者系统的权限(iptables限制端口,selinux拦截,文件目录权限等)原因出现程序或者服务异常,无法启动等等.特别是在Linux的文 ...

  7. 你很牛B,面试却没过,为什么?

    点击标题下「飞测」可快速关注 坚持的是分享,搬运的是知识,图的是大家的进步,没有收费的培训,没有虚度的吹水,喜欢就关注.转发(免费帮助更多伙伴)等来交流,想了解的知识请留言,给你带来更多价值,是我们期 ...

  8. POI实现word文档转html文件

    POI word文件转html package com.feiruo.officeConvert; import java.io.BufferedWriter; import java.io.File ...

  9. Flex4+BlazeDS+JAVA+MySql 构建J2EE工程 对用户信息进行管理实例

    要求 必备知识 本文要求基本了解 Adobe Flex编程知识和JAVA基础知识. 开发环境 MyEclipse10/Flash Builder4.6/Flash Player11及以上 演示地址 演 ...

  10. 【LeetCode】11. Container With Most Water

    题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...