Behavior Classe

Définition

Représente les objets Behavior managés par un BehaviorService.

public ref class Behavior abstract
public abstract class Behavior
type Behavior = class
Public MustInherit Class Behavior
Héritage
Behavior

Exemples

L’exemple de code suivant montre comment créer votre propre classe en fonction de la Behavior classe qui répond aux clics utilisateur. Cet exemple de code fait partie d’un exemple plus grand fourni pour la BehaviorService classe.


// 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

Remarques

Cette classe peut être étendue pour développer n’importe quel type de comportement d’interface utilisateur, notamment la sélection, le glisser et le redimensionnement.

Pour plus d’informations, consultez Vue d’ensemble du service de comportement.

Notes

Votre Behavior type doit être associé à un Glyph type. Les comportements indépendants du glyphe ne sont pas pris en charge.

Constructeurs

Behavior()

Initialise une nouvelle instance de la classe Behavior.

Behavior(Boolean, BehaviorService)

Initialise une nouvelle instance de la classe Behavior avec le BehaviorService donné.

Propriétés

Cursor

Obtient le curseur qui doit être affiché pour ce comportement.

DisableAllCommands

Obtient une valeur indiquant si les objets MenuCommand doivent être désactivés.

Méthodes

Equals(Object)

Détermine si l'objet spécifié est égal à l'objet actuel.

(Hérité de Object)
FindCommand(CommandID)

Intercepte des commandes.

GetHashCode()

Fait office de fonction de hachage par défaut.

(Hérité de Object)
GetType()

Obtient le Type de l'instance actuelle.

(Hérité de Object)
MemberwiseClone()

Crée une copie superficielle du Object actuel.

(Hérité de Object)
OnDragDrop(Glyph, DragEventArgs)

Autorise un comportement de glisser-déplacer personnalisé.

OnDragEnter(Glyph, DragEventArgs)

Autorise un comportement glisser-valider personnalisé.

OnDragLeave(Glyph, EventArgs)

Autorise un comportement glisser-lâcher personnalisé.

OnDragOver(Glyph, DragEventArgs)

Autorise un comportement glisser-survoler personnalisé.

OnGiveFeedback(Glyph, GiveFeedbackEventArgs)

Autorise un comportement de commentaire de glisser-déplacer personnalisé.

OnLoseCapture(Glyph, EventArgs)

Appelé par la fenêtre de dispositif d'ornement lorsqu'il perd la capture de la souris.

OnMouseDoubleClick(Glyph, MouseButtons, Point)

Appelé lorsqu'un message de double-clic entre dans la fenêtre de dispositif d'ornement du BehaviorService.

OnMouseDown(Glyph, MouseButtons, Point)

Appelé lorsqu'un message de trajet de souris descendant entre dans la fenêtre de dispositif d'ornement du BehaviorService.

OnMouseEnter(Glyph)

Appelé lorsqu'un message de validation à la souris entre dans la fenêtre de dispositif d'ornement du BehaviorService.

OnMouseHover(Glyph, Point)

Appelé lorsqu'un message de pointage à la souris entre dans la fenêtre de dispositif d'ornement du BehaviorService.

OnMouseLeave(Glyph)

Appelé lorsqu'un message d'éloignement à la souris entre dans la fenêtre de dispositif d'ornement du BehaviorService.

OnMouseMove(Glyph, MouseButtons, Point)

Appelé lorsqu'un message de déplacement à la souris entre dans la fenêtre de dispositif d'ornement du BehaviorService.

OnMouseUp(Glyph, MouseButtons)

Appelé lorsqu'un message de trajet de souris montant entre dans la fenêtre de dispositif d'ornement du BehaviorService.

OnQueryContinueDrag(Glyph, QueryContinueDragEventArgs)

Envoie cet événement glisser-déplacer de la fenêtre de dispositif d'ornement au Behavior approprié ou au Glyph ayant fait l'objet d'un test d'accès réussi.

ToString()

Retourne une chaîne qui représente l'objet actuel.

(Hérité de Object)

S’applique à

Voir aussi