protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
// If the client sets the Image property, the selection behavior
// remains unchanged, but the RadioButton is not displayed and the
// selection is indicated only by the selection rectangle.
if (Image != null) return;
// Determine the correct state of the RadioButton.
RadioButtonState buttonState = RadioButtonState.UncheckedNormal;
if (Enabled)
{
if (mouseDownState)
{
if (Checked) buttonState = RadioButtonState.CheckedPressed;
else buttonState = RadioButtonState.UncheckedPressed;
}
else if (mouseHoverState)
{
if (Checked) buttonState = RadioButtonState.CheckedHot;
else buttonState = RadioButtonState.UncheckedHot;
}
else
{
if (Checked) buttonState = RadioButtonState.CheckedNormal;
}
}
else
{
if (Checked) buttonState = RadioButtonState.CheckedDisabled;
else buttonState = RadioButtonState.UncheckedDisabled;
}
// Calculate the position at which to display the RadioButton.
Int32 offset = (ContentRectangle.Height -
RadioButtonRenderer.GetGlyphSize(
e.Graphics, buttonState).Height) / 2;
Point imageLocation = new Point(
ContentRectangle.Location.X + 4,
ContentRectangle.Location.Y + offset);
// If the item is selected and the RadioButton paints with partial
// transparency, such as when theming is enabled, the check mark
// shows through the RadioButton image. In this case, paint a
// non-transparent background first to cover the check mark.
if (Checked && RadioButtonRenderer
.IsBackgroundPartiallyTransparent(buttonState))
{
Size glyphSize = RadioButtonRenderer
.GetGlyphSize(e.Graphics, buttonState);
glyphSize.Height--;
glyphSize.Width--;
Rectangle backgroundRectangle =
new Rectangle(imageLocation, glyphSize);
e.Graphics.FillEllipse(
SystemBrushes.Control, backgroundRectangle);
}
RadioButtonRenderer.DrawRadioButton(
e.Graphics, imageLocation, buttonState);
}