Behavior Klass
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Representerar de Behavior objekt som hanteras av en BehaviorService.
public ref class Behavior abstract
public abstract class Behavior
type Behavior = class
Public MustInherit Class Behavior
- Arv
-
Behavior
Exempel
I följande kodexempel visas hur du skapar en egen klass baserat på klassen Behavior som svarar på användarklick. Det här kodexemplet är en del av ett större exempel för BehaviorService klassen.
// 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);
}
};
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.
}
}
}
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
Kommentarer
Den här klassen kan utökas för att utveckla alla typer av användargränssnittsbeteenden, inklusive val, dra och ändra storlek på beteenden.
Mer information finns i Översikt över beteendetjänsten.
Konstruktorer
| Name | Description |
|---|---|
| Behavior() |
Initierar en ny instans av Behavior klassen. |
| Behavior(Boolean, BehaviorService) |
Initierar en ny instans av Behavior klassen med angiven BehaviorService. |
Egenskaper
| Name | Description |
|---|---|
| Cursor |
Hämtar markören som ska visas för det här beteendet. |
| DisableAllCommands |
Hämtar ett värde som anger om MenuCommand objekt ska inaktiveras. |
Metoder
| Name | Description |
|---|---|
| Equals(Object) |
Avgör om det angivna objektet är lika med det aktuella objektet. (Ärvd från Object) |
| FindCommand(CommandID) |
Fångar upp kommandon. |
| GetHashCode() |
Fungerar som standard-hash-funktion. (Ärvd från Object) |
| GetType() |
Hämtar den aktuella instansen Type . (Ärvd från Object) |
| MemberwiseClone() |
Skapar en ytlig kopia av den aktuella Object. (Ärvd från Object) |
| OnDragDrop(Glyph, DragEventArgs) |
Tillåter anpassat drag-och-släpp-beteende. |
| OnDragEnter(Glyph, DragEventArgs) |
Tillåter anpassat drag-enter-beteende. |
| OnDragLeave(Glyph, EventArgs) |
Tillåter anpassat drag-leave-beteende. |
| OnDragOver(Glyph, DragEventArgs) |
Tillåter anpassat drag-over-beteende. |
| OnGiveFeedback(Glyph, GiveFeedbackEventArgs) |
Tillåter anpassat feedbackbeteende för dra och släpp. |
| OnLoseCapture(Glyph, EventArgs) |
Anropas av utsmyckningsfönstret när det förlorar musfångst. |
| OnMouseDoubleClick(Glyph, MouseButtons, Point) |
Anropas när ett dubbelklicksmeddelande anger utsmyckningsfönstret i BehaviorService. |
| OnMouseDown(Glyph, MouseButtons, Point) |
Anropas när ett musknappsmeddelande kommer in i utsmyckningsfönstret i BehaviorService. |
| OnMouseEnter(Glyph) |
Anropas när ett mus-returmeddelande kommer in i utsmyckningsfönstret i BehaviorService. |
| OnMouseHover(Glyph, Point) |
Anropas när ett mus-hovringsmeddelande kommer in i utsmyckningsfönstret i BehaviorService. |
| OnMouseLeave(Glyph) |
Anropas när ett mus-leave-meddelande kommer in i utsmyckningsfönstret i BehaviorService. |
| OnMouseMove(Glyph, MouseButtons, Point) |
Anropas när ett musflyttningsmeddelande kommer in i utsmyckningsfönstret i BehaviorService. |
| OnMouseUp(Glyph, MouseButtons) |
Anropas när ett mus-up-meddelande kommer in i utsmyckningsfönstret i BehaviorService. |
| OnQueryContinueDrag(Glyph, QueryContinueDragEventArgs) |
Skickar den här dra och släpp-händelsen från utsmyckningsfönstret till lämplig Behavior eller träfftestad Glyph. |
| ToString() |
Returnerar en sträng som representerar det aktuella objektet. (Ärvd från Object) |