Comment : utiliser une classe de rendu des contrôles

Mise à jour : novembre 2007

Cet exemple montre comment utiliser la classe ComboBoxRenderer pour restituer la flèche de déroulement d'un contrôle zone de liste déroulante. L'exemple se compose de la méthode OnPaint d'un contrôle personnalisé simple. La propriété ComboBoxRenderer.IsSupported est utilisée pour déterminer si les styles visuels sont activés dans la zone cliente de fenêtres d'application. Si les styles visuels sont actifs, la méthode ComboBoxRenderer.DrawDropDownButton restituera alors la flèche de déroulement avec les styles visuels ; sinon, la méthode ControlPaint.DrawComboButton restituera la flèche de déroulement dans le style Windows classique.

Exemple

' Render the drop-down arrow with or without visual styles.
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
    MyBase.OnPaint(e)

    If Not ComboBoxRenderer.IsSupported Then
        ControlPaint.DrawComboButton(e.Graphics, _
            Me.ClientRectangle, ButtonState.Normal)
    Else
        ComboBoxRenderer.DrawDropDownButton(e.Graphics, _
            Me.ClientRectangle, ComboBoxState.Normal)
    End If
End Sub
// Render the drop-down arrow with or without visual styles.
protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);

    if (!ComboBoxRenderer.IsSupported)
    {
        ControlPaint.DrawComboButton(e.Graphics,
            this.ClientRectangle, ButtonState.Normal);
    }
    else
    {
        ComboBoxRenderer.DrawDropDownButton(e.Graphics,
            this.ClientRectangle, ComboBoxState.Normal);
    }
}
    // Render the drop-down arrow with or without visual styles.
protected:
    virtual void OnPaint(PaintEventArgs^ e) override
    {
        __super::OnPaint(e);

        if (!ComboBoxRenderer::IsSupported)
        {
            ControlPaint::DrawComboButton(e->Graphics,
                this->ClientRectangle, ButtonState::Normal);
        }
        else
        {
            ComboBoxRenderer::DrawDropDownButton(e->Graphics,
                this->ClientRectangle, ComboBoxState::Normal);
        }
    }

Compilation du code

Cet exemple nécessite les éléments suivants :

Voir aussi

Concepts

Rendu des contrôles avec les styles visuels