现在的位置: 首页 > 综合 > 正文

一个不完善的可自定义弹出窗体的下拉框

2014年01月17日 ⁄ 综合 ⁄ 共 6873字 ⁄ 字号 评论关闭

namespace WindowsFormsApplication19
{
    partial class UserControl1
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region 组件设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            components = new System.ComponentModel.Container();
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        }

        #endregion
    }
}

 

 

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication19
{
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();

            this.Invalidated += new InvalidateEventHandler(UserControl1_Invalidated);
        }

        void UserControl1_Invalidated(object sender, InvalidateEventArgs e)
        {
            ((Control)sender).CreateGraphics().DrawString(temp.ToString(), new Font("sum", 12), new SolidBrush(Color.Black), new PointF(1, 1));
            temp++;
        }

        int temp = 1;

        //弹出窗体的位置
        private Point GetPopWindowRect()
        {
            //calculate its position in screen coordinates
            Rectangle rect = Bounds;
            rect = this.Parent.RectangleToScreen(rect);
            Point pt = new System.Drawing.Point(rect.Left, rect.Bottom);
            return pt;
        }

        protected override void OnClick(EventArgs e)
        {
            {
                Button btn = new Button();
                PopWindow control = new PopWindow(btn);
                control.closedPopWindow += ClosedPopWindow;
                control.Show(GetPopWindowRect());
            }
        }

        void control_Closed(object sender, ToolStripDropDownClosedEventArgs e)
        {
            throw new NotImplementedException();
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
                this.Parent.Text = "up";
                Rectangle invalidatedArea = this.ClientRectangle;
                System.Windows.Forms.ComboBoxRenderer.DrawTextBox(this.CreateGraphics(), new Rectangle(invalidatedArea.Left,
                    invalidatedArea.Top, invalidatedArea.Width, invalidatedArea.Height), System.Windows.Forms.VisualStyles.ComboBoxState.Normal);

                System.Windows.Forms.ComboBoxRenderer.DrawDropDownButton(this.CreateGraphics(), new Rectangle(
                    invalidatedArea.Left + invalidatedArea.Width - 20,
                    invalidatedArea.Top + 2, 18, invalidatedArea.Height - 4), System.Windows.Forms.VisualStyles.ComboBoxState.Normal);

            base.OnMouseUp(e);
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            {
                this.Parent.Text = "down";
                Rectangle invalidatedArea = this.ClientRectangle;
                System.Windows.Forms.ComboBoxRenderer.DrawTextBox(this.CreateGraphics(), new Rectangle(invalidatedArea.Left,
                    invalidatedArea.Top, invalidatedArea.Width, invalidatedArea.Height), System.Windows.Forms.VisualStyles.ComboBoxState.Pressed);

                System.Windows.Forms.ComboBoxRenderer.DrawDropDownButton(this.CreateGraphics(), new Rectangle(
                    invalidatedArea.Left + invalidatedArea.Width - 20,
                    invalidatedArea.Top + 2, 18, invalidatedArea.Height - 4), System.Windows.Forms.VisualStyles.ComboBoxState.Pressed);
            }
            base.OnMouseDown(e);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Rectangle invalidatedArea = this.ClientRectangle;
            System.Windows.Forms.ComboBoxRenderer.DrawTextBox(this.CreateGraphics(), new Rectangle(invalidatedArea.Left,
                invalidatedArea.Top, invalidatedArea.Width, invalidatedArea.Height), System.Windows.Forms.VisualStyles.ComboBoxState.Normal);

            System.Windows.Forms.ComboBoxRenderer.DrawDropDownButton(this.CreateGraphics(), new Rectangle(
                invalidatedArea.Left + invalidatedArea.Width - 20,
                invalidatedArea.Top+2, 18, invalidatedArea.Height-4), System.Windows.Forms.VisualStyles.ComboBoxState.Normal);
            //this.CreateGraphics().DrawString(temp.ToString(), new Font("sum", 12), new SolidBrush(Color.Black), new PointF(1, 1));
        }
       
        //弹出窗体关闭时的事件
        public void ClosedPopWindow(ToolStripDropDownCloseReason closeReason)
        {
            #region 控件的动作

            if ((ToolStripDropDownCloseReason.Keyboard != closeReason) && (ToolStripDropDownCloseReason.CloseCalled != closeReason))
            {
                //在combobox单击鼠标时canPop为false
                //canPop = !this.RectangleToScreen(this.ClientRectangle).Contains(Cursor.Position);
            }

            #endregion
        }

        /// <summary>
        /// 用于弹出的窗体的类
        /// </summary>
        private class PopWindow : ToolStripDropDown
        {
            #region 委托

            //关闭事件委托
            public delegate void ClosedPopWindow(ToolStripDropDownCloseReason closeReason);

            #endregion

            #region 方法

            public PopWindow(Control content)
            {
                control = content;
                //create a host that will host the content
                host = new ToolStripControlHost(content);

                this.Padding = Margin = host.Padding = host.Margin = Padding.Empty;
                this.MinimumSize = content.MinimumSize;
                content.MinimumSize = content.Size;
                MaximumSize = new Size(content.Size.Width + 1, content.Size.Height + 1);
                content.MaximumSize = new Size(content.Size.Width + 1, content.Size.Height + 1);
                Size = new Size(content.Size.Width + 1, content.Size.Height + 1);

                content.Location = Point.Empty;

                //add the host to the list
                Items.Add(host);
            }

            #endregion

            #region 重载

            //窗口关闭时自动调用函数
            protected override void OnClosed(ToolStripDropDownClosedEventArgs e)
            {
                if (null != closedPopWindow)
                {
                    closedPopWindow(e.CloseReason);
                }
            }

            //显示窗口
            public new void Show(Point screenLocation)
            {
                //显示
                base.Show(screenLocation);
                Focus();
                control.Capture = true;
            }

            //设定焦点
            public new bool Focus()
            {
                return control.Focus();
            }

            #endregion

            #region 变量

            //弹出的基窗体
            private ToolStripControlHost host;

            //具体显示的窗体
            Control control;

            //关闭事件委托
            public ClosedPopWindow closedPopWindow = null;

            #endregion
        }
    }
}

抱歉!评论已关闭.