本文整理汇总了C#中System.Windows.Forms.RadioButtonRenderer类的典型用法代码示例。如果您正苦于以下问题:C# RadioButtonRenderer类的具体用法?C# RadioButtonRenderer怎么用?C# RadioButtonRenderer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
RadioButtonRenderer类属于System.Windows.Forms命名空间,在下文中一共展示了RadioButtonRenderer类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Button
//引入命名空间
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
namespace RadioButtonRendererSample
{
class Form1 : Form
{
Button button1 = new Button();
public Form1()
: base()
{
CustomRadioButton RadioButton1 = new CustomRadioButton();
button1.Location = new System.Drawing.Point(175, 231);
button1.Size = new System.Drawing.Size(105, 23);
button1.Text = "Toggle Style";
button1.Click += new System.EventHandler(this.button1_Click);
Controls.Add(RadioButton1);
Controls.Add(button1);
if (Application.RenderWithVisualStyles)
this.Text = "Visual Styles Enabled";
else
this.Text = "Visual Styles Disabled";
}
[STAThread]
static void Main()
{
// If you do not call EnableVisualStyles below, then
// RadioButtonRenderer.DrawRadioButton automatically detects
// this and draws the radio button without visual styles.
Application.EnableVisualStyles();
Application.Run(new Form1());
}
private void button1_Click(object sender, EventArgs e)
{
Application.VisualStyleState =
Application.VisualStyleState ^
VisualStyleState.ClientAndNonClientAreasEnabled;
GroupBoxRenderer.RenderMatchingApplicationState = true;
if (Application.RenderWithVisualStyles)
this.Text = "Visual Styles Enabled";
else
this.Text = "Visual Styles Disabled";
}
}
public class CustomRadioButton : Control
{
private Rectangle textRectangleValue = new Rectangle();
private bool clicked = false;
private RadioButtonState state = RadioButtonState.UncheckedNormal;
public CustomRadioButton()
: base()
{
this.Location = new Point(50, 50);
this.Size = new Size(100, 20);
this.Text = "Click here";
this.Font = SystemFonts.IconTitleFont;
}
// Define the text bounds so that the text rectangle
// does not include the radio button.
public Rectangle TextRectangle
{
get
{
using (Graphics g = this.CreateGraphics())
{
textRectangleValue.X = ClientRectangle.X +
RadioButtonRenderer.GetGlyphSize(g,
RadioButtonState.UncheckedNormal).Width;
textRectangleValue.Y = ClientRectangle.Y;
textRectangleValue.Width = ClientRectangle.Width -
RadioButtonRenderer.GetGlyphSize(g,
RadioButtonState.UncheckedNormal).Width;
textRectangleValue.Height = ClientRectangle.Height;
}
return textRectangleValue;
}
}
// Draw the radio button in the current state.
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
RadioButtonRenderer.DrawRadioButton(e.Graphics,
ClientRectangle.Location, TextRectangle, this.Text,
this.Font, clicked, state);
}
// Draw the radio button in the checked or unchecked state.
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
if (!clicked)
{
clicked = true;
this.Text = "Clicked!";
state = RadioButtonState.CheckedPressed;
Invalidate();
}
else
{
clicked = false;
this.Text = "Click here";
state = RadioButtonState.UncheckedNormal;
Invalidate();
}
}
// Draw the radio button in the hot state.
protected override void OnMouseHover(EventArgs e)
{
base.OnMouseHover(e);
state = clicked ? RadioButtonState.CheckedHot :
RadioButtonState.UncheckedHot;
Invalidate();
}
// Draw the radio button in the hot state.
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
this.OnMouseHover(e);
}
// Draw the radio button in the unpressed state.
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
state = clicked ? RadioButtonState.CheckedNormal :
RadioButtonState.UncheckedNormal;
Invalidate();
}
}
}