BehaviorService 类

定义

在设计器中管理用户界面。 此类不能被继承。

C#
public sealed class BehaviorService : IDisposable
继承
BehaviorService
实现

示例

下面的代码示例演示如何创建响应用户单击的基于自己的 Behavior 类。

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Text;
using System.Windows.Forms.Design;
using System.Windows.Forms.Design.Behavior;

namespace BehaviorServiceSample
{
    class Form1 : Form
    {
        private UserControl1 userControl;

        public Form1()
        {
            InitializeComponent();
        }

        private System.ComponentModel.IContainer components = null;

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.userControl = new BehaviorServiceSample.UserControl1();
            this.SuspendLayout();

            this.userControl.Location = new System.Drawing.Point(12, 13);
            this.userControl.Name = "userControl";
            this.userControl.Size = new System.Drawing.Size(143, 110);
            this.userControl.TabIndex = 0;
            
            this.ClientSize = new System.Drawing.Size(184, 153);
            this.Controls.Add(this.userControl);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
        }

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.Run(new Form1());
        }
    }

    [Designer(typeof(MyDesigner))]
    public class UserControl1 : UserControl
    {
        private System.ComponentModel.IContainer components = null;

        public UserControl1()
        {
            InitializeComponent();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.Name = "UserControl1";
            this.Size = new System.Drawing.Size(170, 156);
        }
    }

    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);

            // Add the custom set of glyphs using the BehaviorService. 
            // Glyphs live on adornders.
            myAdorner = new Adorner();
            BehaviorService.Adorners.Add(myAdorner);
            myAdorner.Glyphs.Add(new MyGlyph(BehaviorService, Control));
        }
    }

    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
            {
                // Create a glyph that is 10x10 and sitting
                // in the middle of the control.  Glyph coordinates
                // are in adorner window coordinates, so we must map
                // using the behavior service.
                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 - 5,
                    center.Y - 5,
                    10,
                    10);

                return bounds;
            }
        }

        public override Cursor GetHitTest(Point p)
        {
            // GetHitTest is called to see if the point is
            // within this glyph.  This gives us a chance to decide
            // what cursor to show.  Returning null from here means
            // the mouse pointer is not currently inside of the glyph.
            // Returning a valid cursor here indicates the pointer is
            // inside the glyph, and also enables our Behavior property
            // as the active behavior.
            if (Bounds.Contains(p))
            {
                return Cursors.Hand;
            }

            return null;
        }

        public override void Paint(PaintEventArgs pe)
        {
            // Draw our glyph. It is simply a blue ellipse.
            pe.Graphics.FillEllipse(Brushes.Blue, Bounds);
        }

        // By providing our own behavior we can do something interesting
        // when the user clicks or manipulates our glyph.
        class MyBehavior : Behavior
        {
            public override bool OnMouseUp(Glyph g, MouseButtons button)
            {
                MessageBox.Show("Hey, you clicked the mouse here");
                return true; // indicating we processed this event.
            }
        }
    }
}

注解

BehaviorService创建 时,它会在设计器框架上添加一个透明窗口。 然后,可以使用 BehaviorService 此窗口呈现用户界面元素(称为 Glyph 对象)以及捕获所有鼠标消息。 通过这种方式, BehaviorService 可以控制设计器的行为。

BehaviorService 支持一个行为堆栈,对象可以推送到该 Behavior 堆栈上。 通过透明窗口截获消息时, BehaviorService 可以将消息发送到 Behavior 堆栈顶部的 。 这将根据当前推送 Behavior的 启用不同的用户界面模式。 BehaviorService用于呈现所有Glyph对象,例如选择边框、大小控点和智能标记。 BehaviorService还控制许多设计时行为,例如使用对齐线、拖动和选择。

有关详细信息,请参阅 行为服务概述

属性

Adorners

获取 BehaviorServiceAdornerCollection

AdornerWindowGraphics

获取修饰器窗口的 Graphics

CurrentBehavior

获取位于行为堆栈顶部的 Behavior,但不移除它。

方法

AdornerWindowPointToScreen(Point)

将装饰器窗口中的 Point 转换为屏幕坐标。

AdornerWindowToScreen()

获取修饰工具窗口在屏幕坐标中的位置。

ControlRectInAdornerWindow(Control)

返回 Rectangle 的边界 Control

ControlToAdornerWindow(Control)

返回转换为装饰器窗口坐标的 Control 的位置。

Dispose()

释放由 BehaviorService 使用的所有资源。

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetNextBehavior(Behavior)

返回行为堆栈中紧接在给定 Behavior 之后的 Behavior

GetType()

获取当前实例的 Type

(继承自 Object)
Invalidate()

使 BehaviorService 的装饰器窗口无效。

Invalidate(Rectangle)

在装饰器窗口中,使 BehaviorService 的指定区域无效。

Invalidate(Region)

在装饰器窗口中,使 BehaviorService 的指定区域无效。

MapAdornerWindowPoint(IntPtr, Point)

将句柄的坐标系统中的点转换为修饰工具窗口坐标。

MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
PopBehavior(Behavior)

移除并返回位于堆栈顶部的 Behavior

PushBehavior(Behavior)

Behavior 压到行为堆栈上。

PushCaptureBehavior(Behavior)

Behavior 压到该行为堆栈上,并为该行为分配鼠标捕获。

ScreenToAdornerWindow(Point)

将屏幕坐标中的点转换为 BehaviorService 的装饰器窗口坐标。

SyncSelection()

同步所有所选内容字形。

ToString()

返回表示当前对象的字符串。

(继承自 Object)

事件

BeginDrag

BehaviorService 启动拖放操作时发生。

EndDrag

BehaviorService 完成拖动操作时发生。

Synchronize

在应刷新当前所选内容时发生。

适用于

产品 版本
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

另请参阅