Behavior 类

定义

表示由 Behavior 管理的 BehaviorService 对象。

public ref class Behavior abstract
public abstract class Behavior
type Behavior = class
Public MustInherit Class Behavior
继承
Behavior

示例

下面的代码示例演示如何基于 Behavior 响应用户单击的 类创建自己的类。 此代码示例是为 BehaviorService 类提供的一个更大示例的一部分。


// By providing our own behavior we can do something
// interesting when the user clicks or manipulates our glyph.
public  ref class DemoBehavior : public Behavior
{
public:
    bool OnMouseUp(Glyph^ g, MouseButtons^ button)
    {
        MessageBox::Show("Hey, you clicked the mouse here");

        // indicating we processed this event.
        return true;
    }
};

public ref class DemoGlyph : public Glyph
{
    Control^ control;
    BehaviorService^ behavior;

public:
    DemoGlyph(BehaviorService^ behavior, Control^ control):
      Glyph(gcnew BehaviorServiceSample::DemoBehavior)
      {
          this->behavior = behavior;
          this->control = control;
      }

public:
    virtual property Rectangle Bounds
    {
        Rectangle get() override
        {
            // 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 = behavior->ControlToAdornerWindow(control);
            Size size = control->Size;
            Point center = Point(edge.X + (size.Width / 2),
                edge.Y + (size.Height / 2));

            Rectangle bounds = Rectangle(center.X - 5,
                center.Y - 5, 10, 10);

            return bounds;
        }
    }

public:
    virtual Cursor^ GetHitTest(Point p) override
    {
        // 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 nullptr;
    }

public:
    virtual void Paint(PaintEventArgs^ pe) override
    {
        // Draw our glyph.  Our's is simple:  a blue ellipse.
        pe->Graphics->FillEllipse(Brushes::Blue, Bounds);
    }
};
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.
        }
    }
}
Class MyGlyph
    Inherits Glyph
    Private control As Control
    Private behaviorSvc As _
        System.Windows.Forms.Design.Behavior.BehaviorService

    Public Sub New(ByVal behaviorSvc As _
        System.Windows.Forms.Design.Behavior.BehaviorService, _
        ByVal control As Control)

        MyBase.New(New MyBehavior())
        Me.behaviorSvc = behaviorSvc
        Me.control = control
    End Sub

    Public Overrides ReadOnly Property Bounds() As Rectangle
        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.
            Dim edge As Point = behaviorSvc.ControlToAdornerWindow(control)
            Dim size As Size = control.Size
            Dim center As New Point(edge.X + size.Width / 2, edge.Y + _
                size.Height / 2)

            Dim bounds1 As New Rectangle(center.X - 5, center.Y - 5, 10, 10)

            Return bounds1
        End Get
    End Property

    Public Overrides Function GetHitTest(ByVal p As Point) As Cursor
        ' 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) Then
            Return Cursors.Hand
        End If

        Return Nothing

    End Function


    Public Overrides Sub Paint(ByVal pe As PaintEventArgs)
        ' Draw our glyph.  It is simply a blue ellipse.
        pe.Graphics.FillEllipse(Brushes.Blue, Bounds)

    End Sub

    ' By providing our own behavior we can do something interesting
    ' when the user clicks or manipulates our glyph.

    Class MyBehavior
        Inherits System.Windows.Forms.Design.Behavior.Behavior

        Public Overrides Function OnMouseUp(ByVal g As Glyph, _
            ByVal button As MouseButtons) As Boolean
            MessageBox.Show("Hey, you clicked the mouse here")
            Return True
            ' indicating we processed this event.
        End Function 'OnMouseUp
    End Class

End Class

注解

可以扩展此类以开发任何类型的用户界面行为,包括选择、拖动和调整大小行为。

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

注意

类型 Behavior 必须与类型 Glyph 相关联。 不支持与字形无关的行为。

构造函数

Behavior()

初始化 Behavior 类的新实例。

Behavior(Boolean, BehaviorService)

用给定的 Behavior 初始化 BehaviorService 类的新实例。

属性

Cursor

获取应为此行为显示的光标。

DisableAllCommands

获取一个值,该值指示是否应该禁用 MenuCommand 对象。

方法

Equals(Object)

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

(继承自 Object)
FindCommand(CommandID)

截获命令。

GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
OnDragDrop(Glyph, DragEventArgs)

允许自定义的拖放行为。

OnDragEnter(Glyph, DragEventArgs)

允许自定义的拖入行为。

OnDragLeave(Glyph, EventArgs)

允许自定义的拖离行为。

OnDragOver(Glyph, DragEventArgs)

允许自定义的拖过行为。

OnGiveFeedback(Glyph, GiveFeedbackEventArgs)

允许自定义的拖放反馈行为。

OnLoseCapture(Glyph, EventArgs)

在丢失鼠标捕获时由装饰器窗口调用。

OnMouseDoubleClick(Glyph, MouseButtons, Point)

在任何双击消息进入 BehaviorService 的装饰器窗口时调用。

OnMouseDown(Glyph, MouseButtons, Point)

在任何鼠标按下消息进入 BehaviorService 的装饰器窗口时调用。

OnMouseEnter(Glyph)

在任何鼠标输入消息进入 BehaviorService 的装饰器窗口时调用。

OnMouseHover(Glyph, Point)

在任何鼠标悬停消息进入 BehaviorService 的装饰器窗口时调用。

OnMouseLeave(Glyph)

在任何鼠标离开消息进入 BehaviorService 的装饰器窗口时调用。

OnMouseMove(Glyph, MouseButtons, Point)

在任何鼠标移动消息进入 BehaviorService 的装饰器窗口时调用。

OnMouseUp(Glyph, MouseButtons)

在任何鼠标弹起消息进入 BehaviorService 的装饰器窗口时调用。

OnQueryContinueDrag(Glyph, QueryContinueDragEventArgs)

将此拖放事件从装饰器窗口发送到相应的 Behavior 或经过命中测试的 Glyph

ToString()

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

(继承自 Object)

适用于

另请参阅