首页 新闻 会员 周边 捐助

c# winform重绘comboBox 给选择项添加一个删除按钮

0
悬赏园豆:50 [已解决问题] 解决于 2015-10-29 09:31

c# winform重绘comboBox 给选择想添加一个删除按钮

private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
            if ((e.State & DrawItemState.Selected) != 0)//鼠标选中在这个项上
            {
                //渐变画刷
                LinearGradientBrush brush = new LinearGradientBrush(e.Bounds, Color.FromArgb(255, 251, 237),
                                                 Color.FromArgb(255, 236, 181), LinearGradientMode.Vertical);
                //填充区域
                Rectangle borderRect = new Rectangle(0, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height - 2);
                e.Graphics.FillRectangle(brush, borderRect);
                //画边框
                Pen pen = new Pen(Color.FromArgb(229, 195, 101));
                e.Graphics.DrawRectangle(pen, borderRect);
            }
            else
            {
                SolidBrush brush = new SolidBrush(Color.FromArgb(217, 223, 230));
                e.Graphics.FillRectangle(brush, e.Bounds);
            }
            //获得项图片,绘制图片
            MyItem item = (MyItem)comboBox1.Items[e.Index];
            Image img = item.Img;

            //图片绘制的区域
            if (img != null)
            {
                Rectangle imgRect = new Rectangle(e.Bounds.Width - 30, e.Bounds.Y, 24, 24);
                e.Graphics.DrawImage(img, imgRect);
                Rectangle textRect =
                    new Rectangle(10, imgRect.Y, e.Bounds.Width - imgRect.Width, e.Bounds.Height + 2);
                string itemText = comboBox1.Items[e.Index].ToString();
                StringFormat strFormat = new StringFormat();
                strFormat.LineAlignment = StringAlignment.Center;
                e.Graphics.DrawString(itemText, new Font("宋体", 12), Brushes.Black, textRect, strFormat);
            }
        }

怎样才能给这个image添加一个点击事件

ixxiyy的主页 ixxiyy | 初学一级 | 园豆:14
提问于:2015-10-29 00:17
< >
分享
最佳答案
0

直接用Item的点击事件就可以了,但是点击时判断鼠标有没有落在图片上面,懂吗?

收获园豆:50
XiaoFaye | 老鸟四级 |园豆:3087 | 2015-10-29 08:38

要怎么判断呢,请大神指点

ixxiyy | 园豆:14 (初学一级) | 2015-10-29 08:40

@SnowLove: 

鼠标位置你能知道不?Item的Rect你能知道不?

XiaoFaye | 园豆:3087 (老鸟四级) | 2015-10-29 08:43

@XiaoFaye: 要用哪个事件,combobox_mouseclick吗

ixxiyy | 园豆:14 (初学一级) | 2015-10-29 08:59

@SnowLove: 

public 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;
  }
  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() >> 16;
                if (MouseDown != null) MouseDown(null, new MouseEventArgs(MouseButtons.Left, 1, x, y, 0));                                                                  
            }
            base.WndProc(ref m);
        }
  }
  //DropDown event handler of your comboBox1
  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;
      }
    }
  }
  //DrawItem event handler
  private void comboBox1_DrawItem(object sender, DrawItemEventArgs e) {
     //We have to save the Rectangle info of the item icons
     Rectangle rect = new Rectangle(0, e.Bounds.Top, e.Bounds.Height, e.Bounds.Height);
     dict[e.Index] = rect;
     //Draw the icon
     //e.Graphics.DrawImage(yourImage, rect);   
  }
}
XiaoFaye | 园豆:3087 (老鸟四级) | 2015-10-29 09:28

@XiaoFaye:感谢

ixxiyy | 园豆:14 (初学一级) | 2015-10-29 09:30

@XiaoFaye: 我按照这种方式画出来以后 combobox 没有滚动条了,郁闷

ixxiyy | 园豆:14 (初学一级) | 2015-10-29 15:05

@SnowLove: 

应该是被覆盖住了,你调整下大小位置看看。

XiaoFaye | 园豆:3087 (老鸟四级) | 2015-10-30 02:53
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册