• 扩展WinForm的ComboBox


    个人认为winform的combobox不是那么的好用,所以自己扩展了一下。
        重新定义Items属性,并且支持树结构。
        为每项加入了CheckBox状态。
        丰富的列表项类ListItem。
        效果如图:
        代码清单:
    using System;
    using System.ComponentModel;
    using System.ComponentModel.Design;
    using System.Collections;
    using System.Drawing.Design;
    using System.Drawing.Drawing2D;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Windows.Forms.Design;

    namespace FaibClass.Windows.Forms
    {
        [Designer(
    typeof(ControlDesigner))]
        
    public class ComboBox : System.Windows.Forms.ComboBox
        


            
    #endregion


            
    CheckedListItemCollection CheckedListItemCollection
            
    public class CheckedListItemCollection : CollectionBase
            
    {
                
    public void Add(ListItem item)
                
    {
                    List.Add(item);
                }


                
    public void Remove(ListItem item)
                
    {    
                    List.Remove(item);
                }


                
    public ListItem this[int index]
                
    {
                    
    get{
                        
    if(index < 0 || index > List.Count - 1)
                            
    throw new ArgumentNullException("索引值超出集合的范围");
                        
    return List[index] as ListItem;
                    }

                }

            }

            
    #endregion
            
            
    ComboBoxWindow ComboBoxWindow
            
    internal class ComboBoxWindow : NativeWindow
            
    {
                
    private System.Windows.Forms.ComboBox.ObjectCollection items;
                
    private IntPtr handle = IntPtr.Zero;

                
    internal ComboBoxWindow(System.Windows.Forms.ComboBox.ObjectCollection items, IntPtr handle)
                
    {
                    
    this.items = items;
                    
    this.handle = handle;
                }


                
    protected override void WndProc(ref Message m)
                
    {
                    
    if(m.Msg == 0x201)//WM_LBUTTONDOWN
                    {
                        Win32.RECT rect 
    = new Win32.RECT();
                        Win32.GetClientRect(m.HWnd, 
    ref rect);
                        Win32.POINTAPI pt 
    = new Win32.POINTAPI();
                        pt.x 
    = Win32.LOWORD(m.LParam.ToInt32());
                        pt.y 
    = Win32.HIWORD(m.LParam.ToInt32());
                        
    //如果在区域内
                        if(new Rectangle(rect.Left, rect.Top,rect.Left + rect.Right, rect.Top + rect.Bottom).Contains(new Point(pt.x, pt.y)))
                        
    {
                            
    int nItemHeight = Win32.SendMessage(m.HWnd, 0x1A100);//LB_GETITEMHEIGHT
                            
    //获得顶部项的索引
                            int nTopIndex = Win32.SendMessage(m.HWnd, 0x18E00); //LB_GETTOPINDEX
                            int nIndex = nTopIndex + pt.y / nItemHeight;
                            
    if (items.Count == 0)
                            
    {
                                
    base.WndProc (ref m);
                                
    return;
                            }

                            
    //判断是否在复选框处
                            if(pt.x > ((ListItem)items[nIndex]).checkboxLeft && pt.x < ((ListItem)items[nIndex]).checkboxLeft + 16)
                            
    {
                                Win32.RECT re 
    = new Win32.RECT();
                                
    //取位置
                                Win32.SendMessage(m.HWnd, 0x198, nIndex, ref re); //LB_GETITEMRECT
                                
    //重画
                                Win32.InvalidateRect(m.HWnd, re, 0);
                                
    //发送自定消息勾选复选框
                                Win32.SendMessage(handle, 0x400 + 0x105, nIndex, 0);
                                
    return;
                            }

                        }

                    }

                    
    base.WndProc (ref m);
                }

            }

            
    #endregion

        }

    }


    using System;
    using System.ComponentModel;
    using System.ComponentModel.Design;
    using System.Collections;
    using System.Drawing.Design;
    using System.Windows.Forms;
    using System.Drawing;

    namespace FaibClass.Windows.Forms


    using System;
    using System.Drawing.Design;
    using System.ComponentModel;
    using System.ComponentModel.Design;
    using System.ComponentModel.Design.Serialization;
    using System.Globalization;
    using System.Reflection;

    namespace FaibClass.Windows.Forms


    using System;
    using System.Runtime.InteropServices;

    namespace FaibClass.Windows.Forms

    作者:wpf之家
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    一张图看懂开源许可协议,开源许可证GPL、BSD、MIT、Mozilla、Apache和LGPL的区别
    常用Flex 布局scss
    设置npm registry的几种方法
    JavaScript计算平方数的三种方法
    NPM 使用介绍
    x 的 y次幂科学计数法
    Docker 容器使用
    使用dos的tree命令输出文件夹树
    赣州(虔州)历史文化
    vue通过$ref获取不到元素样式?
  • 原文地址:https://www.cnblogs.com/wpf123/p/2347474.html
Copyright © 2020-2023  润新知