BehaviorService Klasa
Definicja
Ważne
Niektóre informacje odnoszą się do produktu w wersji wstępnej, który może zostać znacząco zmodyfikowany przed wydaniem. Firma Microsoft nie udziela żadnych gwarancji, jawnych lub domniemanych, w odniesieniu do informacji podanych w tym miejscu.
Zarządza interfejsem użytkownika w projektancie. Klasa ta nie może być dziedziczona.
public ref class BehaviorService sealed : IDisposable
public sealed class BehaviorService : IDisposable
type BehaviorService = class
interface IDisposable
Public NotInheritable Class BehaviorService
Implements IDisposable
- Dziedziczenie
-
BehaviorService
- Implementuje
Przykłady
W poniższym przykładzie kodu pokazano, jak utworzyć własną Behavior klasę opartą na kliknięciach użytkownika.
#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.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
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
Uwagi
Po utworzeniu BehaviorService elementu dodaje przezroczyste okno nad ramką projektanta. Następnie BehaviorService można użyć tego okna, aby renderować elementy interfejsu użytkownika, nazywane Glyph obiektami, a także przechwytywać wszystkie komunikaty myszy. W ten sposób BehaviorService może kontrolować zachowanie projektanta.
Klasa BehaviorService obsługuje stos zachowań, na który Behavior można wypchnąć obiekty. Po przechwyceniu komunikatu za pośrednictwem okna BehaviorService przezroczystego komunikat może wysłać wiadomość do Behavior górnej części stosu. Umożliwia to korzystanie z różnych trybów interfejsu użytkownika w zależności od aktualnie wypychanego Behavior. Służy BehaviorService do renderowania wszystkich Glyph obiektów, takich jak obramowania zaznaczenia, uchwyty rozmiaru i tagi inteligentne. Kontrolka BehaviorService steruje również wieloma zachowaniami czasu projektowania, takimi jak używanie linii przyciągania, przeciąganie i wybieranie.
Aby uzyskać więcej informacji, zobacz Omówienie usługi zachowania.
Właściwości
Adorners |
Pobiera klasę BehaviorServiceAdornerCollection. |
AdornerWindowGraphics |
Graphics Pobiera okno dla adoratora. |
CurrentBehavior |
Behavior Pobiera element w górnej części stosu zachowania bez usuwania go. |
Metody
AdornerWindowPointToScreen(Point) |
Tłumaczy element Point w oknie adornera na współrzędne ekranu. |
AdornerWindowToScreen() |
Pobiera lokalizację okna adoratora we współrzędnych ekranu. |
ControlRectInAdornerWindow(Control) | |
ControlToAdornerWindow(Control) |
Zwraca lokalizację przetłumaczonej Control na współrzędnych okna adoratora. |
Dispose() |
Zwalnia wszelkie zasoby używane przez element BehaviorService. |
Equals(Object) |
Określa, czy dany obiekt jest taki sam, jak bieżący obiekt. (Odziedziczone po Object) |
GetHashCode() |
Służy jako domyślna funkcja skrótu. (Odziedziczone po Object) |
GetNextBehavior(Behavior) |
Zwraca wartość Behavior bezpośrednio po danym Behavior w stosie zachowania. |
GetType() |
Type Pobiera bieżące wystąpienie. (Odziedziczone po Object) |
Invalidate() |
Unieważnia okno modułu adoratora elementu BehaviorService. |
Invalidate(Rectangle) |
Unieważnia w oknie modułu adoratora określony obszar BehaviorService. |
Invalidate(Region) |
Unieważnia w oknie modułu adoratora określony obszar BehaviorService. |
MapAdornerWindowPoint(IntPtr, Point) |
Konwertuje punkt w układzie współrzędnych uchwytu na współrzędne okna adoratora. |
MemberwiseClone() |
Tworzy płytkią kopię bieżącego Objectelementu . (Odziedziczone po Object) |
PopBehavior(Behavior) |
Usuwa i zwraca wartość Behavior w górnej części stosu. |
PushBehavior(Behavior) |
Wypycha element Behavior do stosu zachowania. |
PushCaptureBehavior(Behavior) |
Wypycha element Behavior na stos zachowania i przypisuje przechwytywanie myszy do zachowania. |
ScreenToAdornerWindow(Point) |
Tłumaczy punkt w współrzędnych ekranu do współrzędnych okna adoratora .BehaviorService |
SyncSelection() |
Synchronizuje wszystkie symbole wyboru. |
ToString() |
Zwraca ciąg reprezentujący bieżący obiekt. (Odziedziczone po Object) |
Zdarzenia
BeginDrag |
Występuje podczas uruchamiania BehaviorService operacji przeciągania i upuszczania. |
EndDrag |
Występuje po zakończeniu BehaviorService operacji przeciągania. |
Synchronize |
Występuje po odświeżeniu bieżącego zaznaczenia. |