C# 自定义控件的设计时鼠标响应事件

时间:2022-08-31 08:02:24
这两天用到了自定义控件的设计时鼠标响应事件,查了些资料,记下来备查。
要实现自定义控件的设计时鼠标响应事件,需要分别从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);
        }
    }
}