• 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);
            }
        }
    }
  • 相关阅读:
    让Android模拟器速度飞起来_Eclipse+BlueStacks调试Android应用【2012-10-30】
    开源镜像站-Android镜像
    字符编码的几篇文章
    [C/C++]_[Unicode转Utf8,Ansi转Unicode,Ansi文件转Utf8文件]
    MSVC下快速Unicode I/O
    edltplus使用正则表达式替换多余空行
    修改CMD的编码
    windows 安裝 gcc 編譯器
    CF369 C(递归 + 回溯)
    VIM支持系统剪切板
  • 原文地址:https://www.cnblogs.com/qingtianhua/p/3574274.html
Copyright © 2020-2023  润新知