Behavior 類別

定義

表示由 Behavior 管理的 BehaviorService 物件。

C#
public abstract class Behavior
繼承
Behavior

範例

下列程式代碼範例示範如何根據 Behavior 回應用戶點選的 類別建立您自己的類別。 此程式代碼範例是針對 類別提供的較大範例的 BehaviorService 一部分。

C#
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.
        }
    }
}

備註

您可以擴充這個類別,以開發任何類型的使用者介面行為,包括選取、拖曳和重設大小行為。

如需詳細資訊,請參閱 行為服務概觀

備註

您的 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)

允許自訂的拖入 (drag-enter) 行為。

OnDragLeave(Glyph, EventArgs)

允許自訂的拖出 (drag-leave) 行為。

OnDragOver(Glyph, DragEventArgs)

允許自訂的拖過 (Drag-Over) 行為。

OnGiveFeedback(Glyph, GiveFeedbackEventArgs)

允許自訂的拖放回應行為。

OnLoseCapture(Glyph, EventArgs)

裝飾項視窗在其遺失滑鼠捕捉 (Mouse Capture) 時呼叫。

OnMouseDoubleClick(Glyph, MouseButtons, Point)

在任何按兩下滑鼠 (Double-Click) 訊息進入 BehaviorService 的裝飾項視窗時進行呼叫。

OnMouseDown(Glyph, MouseButtons, Point)

會在任何按下滑鼠 (Mouse-Down) 訊息進入 BehaviorService 的裝飾項視窗時進行呼叫。

OnMouseEnter(Glyph)

會在任何滑鼠移入 (Mouse-Enter) 訊息進入 BehaviorService 的裝飾項視窗時進行呼叫。

OnMouseHover(Glyph, Point)

會在任何滑鼠停留 (Mouse-Hover) 訊息進入 BehaviorService 的裝飾項視窗時進行呼叫。

OnMouseLeave(Glyph)

會在任何滑鼠移出 (Mouse-Leave) 訊息進入 BehaviorService 的裝飾項視窗時進行呼叫。

OnMouseMove(Glyph, MouseButtons, Point)

會在任何滑鼠移動 (Mouse-Move) 訊息進入 BehaviorService 的裝飾項視窗時進行呼叫。

OnMouseUp(Glyph, MouseButtons)

會在任何放開滑鼠 (Mouse-Up) 訊息進入 BehaviorService 的裝飾項視窗時進行呼叫。

OnQueryContinueDrag(Glyph, QueryContinueDragEventArgs)

將這個拖放事件從裝飾項 (Adorner) 視窗傳送至適當的 Behavior 或經過點擊測試的 Glyph

ToString()

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

(繼承來源 Object)

適用於

產品 版本
.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

另請參閱