Freigeben über


How to: Use a Control Rendering Class

This example demonstrates how to use the ComboBoxRenderer class to render the drop-down arrow of a combo box control. The example consists of the OnPaint method of a simple custom control. The System.Windows.Forms.ComboBoxRenderer.IsSupported property is used to determine whether visual styles are enabled in the client area of application windows. If visual styles are active, then the System.Windows.Forms.ComboBoxRenderer.DrawDropDownButton(System.Drawing.Graphics,System.Drawing.Rectangle,System.Windows.Forms.VisualStyles.ComboBoxState) method will render the drop-down arrow with visual styles; otherwise, the System.Windows.Forms.ControlPaint.DrawComboButton method will render the drop-down arrow in the classic Windows style.

Example

' 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);
        }
    }

Compiling the Code

This example requires:

See Also

Concepts

Rendering Controls with Visual Styles