BehaviorService BehaviorService BehaviorService BehaviorService Class

定義

管理設計工具中的使用者介面。Manages user interface in the designer. 這個類別無法被繼承。This class cannot be inherited.

public ref class BehaviorService sealed : IDisposable
public sealed class BehaviorService : IDisposable
type BehaviorService = class
    interface IDisposable
Public NotInheritable Class BehaviorService
Implements IDisposable
繼承
BehaviorServiceBehaviorServiceBehaviorServiceBehaviorService
實作

範例

下列程式碼範例示範如何建立您自己Behavior基礎類別,以回應使用者點選動作。The following code example demonstrates how to create your own Behavior based class that responds to user clicks.

#using <System.dll>
#using <System.Data.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>
#using <System.Design.dll>

using namespace System;
using namespace System::Collections::Generic;
using namespace System::ComponentModel;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Windows::Forms;
using namespace System::Windows::Forms::Design;
using namespace System::Windows::Forms::Design::Behavior;
using namespace System::Text;

namespace BehaviorServiceSample
{

    public ref class UserControl1 : public UserControl
    {
    private:
        System::ComponentModel::IContainer^ components;

    public:
        UserControl1()
        {
            InitializeComponent();
        }

    protected:
        ~UserControl1()
        {
            if (components != nullptr)
            {
                delete components;
            }
        }

    private:
        void InitializeComponent()
        {
            this->Name = "UserControl1";
            this->Size = System::Drawing::Size(170, 156);
        }
    };

    public ref class Form1 : public Form
    {
    private:
        UserControl1^ userControl;

    public:
        Form1()
        {
            InitializeComponent();
        }

    private:
        System::ComponentModel::IContainer^ components;

    protected:
        ~Form1()
        {
            if (components != nullptr)
            {
                delete components;
            }
        }

    private:
        void InitializeComponent()
        {
            this->userControl = gcnew UserControl1();
            this->SuspendLayout();

            this->userControl->Location = System::Drawing::Point(12,13);
            this->userControl->Name = "userControl";
            this->userControl->Size = System::Drawing::Size(143,110);
            this->userControl->TabIndex = 0;

            this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
            this->ClientSize = System::Drawing::Size(184, 153);
            this->Controls->Add(this->userControl);
            this->Name = "Form1";
            this->Text = "Form1";
            this->ResumeLayout(false);
        }
    };


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


    public ref class DemoDesigner : public ControlDesigner
    {
    private:
        Adorner^ demoAdorner;

    protected:
        ~DemoDesigner()
        {
            if (demoAdorner != nullptr)
            {
                System::Windows::Forms::Design::Behavior::BehaviorService^ b = 
                    this->BehaviorService;
                if (b != nullptr)
                {
                    b->Adorners->Remove(demoAdorner);
                }
            }
        }

    public:
        virtual void Initialize(IComponent^ component) override
        {
            __super::Initialize(component);

            // Get a hold of the behavior service and add our own set
            // of glyphs.  Glyphs live on adorners.
            demoAdorner = gcnew Adorner();
            BehaviorService->Adorners->Add(demoAdorner);
            demoAdorner->Glyphs->Add 
                (gcnew DemoGlyph(BehaviorService, Control));
        }
    };
}

[STAThread]
int main()
{
    Application::EnableVisualStyles();
    Application::Run(gcnew BehaviorServiceSample::Form1());
}


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.
            }
        }
    }
}
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Text
Imports System.Windows.Forms.Design
Imports System.Windows.Forms.Design.Behavior

Namespace BehaviorServiceSample

    Public Class Form1
        Inherits System.Windows.Forms.Form

        Private userControl As UserControl1
        Private components As System.ComponentModel.IContainer = Nothing

        Public Sub New()
            MyBase.New()
            InitializeComponent()
        End Sub

        Protected Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing AndAlso (components IsNot Nothing) Then
                components.Dispose()
            End If
            MyBase.Dispose(disposing)
        End Sub


        Private Sub InitializeComponent()
            Me.userControl = New UserControl1()
            Me.SuspendLayout()

            Me.userControl.Location = New System.Drawing.Point(12, 13)
            Me.userControl.Name = "userControl"
            Me.userControl.Size = New System.Drawing.Size(143, 110)
            Me.userControl.TabIndex = 0

            Me.ClientSize = New System.Drawing.Size(184, 153)
            Me.Controls.Add(userControl)
            Me.Name = "Form1"
            Me.Text = "Form1"
            Me.ResumeLayout(False)
        End Sub

        <STAThread()> _
        Shared Sub Main()
            Application.EnableVisualStyles()
            Application.Run(New Form1())
        End Sub

    End Class

    <Designer(GetType(MyDesigner))> _
    Public Class UserControl1
        Inherits UserControl
        Private components As System.ComponentModel.IContainer = Nothing


        Public Sub New()
            InitializeComponent()
        End Sub


        Protected Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing AndAlso (components IsNot Nothing) Then
                components.Dispose()
            End If
            MyBase.Dispose(disposing)
        End Sub


        Private Sub InitializeComponent()
            Me.Name = "UserControl1"
            Me.Size = New System.Drawing.Size(170, 156)
        End Sub 'InitializeComponent
    End Class

    Class MyDesigner
        Inherits ControlDesigner
        Private myAdorner As Adorner


        Protected Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing AndAlso (myAdorner IsNot Nothing) Then
                Dim b As System.Windows.Forms.Design.Behavior.BehaviorService _
                    = BehaviorService
                If (b IsNot Nothing) Then
                    b.Adorners.Remove(myAdorner)
                End If
            End If

        End Sub


        Public Overrides Sub Initialize(ByVal component As IComponent)
            MyBase.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))

        End Sub
    End Class

    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

End Namespace

備註

BehaviorService已建立,它會將設計工具的範圍內執行透明視窗。When the BehaviorService is created, it adds a transparent window over the designer frame. BehaviorService可以使用這個視窗來呈現使用者介面項目,稱為Glyph物件,以及擷取所有滑鼠訊息。The BehaviorService can then use this window to render user interface elements, called Glyph objects, as well as catch all mouse messages. 如此一來,BehaviorService可以控制設計工具的行為。In this way, the BehaviorService can control designer behavior.

BehaviorService類別支援行為堆疊,以便Behavior可以推入物件。The BehaviorService class supports a behavior stack, onto which Behavior objects can be pushed. 當訊息透過 [透明] 視窗中,攔截BehaviorService可以傳送訊息給Behavior堆疊的頂端。When a message is intercepted through the transparent window, the BehaviorService can send the message to the Behavior at the top of the stack. 這可讓不同的使用者介面模式,根據目前的推送BehaviorThis enables different user interface modes depending on the currently pushed Behavior. BehaviorService用來呈現所有Glyph物件,例如選取項目框線,調整大小控點和智慧標籤。The BehaviorService is used to render all Glyph objects, such as selection borders, sizing handles, and smart tags. BehaviorService也會控制許多設計階段行為,例如使用對齊線、 拖曳,然後選取。The BehaviorService also controls many design-time behaviors, such as using snaplines, dragging, and selecting.

如需詳細資訊,請參閱 < 行為服務概觀For more information, see Behavior Service Overview.

屬性

Adorners Adorners Adorners Adorners

取得 BehaviorServiceAdornerCollectionGets the BehaviorServiceAdornerCollection.

AdornerWindowGraphics AdornerWindowGraphics AdornerWindowGraphics AdornerWindowGraphics

取得Graphics裝飾項視窗。Gets the Graphics for the adorner window.

CurrentBehavior CurrentBehavior CurrentBehavior CurrentBehavior

取得Behavior但不移除它的行為堆疊頂端。Gets the Behavior at the top of the behavior stack without removing it.

方法

AdornerWindowPointToScreen(Point) AdornerWindowPointToScreen(Point) AdornerWindowPointToScreen(Point) AdornerWindowPointToScreen(Point)

將轉譯Point為螢幕座標的裝飾項視窗中。Translates a Point in the adorner window to screen coordinates.

AdornerWindowToScreen() AdornerWindowToScreen() AdornerWindowToScreen() AdornerWindowToScreen()

取得螢幕座標中裝飾項 (Adorner) 視窗的位置。Gets the location of the adorner window in screen coordinates.

ControlRectInAdornerWindow(Control) ControlRectInAdornerWindow(Control) ControlRectInAdornerWindow(Control) ControlRectInAdornerWindow(Control)

傳回的周框RectangleControlReturns the bounding Rectangle of a Control.

ControlToAdornerWindow(Control) ControlToAdornerWindow(Control) ControlToAdornerWindow(Control) ControlToAdornerWindow(Control)

傳回的位置Control轉譯成裝飾項視窗座標。Returns the location of a Control translated to adorner window coordinates.

Dispose() Dispose() Dispose() Dispose()

釋放 BehaviorService 所使用的所有資源。Releases all resources used by the BehaviorService.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetNextBehavior(Behavior) GetNextBehavior(Behavior) GetNextBehavior(Behavior) GetNextBehavior(Behavior)

傳回Behavior後面指定Behavior行為堆疊中。Returns the Behavior immediately after the given Behavior in the behavior stack.

GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
Invalidate() Invalidate() Invalidate() Invalidate()

失效的裝飾項視窗BehaviorServiceInvalidates the adorner window of the BehaviorService.

Invalidate(Rectangle) Invalidate(Rectangle) Invalidate(Rectangle) Invalidate(Rectangle)

裝飾項視窗內的指定的區域失效的BehaviorServiceInvalidates, within the adorner window, the specified area of the BehaviorService.

Invalidate(Region) Invalidate(Region) Invalidate(Region) Invalidate(Region)

裝飾項視窗內的指定的區域失效的BehaviorServiceInvalidates, within the adorner window, the specified area of the BehaviorService.

MapAdornerWindowPoint(IntPtr, Point) MapAdornerWindowPoint(IntPtr, Point) MapAdornerWindowPoint(IntPtr, Point) MapAdornerWindowPoint(IntPtr, Point)

將控制代碼座標系統中的某個點轉換成裝飾項視窗座標。Converts a point in a handle's coordinate system to the adorner window coordinates.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
PopBehavior(Behavior) PopBehavior(Behavior) PopBehavior(Behavior) PopBehavior(Behavior)

移除並傳回Behavior堆疊的頂端。Removes and returns the Behavior at the top of the stack.

PushBehavior(Behavior) PushBehavior(Behavior) PushBehavior(Behavior) PushBehavior(Behavior)

推播Behavior至行為堆疊。Pushes a Behavior onto the behavior stack.

PushCaptureBehavior(Behavior) PushCaptureBehavior(Behavior) PushCaptureBehavior(Behavior) PushCaptureBehavior(Behavior)

推播Behavior至行為堆疊並將滑鼠擷取指派的行為。Pushes a Behavior onto the behavior stack and assigns mouse capture to the behavior.

ScreenToAdornerWindow(Point) ScreenToAdornerWindow(Point) ScreenToAdornerWindow(Point) ScreenToAdornerWindow(Point)

將螢幕座標的裝飾項視窗座標中的點轉譯BehaviorServiceTranslates a point in screen coordinates into the adorner window coordinates of the BehaviorService.

SyncSelection() SyncSelection() SyncSelection() SyncSelection()

同步處理所有的選取圖形 (Glyph)。Synchronizes all selection glyphs.

ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)

事件

BeginDrag BeginDrag BeginDrag BeginDrag

發生時BehaviorService啟動拖放作業。Occurs when the BehaviorService starts a drag-and-drop operation.

EndDrag EndDrag EndDrag EndDrag

發生時BehaviorService完成拖曳作業。Occurs when the BehaviorService completes a drag operation.

Synchronize Synchronize Synchronize Synchronize

發生於應重新整理目前的選取範圍時。Occurs when the current selection should be refreshed.

適用於

另請參閱