Behavior 類別

定義

表示 BehaviorBehaviorService管理的物件。

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 類型相關聯。 不支援與符文無關的行為。

建構函式

名稱 Description
Behavior()

初始化 Behavior 類別的新執行個體。

Behavior(Boolean, BehaviorService)

初始化該類別的新實例 Behavior ,給定 BehaviorService

屬性

名稱 Description
Cursor

會獲得應該顯示的游標。

DisableAllCommands

會獲得一個值,指示是否 MenuCommand 應該停用物件。

方法

名稱 Description
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)

當任何雙擊訊息進入 Address 視窗 BehaviorService時,該訊息會被呼叫。

OnMouseDown(Glyph, MouseButtons, Point)

當任何滑鼠按下的訊息進入 Dotner 視窗 BehaviorService時,該訊息會被呼叫。

OnMouseEnter(Glyph)

當任何滑鼠輸入訊息進入 Doter 視窗 BehaviorService時呼叫。

OnMouseHover(Glyph, Point)

當任何滑鼠懸停訊息進入 T. 的 裝飾視窗 BehaviorService時,會呼叫該訊息。

OnMouseLeave(Glyph)

當任何滑鼠離開訊息進入 Dotner 視窗 BehaviorService時,會呼叫。

OnMouseMove(Glyph, MouseButtons, Point)

當任何滑鼠移動訊息進入 Assistant 視窗 BehaviorService時,會被呼叫。

OnMouseUp(Glyph, MouseButtons)

當任何滑鼠向上訊息進入 T. 的 裝飾視窗 BehaviorService時,會被呼叫。

OnQueryContinueDrag(Glyph, QueryContinueDragEventArgs)

將此拖放事件從 Adorner 視窗傳送至相應 Behavior 或經過測試 Glyph的視窗。

ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)

適用於

另請參閱