MonthCalendar(日历控件)扩展DoubleClick事件

翻译|其它|编辑:郝浩|2007-08-30 09:19:11.000|阅读 1517 次

概述:

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

C#初学并不是很难,学过  C,学过  JAVA  就能很容易入门,但仅仅是入门而已!真正的较量是在开发过程中碰到的一些实际的问题,昨天在开发一个和日历控件(MonthCalendar)有关联的项目,本想使用  DoubleClick  事件使得控件关闭然后传值,就这么简单!可是发现微软既然没有提供  MonthCalendar    DoubleClick  事件,原因不知为何,但问题必须解决,没办法,只能亲手扩展了!
下面就与大家一起分享解决方法!其中参考国外网的一些代码!
首先需要新建一个自定义控件继承  MonthCalendar
其中重写几个重要的方法如  OnMouseDown    OnDoubleClick  方法即可
代码区域
    public partial class Cldar : MonthCalendar
    {
        private Point m_LastClickPosition;
        private long m_LastClickTime;
        private Boolean m_LastClickRaisedDoubleClick;

        public Cldar()
        {
            InitializeComponent();
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            // TODO: Add custom paint code here

            // Calling the base class OnPaint
            base.OnPaint(pe);
        }

        /**//// <summary>
        /// ModeChanged Event.
        /// </summary>
        [Browsable(true)]
        [Category("Basic_Event"), Description("
觸發  MonthCalendar  雙擊時的事件.")]
        public event EventHandler DoubleClick;

        protected override void OnDoubleClick(EventArgs e)
        {
            DoubleClick(this, e);
            base.OnDoubleClick(e);
        }

        bool IsInDoubleClickArea(Point Point1, Point Point2)
        {
            return Math.Abs(Point1.X - Point2.X) <= SystemInformation.DoubleClickSize.Width &&
            Math.Abs(Point1.Y - Point2.Y) <= SystemInformation.DoubleClickSize.Height;
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (!m_LastClickRaisedDoubleClick && System.DateTime.Now.Ticks - m_LastClickTime <= SystemInformation.DoubleClickTime * 10000 && IsInDoubleClickArea(m_LastClickPosition, Cursor.Position))
                {
                    OnDoubleClick(EventArgs.Empty);
                    m_LastClickRaisedDoubleClick = true;
                }
                else
                {
                    m_LastClickRaisedDoubleClick = false;
                }
                m_LastClickPosition = Cursor.Position;
                m_LastClickTime = System.DateTime.Now.Ticks;

            }
            base.OnMouseDown(e);
        }
    }

 


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com

文章转载自:csdn

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP