在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → C# 自定义ComboBox实现下拉任意组件 示例源码

C# 自定义ComboBox实现下拉任意组件 示例源码

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:0.08M
  • 下载次数:82
  • 浏览次数:1326
  • 发布时间:2018-06-29
  • 实例类别:C#语言基础
  • 发 布 人:crazycode
  • 文件格式:.rar
  • 所需积分:2
 相关标签: C# ComboBox 组件 c 源码

实例介绍

【实例简介】

【实例截图】

from clipboard

【核心代码】

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Threading;

namespace Test
{
    public partial class HsComboBox : ComboBox
    {
        //用于模拟键盘输入
        [DllImport("user32.dll")]
        private static extern void keybd_event(
            byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);

        //显示控件
        //Control Control;
        //下拉面板
        Panel panel;

        //绘制面板用变量
        //光标前一位置
        Point pPoint;
        //光标当前位置
        Point cPoint;
        //鼠标是否已按下
        bool isMouseDown = false;

        //关闭下拉时光标是否在ComboBox上
        bool isCursorOnComboBox = false;
        
        [Browsable(false)]
        public Control Control { get; set; }
        [Browsable(false)]
        public CheckedListBox CheckedListBox { get; set; }
        [Browsable(false)]
        public TreeView TreeView { get; set; }

        //控件类型
        public enum TypeC
        {
            //默认普通ComboBox
            ComboBox = 0,
            CheckedListBox = 1,
            TreeView = 2,
            //普通Control
            Control = 99
        }

        /// <summary>
        /// 单位是否可见
        /// </summary>
        private TypeC _CtlType;
        [Description("设置下拉类型。")]
        [DefaultValue(0)]
        public TypeC CtlType
        {
            get
            {
                return _CtlType;
            }
            set
            {
                _CtlType = value;
                switch (_CtlType)
                {
                    case TypeC.ComboBox:
                        break;
                    case TypeC.CheckedListBox:
                        CheckedListBox CheckedListBox = new CheckedListBox();
                        this.SetDropDown(CheckedListBox);
                        break;
                    case TypeC.TreeView:
                        TreeView TreeView = new TreeView();
                        this.SetDropDown(TreeView);
                        break;
                    default:
                        Control Control = new Panel();
                        this.SetDropDown(Control);
                        break;
                }
            }
        }

        public HsComboBox()
        {
            InitializeComponent();
        }

        public HsComboBox(IContainer container)
        {
            container.Add(this);

            InitializeComponent();

            //设置下拉样式为DropDownList,不能手动输入
            this.DropDownStyle = ComboBoxStyle.DropDownList;
            //绘制下拉面板
            this.DrawPanel();
        }

        /// <summary>
        /// 点击事件
        /// </summary>
        protected override void  OnMouseClick(MouseEventArgs e)
        {
            //如果为Null则为原生ComboBox
            if (this._CtlType != TypeC.ComboBox)
            {
                if (isCursorOnComboBox)
                {
                    isCursorOnComboBox = false;
                    //模拟Enter键,取消掉下拉状态
                    keybd_event(0xD, 0, 0, 0);
                    keybd_event(0xD, 0, 0x0002, 0);
                }
                else
                {
                    //创建下拉窗
                    ToolStripControlHost toolStripControlHost = new ToolStripControlHost(this.panel);
                    HsToolStripDropDown toolStripDropDown = new HsToolStripDropDown();
                    //设置边框
                    toolStripControlHost.Margin = Padding.Empty;
                    toolStripControlHost.Padding = Padding.Empty;
                    toolStripControlHost.AutoSize = false;
                    toolStripDropDown.Padding = Padding.Empty;
                    //添加
                    toolStripDropDown.Items.Add(toolStripControlHost);
                    toolStripDropDown.Show(this, 0, this.Height);
                    //设置宽度最小值
                    if (this.panel.Width < this.Width)
                    {
                        this.panel.Size = new System.Drawing.Size(this.Width, this.panel.Height);
                    }
                    //判断关闭时光标在ComboBox组件内
                    toolStripDropDown.Closed  = delegate(object sender, ToolStripDropDownClosedEventArgs e1)
                    {
                        Rectangle rec = new Rectangle(0, 0, this.Width, this.Height);
                        this.isCursorOnComboBox = rec.Contains(this.PointToClient(Cursor.Position));
                    };
                    //设置焦点
                    toolStripDropDown.Focus();
                }
            }
        }

        /// <summary>
        /// 绘制下拉面板
        /// </summary>
        public void DrawPanel()
        {
            this.panel = new Panel();
            this.panel.Size = new System.Drawing.Size(this.Width, 100);
            this.panel.Padding = new Padding(1, 1, 1, 13);
            this.panel.BackColor = Color.Gainsboro;
            //绘制边线
            this.panel.Paint  = delegate(object sender, PaintEventArgs e)
            {
                ControlPaint.DrawBorder(e.Graphics,
                               this.panel.ClientRectangle,
                               Color.DarkGray,
                               1,
                               ButtonBorderStyle.Solid,
                               Color.DarkGray,
                               1,
                               ButtonBorderStyle.Solid,
                               Color.DarkGray,
                               1,
                               ButtonBorderStyle.Solid,
                               Color.DarkGray,
                               1,
                               ButtonBorderStyle.Solid);
            };
            //使用Label实现右下角拖动按钮
            Label label = new Label();
            label.Text = "◢";
            label.Font = new Font("宋体", 9);
            label.Parent = this.panel;
            label.AutoSize = true;
            label.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right);
            label.Location = new Point(this.panel.Location.X   this.panel.Size.Width - label.Width   3,
                this.panel.Location.Y   this.panel.Size.Height - label.Height - 1);
            //实现缩放功能
            label.MouseDown  = delegate(object sender, MouseEventArgs e1)
            {
                this.pPoint = Cursor.Position;
                this.isMouseDown = true;
            };
            label.MouseLeave  = delegate(object sender, EventArgs e1)
            {
                this.isMouseDown = false;
            };
            label.MouseMove  = delegate(object sender, MouseEventArgs e1)
            {
                this.cPoint = Cursor.Position;
                if (e1.Button == MouseButtons.Left && isMouseDown)
                {
                    this.panel.Height = Math.Max(this.panel.Height   cPoint.Y - pPoint.Y, 23);
                    this.panel.Width = Math.Max(this.panel.Width   cPoint.X - pPoint.X, this.Width);
                    pPoint = Cursor.Position;
                }
                else
                {
                    label.Cursor = Cursors.SizeNWSE;
                }
            };
        }

        /// <summary>
        /// 设置下拉内容-普通Control
        /// </summary>
        public void SetDropDown(Control control)
        {
            //需要将此前的Control注销掉,否则会一直显示在Panel中
            if(this.Control != null)
            {
                this.Control.Dispose();
            }
            this.Control = control;
            this.Control.Location = new Point(0, 0);
            this.Control.Dock = DockStyle.Fill;
            this.Control.Parent = this.panel;
            //将下拉高度设为1,实现隐藏效果
            this.DropDownHeight = 1;
        }

        /// <summary>
        /// 设置下拉内容-复选框列表
        /// </summary>
        public void SetDropDown(CheckedListBox checkedListBox)
        {
            //单击可选中
            checkedListBox.CheckOnClick = true;
            //边框格式
            checkedListBox.BorderStyle = BorderStyle.None;
            //去掉强制的高度修改,CheckedListBox的一个特殊高度判断,如果为true则会在底端多出一段空白
            checkedListBox.IntegralHeight = false;
            //选中事件添加监听(此时还未选中,刚Check还没有Checked)
            checkedListBox.ItemCheck  = delegate(object sender, ItemCheckEventArgs e)
            {
                String text = "";
                for (int i = 0; i < ((CheckedListBox)(this.Control)).Items.Count; i   )
                {
                    //使用异或特殊处理当前正在check的条目
                    if ((i == e.Index) != ((CheckedListBox)(this.Control)).GetItemChecked(i))
                    {
                        text  = ((CheckedListBox)(this.Control)).Items[i].ToString()   ",";
                    }
                }
                text = text.Substring(0,Math.Max(text.Length-1,0));
                //显示所有内容
                ShowText(text);
            };
            this.CheckedListBox = checkedListBox;
            this.SetDropDown((Control)checkedListBox);
        }

        /// <summary>
        /// 设置下拉内容-树形列表
        /// </summary>
        public void SetDropDown(TreeView treeView)
        {
            //整行选择
            treeView.FullRowSelect = true;
            //边框格式
            treeView.BorderStyle = BorderStyle.None;
            //选中事件添加监听
            treeView.AfterSelect  = delegate(object sender, TreeViewEventArgs e)
            {
                ShowText(((TreeView)(this.Control)).SelectedNode.Text);
            };
            this.TreeView = treeView;
            this.SetDropDown((Control)treeView);
        }

        /// <summary>
        /// 显示信息
        /// </summary>
        /// <param name="Text">信息内容</param>
        public void ShowText(String Text)
        {
            //当DropDownStyle = DropDownList时不能直接对Text赋值
            this.Items.Clear();
            this.Items.Add(Text);
            this.SelectedIndex = 0;
        }
    }

    /// <summary>
    /// 重写ToolStripDropDown
    /// 使用双缓存减少闪烁
    /// </summary>
    public class HsToolStripDropDown : ToolStripDropDown
    {
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x02000000;//双缓存
                return cp;
            }
        }
    }
    
}

标签: C# ComboBox 组件 c 源码

实例下载地址

C# 自定义ComboBox实现下拉任意组件 示例源码

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警