閱讀英文

共用方式為


BehaviorService 類別

定義

重要

部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。

管理設計工具中的使用者介面。 此類別無法獲得繼承。

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

取得裝飾項 (Adorner) 視窗的 Graphics

CurrentBehavior

取得行為堆疊頂端的 Behavior,而不必將它移除。

方法

AdornerWindowPointToScreen(Point)

將裝飾項視窗中的 Point 轉譯成螢幕座標。

AdornerWindowToScreen()

取得螢幕座標中裝飾項 (Adorner) 視窗的位置。

ControlRectInAdornerWindow(Control)

傳回 Rectangle 的週框 Control

ControlToAdornerWindow(Control)

傳回轉譯成裝飾項 (Adorner) 視窗座標之 Control 的位置。

Dispose()

釋放 BehaviorService 所使用的所有資源。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetNextBehavior(Behavior)

傳回緊接著行為堆疊中指定的 Behavior 後面的 Behavior

GetType()

取得目前執行個體的 Type

(繼承來源 Object)
Invalidate()

BehaviorService 的裝飾項 (Adorner) 視窗失效。

Invalidate(Rectangle)

讓裝飾項 (Adorner) 視窗內 BehaviorService 的指定區域失效。

Invalidate(Region)

讓裝飾項 (Adorner) 視窗內 BehaviorService 的指定區域失效。

MapAdornerWindowPoint(IntPtr, Point)

將控制代碼座標系統中的某個點轉換成裝飾項視窗座標。

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
PopBehavior(Behavior)

移除並傳回堆疊頂端的 Behavior

PushBehavior(Behavior)

Behavior 推入行為堆疊中。

PushCaptureBehavior(Behavior)

Behavior 推入行為堆疊中,並將滑鼠擷取指派給該行為。

ScreenToAdornerWindow(Point)

將螢幕座標中的點轉譯成 BehaviorService 的裝飾項視窗座標。

SyncSelection()

同步處理所有的選取圖形 (Glyph)。

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

另請參閱