zoukankan      html  css  js  c++  java
  • C# 自定义控件的设计时鼠标响应事件

    这两天用到了自定义控件的设计时鼠标响应事件,查了些资料,记下来备查。
    要实现自定义控件的设计时鼠标响应事件,需要分别从Glyph类和Behavior类派生出自己的类,然后重写Behavior类的OnMouseDown事件,详细信息可以参考MSDN中的BehaviorService Class
    1。添加引用System.Design.dll
    2。设计时鼠标响应事件支持类DesignSupport.cs

    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Windows.Forms.Design;
    using System.Windows.Forms.Design.Behavior;
       
    public class MyDesigner : ControlDesigner
    {
        private Adorner myAdorner;
        protected override void Dispose(bool disposing)
        {
            if (disposing && myAdorner != null)
            {
                BehaviorService b = BehaviorService;
                if (b != null)
                {
                    b.Adorners.Remove(myAdorner);
                }
            }
        }
        public override void Initialize(IComponent component)
        {
            base.Initialize(component);
            myAdorner = new Adorner();
            this.BehaviorService.Adorners.Add(myAdorner);
            myAdorner.Glyphs.Add(new MyGlyph(BehaviorService, Control));
        }
    }
       
    public class MyGlyph : Glyph
    {
        Control control;
        BehaviorService behaviorSvc;
        public MyGlyph(BehaviorService behaviorSvc, Control control)
            : base(new MyBehavior())
        {
            this.behaviorSvc = behaviorSvc;
            this.control = control;
        }
       
        public override Rectangle Bounds
        {
            get
            {
                Point edge = behaviorSvc.ControlToAdornerWindow(control);
                Size size = control.Size;
                Point center = new Point(edge.X + (size.Width / 2),
                        edge.Y + (size.Height / 2));
       
                Rectangle bounds = new Rectangle(
                    center.X - 10,
                    center.Y - 10,
                    20,
                    20);
                return bounds;
            }
        }
       
        public override Cursor GetHitTest(Point p)
        {
            if (Bounds.Contains(p))
            {
                return Cursors.Hand;
            }
            return null;
        }
       
        public override void Paint(PaintEventArgs pe)
        {
            pe.Graphics.FillEllipse(Brushes.Red, Bounds);
        }
       
        class MyBehavior : Behavior
        {
            public override bool OnMouseDown(Glyph g, MouseButtons button, Point mouseLoc)
            {
                MessageBox.Show("you clicked at " + mouseLoc.ToString());
                return base.OnMouseDown(g, button, mouseLoc);
            }
        }
    }

    3。自定义控件类MyControl.cs

    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;
       
    namespace MyControls
    {
        [Designer(typeof(MyDesigner))]
        public class MyControl : Control
        {
            private System.ComponentModel.IContainer components = null;
       
            private void InitializeComponent()
            {
                this.SuspendLayout();
                this.BackColor = Color.LightCyan;
                this.ResumeLayout(false);
            }
       
            public MyControl()
            {
                InitializeComponent();
            }
       
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
       
            protected override void OnPaint(PaintEventArgs pe)
            {
                base.OnPaint(pe);
            }
        }
    }
  • 相关阅读:
    PHP7函数大全(4553个函数)
    Mysql 查看连接数,状态 最大并发数
    linux安装git
    PHP new StdClass() 创建空对象
    PHP 如何向关联数组指定的 Key 之前插入元素
    php 常用 小知识点
    PHP激活用户注册验证邮箱
    php rsa 加密、解密、签名、验签
    PHP支付接口RSA验证
    [2018-12-07]用ABP入门DDD
  • 原文地址:https://www.cnblogs.com/qingtianhua/p/3574274.html
Copyright © 2011-2022 走看看