BehaviorServiceとは? わかりやすく解説

BehaviorService イベント


BehaviorService クラス

メモ : このクラスは、.NET Framework version 2.0 で新しく追加されたものです。

デザイナのユーザー インターフェイスを管理します。このクラスは継承できません。

名前空間: System.Windows.Forms.Design.Behavior
アセンブリ: System.Design (system.design.dll 内)
構文構文

Public NotInheritable Class
 BehaviorService
    Implements IDisposable
Dim instance As BehaviorService
public sealed class BehaviorService : IDisposable
public ref class BehaviorService sealed : IDisposable
public final class BehaviorService implements
 IDisposable
public final class BehaviorService implements
 IDisposable
解説解説
使用例使用例

ユーザーのクリック操作に応答する独自の Behavior に基づいたクラスを作成する方法を次のコード例に示します。

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 Not
 (components Is 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 Not
 (components Is 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 Not
 (myAdorner Is Nothing) Then
                Dim b As System.Windows.Forms.Design.Behavior.BehaviorService
 _
                    = BehaviorService
                If Not (b Is
 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
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.
            }
        }
    }
}
継承階層継承階層
System.Object
  System.Windows.Forms.Design.Behavior.BehaviorService
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

BehaviorService プロパティ


パブリック プロパティパブリック プロパティ

  名前 説明
パブリック プロパティ Adorners BehaviorServiceAdornerCollection を取得します。
パブリック プロパティ AdornerWindowGraphics 装飾ウィンドウの Graphics を取得します。
パブリック プロパティ CurrentBehavior 動作スタックの先頭にある Behavior を削除せずに取得します。
参照参照

関連項目

BehaviorService クラス
System.Windows.Forms.Design.Behavior 名前空間
Behavior クラス
Glyph
Adorner クラス

その他の技術情報

方法 : デザイン モードでコントロールの外観と動作を拡張する
動作サービスの概要

BehaviorService メソッド


パブリック メソッドパブリック メソッド

( プロテクト メソッド も参照)
  名前 説明
パブリック メソッド AdornerWindowPointToScreen 装飾ウィンドウの Point を画面座標に変換します。
パブリック メソッド AdornerWindowToScreen 画面座標での装飾ウィンドウの位置を取得します。
パブリック メソッド ControlRectInAdornerWindow Control の境界 Rectangle を返します。
パブリック メソッド ControlToAdornerWindow 装飾ウィンドウ座標に平行移動された Control の位置を返します。
パブリック メソッド Dispose BehaviorService によって使用されているすべてのリソースを解放します。
パブリック メソッド Equals  オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 ( Object から継承されます。)
パブリック メソッド GetHashCode  特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 ( Object から継承されます。)
パブリック メソッド GetNextBehavior 動作スタック内の指定された Behavior の直後にある Behavior を返します。
パブリック メソッド GetType  現在のインスタンスの Type を取得します。 ( Object から継承されます。)
パブリック メソッド Invalidate オーバーロードされます。 BehaviorService の装飾ウィンドウを無効にします。
パブリック メソッド MapAdornerWindowPoint ハンドルの座標システムの位置を装飾ウィンドウ座標に変換します。
パブリック メソッド PopBehavior スタックの先頭にある Behavior を削除し、返します。
パブリック メソッド PushBehavior 動作スタックに Behavior をプッシュします。
パブリック メソッド PushCaptureBehavior 動作スタックに Behavior をプッシュし、動作に対してマウスのキャプチャを割り当てます。
パブリック メソッド ReferenceEquals  指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。)
パブリック メソッド ScreenToAdornerWindow 画面座標の位置が BehaviorService の装飾ウィンドウ座標に変換されます。
パブリック メソッド SyncSelection すべての選択グリフを同期させます。
パブリック メソッド ToString  現在の Object を表す String を返します。 ( Object から継承されます。)
プロテクト メソッドプロテクト メソッド
参照参照

関連項目

BehaviorService クラス
System.Windows.Forms.Design.Behavior 名前空間
Behavior クラス
Glyph
Adorner クラス

その他の技術情報

方法 : デザイン モードでコントロールの外観と動作を拡張する
動作サービスの概要

BehaviorService メンバ

デザイナのユーザー インターフェイスを管理します。このクラスは継承できません。

BehaviorService データ型で公開されるメンバを以下の表に示します。


パブリック プロパティパブリック プロパティ
  名前 説明
パブリック プロパティ Adorners BehaviorServiceAdornerCollection を取得します。
パブリック プロパティ AdornerWindowGraphics 装飾ウィンドウの Graphics を取得します。
パブリック プロパティ CurrentBehavior 動作スタックの先頭にある Behavior を削除せずに取得します。
パブリック メソッドパブリック メソッド
( プロテクト メソッド も参照)
  名前 説明
パブリック メソッド AdornerWindowPointToScreen 装飾ウィンドウの Point を画面座標に変換します。
パブリック メソッド AdornerWindowToScreen 画面座標での装飾ウィンドウの位置を取得します。
パブリック メソッド ControlRectInAdornerWindow Control の境界 Rectangle を返します。
パブリック メソッド ControlToAdornerWindow 装飾ウィンドウ座標に平行移動された Control の位置を返します。
パブリック メソッド Dispose BehaviorService によって使用されているすべてのリソースを解放します。
パブリック メソッド Equals  オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 (Object から継承されます。)
パブリック メソッド GetHashCode  特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 (Object から継承されます。)
パブリック メソッド GetNextBehavior 動作スタック内の指定された Behavior の直後にある Behavior を返します。
パブリック メソッド GetType  現在のインスタンスの Type を取得します。 (Object から継承されます。)
パブリック メソッド Invalidate オーバーロードされます。 BehaviorService の装飾ウィンドウを無効にします。
パブリック メソッド MapAdornerWindowPoint ハンドルの座標システムの位置を装飾ウィンドウ座標に変換します。
パブリック メソッド PopBehavior スタックの先頭にある Behavior を削除し、返します。
パブリック メソッド PushBehavior 動作スタックに Behavior をプッシュします。
パブリック メソッド PushCaptureBehavior 動作スタックに Behavior をプッシュし、動作に対してマウスのキャプチャを割り当てます。
パブリック メソッド ReferenceEquals  指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。)
パブリック メソッド ScreenToAdornerWindow 画面座標の位置が BehaviorService の装飾ウィンドウ座標に変換されます。
パブリック メソッド SyncSelection すべての選択グリフを同期させます。
パブリック メソッド ToString  現在の Object を表す String を返します。 (Object から継承されます。)
プロテクト メソッドプロテクト メソッド
パブリック イベントパブリック イベント
参照参照

関連項目

BehaviorService クラス
System.Windows.Forms.Design.Behavior 名前空間
Behavior クラス
Glyph
Adorner クラス

その他の技術情報

方法 : デザイン モードでコントロールの外観と動作を拡張する
動作サービスの概要



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「BehaviorService」の関連用語

BehaviorServiceのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



BehaviorServiceのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS