C # WinForm ComboBox control sub-item plus Delete button (original)

Source: Internet
Author: User

Effects such as, I search the information on the Internet and their own research finally realized in the ComboBox subkey plus Delete button.

First, the code in the form:

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Runtime.InteropServices;usingSystem.Drawing.Drawing2D;namespacecomboboxex{ Public Partial classForm1:form {[DllImport ("User32")]        Private Static extern intGetcomboboxinfo (INTPTR hwnd, outcomboboxinfo comboinfo); structRECT { Public intLeft , top, right, bottom; }        structComboboxinfo { Public intcbsize;  PublicRECT RcItem;  PublicRECT Rcbutton;  Public intStatebutton;  PublicIntPtr Hwndcombo;  PublicIntPtr Hwnditem;  PublicIntPtr hwndlist; }        Private voidForm1_Load (Objectsender, EventArgs e) {Combobox1.drawmode=drawmode.ownerdrawfixed; COMBOBOX1.ITEMS.ADD (NewComboItem ("Sgset", pictureBox1.Image)); COMBOBOX1.ITEMS.ADD (NewComboItem ("SDGSDG", pictureBox1.Image)); COMBOBOX1.ITEMS.ADD (NewComboItem ("SDGSDG", pictureBox1.Image)); }         PublicForm1 () {InitializeComponent (); Combobox1.handlecreated+ = (s, e) = ={Comboboxinfo Combo=NewComboboxinfo (); Combo.cbsize=marshal.sizeof (combo); Getcomboboxinfo (Combobox1.handle, outcombo); HWND=combo.hwndlist; Init=false;        }; }        BOOLInit;        INTPTR hwnd; Nativecombo Nativecombo=NewNativecombo (); //This was 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 =Newdictionary<int, rectangle>();  Public classNativecombo:nativewindow {//This is a custom MouseDown event to hooks into later             Public EventMouseEventHandler MouseDown; protected Override voidWndProc (refMessage m) {                if(M.msg = =0x201)//wm_lbuttondown = 0x201                {                    intx = M.lparam.toint32 () &0x00ff; inty = M.lparam.toint32 () >> -; if(MouseDown! =NULL) MouseDown (NULL,NewMouseEventArgs (MouseButtons.Left,1, X, Y,0)); }                Base. WndProc (refm); }        }        Private voidCombobox1_drawitem (Objectsender, DrawItemEventArgs e) {                        if((E.state & drawitemstate.selected)! =0)//The mouse is selected on this item            {                //Gradient Paint BrushLinearGradientBrush brush =NewLinearGradientBrush (E.bounds, Color.FromArgb (255,251,237), Color.FromArgb (255,236,181), lineargradientmode.vertical); //Fill AreaRectangle Borderrect =NewRectangle (0, E.bounds.y, E.bounds.width, E.bounds.height-2);                E.graphics.fillrectangle (brush, borderrect); //Draw BorderPen pen =NewPen (Color.FromArgb (229,195,101));            E.graphics.drawrectangle (pen, borderrect); }            Else{SolidBrush Brush=NewSolidBrush (Color.FromArgb (217,223, the));            E.graphics.fillrectangle (brush, e.bounds); }            //get an item picture, draw a pictureComboItem item =(ComboItem) Combobox1.items[e.index]; Image img=item.            IMG; //the area where the picture is drawnRectangle Imgrect =NewRectangle (E.bounds.width- -, E.bounds.y, the, the); Dict[e.index]=Imgrect; if(img! =NULL&& (E.state & drawitemstate.selected)! =0) {e.graphics.drawimage (img, imgrect); } Rectangle Textrect=NewRectangle (Ten, Imgrect.y, E.bounds.width-imgrect.width, E.bounds.height +2); stringItemtext =Combobox1.items[e.index].            ToString (); StringFormat Strformat=NewStringFormat (); Strformat.linealignment=Stringalignment.center; E.graphics.drawstring (Itemtext,NewFont ("Song Body", -), Brushes.black, Textrect, Strformat); }        Private voidCombobox1_dropdown (Objectsender, EventArgs e) {            if(!init) {                //Register The MouseDown event handler <--- This was what you want.Nativecombo.mousedown + =Combolistmousedown;                Nativecombo.assignhandle (HWND); Init=true; }        }        //The MouseDown event handler to handle the clicked icon        Private voidCombolistmousedown (Objectsender, MouseEventArgs e) {            foreach(varKvinchdict) {                if(KV. Value.contains (e.location)) {//Show the item index whose the corresponding icon was held downMessageBox.Show (KV.                    Key.tostring ()); return; }            }        }    }}

Second, the Child auxiliary class

usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Drawing;namespacecomboboxex{ Public classComboItem:Object    {        PrivateImage img =NULL; Private stringText =NULL;  PublicComboItem () {} PublicComboItem (stringtext) {Text=text; }         PublicComboItem (stringtext,image img) {Text=text; IMG=img; }        //Item IMG         PublicImage Img {Get            {                returnimg; }            Set{img=value; }        }        //Item Text         Public stringText {Get            {                returntext; }            Set{text=value; }        }        //ToString () should return item text         Public Override stringToString () {returntext; }    }}

C # WinForm ComboBox control sub-item plus Delete button (original)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.