radiobutton 带小圆点_RadioButtonRenderer

RadioButtonRenderer 类

定义

程序集:System.Windows.Forms.dll

提供用于呈现带/不带视觉样式的选项按钮控件(也称为单选按钮)的方法。Provides methods used to render an option button control (also known as a radio button) with or without visual styles. 此类不能被继承。This class cannot be inherited.

本文内容

public ref class RadioButtonRenderer abstract sealed

public ref class RadioButtonRenderer sealed

public static class RadioButtonRenderer

public sealed class RadioButtonRenderer

type RadioButtonRenderer = class

Public Class RadioButtonRenderer

Public NotInheritable Class RadioButtonRenderer

继承

RadioButtonRenderer

示例

下面的代码示例演示如何编写一个自定义控件,该控件使用 DrawRadioButton 方法绘制一个用于响应鼠标单击的选项按钮。The following code example demonstrates how to write a custom control that uses the DrawRadioButton method to draw an option button that responds to mouse clicks.

#using

#using

#using

using namespace System;

using namespace System::Drawing;

using namespace System::Windows::Forms;

using namespace System::Windows::Forms::VisualStyles;

namespace RadioButtonRendererSample

{

public ref class CustomRadioButton : public Control

{

private:

Rectangle textRectangleValue;

private:

bool clicked;

private:

RadioButtonState state;

public:

CustomRadioButton() : Control()

{

textRectangleValue = Rectangle();

state = RadioButtonState::UncheckedNormal;

this->Location = Point(50, 50);

this->Size = System::Drawing::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:

property Rectangle TextRectangle

{

Rectangle get()

{

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;

delete g;

return textRectangleValue;

}

}

// Draw the radio button in the current state.

protected:

virtual void OnPaint(PaintEventArgs^ e) override

{

__super::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:

virtual void OnMouseDown(MouseEventArgs^ e) override

{

__super::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:

virtual void OnMouseHover(EventArgs^ e) override

{

__super::OnMouseHover(e);

state = clicked ? RadioButtonState::CheckedHot :

RadioButtonState::UncheckedHot;

Invalidate();

}

// Draw the radio button in the hot state.

protected:

virtual void OnMouseUp(MouseEventArgs^ e) override

{

__super::OnMouseUp(e);

this->OnMouseHover(e);

}

// Draw the radio button in the normal (i.e. not hot) state

protected:

virtual void OnMouseLeave(EventArgs^ e) override

{

__super::OnMouseLeave(e);

state = clicked ? RadioButtonState::CheckedNormal :

RadioButtonState::UncheckedNormal;

Invalidate();

}

};

public ref class Form1 : public Form

{

public:

Form1() : Form()

{

Controls->Add(gcnew CustomRadioButton());

if (Application::RenderWithVisualStyles)

{

this->Text = "Visual Styles Enabled";

}

else

{

this->Text = "Visual Styles Disabled";

}

}

};

}

[STAThread]

int 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(gcnew RadioButtonRendererSample::Form1());

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

}

}

}Imports System.Drawing

Imports System.Windows.Forms

Imports System.Windows.Forms.VisualStyles

Namespace RadioButtonRendererSample

Class Form1

Inherits Form

Dim WithEvents button1 As Button

Public Sub New()

Dim RadioButton1 As New CustomRadioButton()

button1 = New Button

Me.button1.Location = New System.Drawing.Point(185, 231)

Me.button1.Size = New System.Drawing.Size(105, 23)

Me.button1.Text = "Toggle Styles"

Controls.Add(RadioButton1)

Controls.Add(button1)

If Application.RenderWithVisualStyles Then

Me.Text = "Visual Styles Enabled"

Else

Me.Text = "Visual Styles Disabled"

End If

End Sub

_

Shared Sub 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())

End Sub

' Match application style and toggle visual styles off

' and on for the application.

Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) _

Handles button1.Click

RadioButtonRenderer.RenderMatchingApplicationState = True

Application.VisualStyleState = _

Application.VisualStyleState Xor _

VisualStyleState.ClientAndNonClientAreasEnabled

If Application.RenderWithVisualStyles Then

Me.Text = "Visual Styles Enabled"

Else

Me.Text = "Visual Styles Disabled"

End If

End Sub

End Class

Public Class CustomRadioButton

Inherits Control

Private textRectangleValue As New Rectangle()

Private clicked As Boolean = False

Private state As RadioButtonState = RadioButtonState.UncheckedNormal

Public Sub New()

With Me

.Location = New Point(50, 50)

.Size = New Size(100, 20)

.Text = "Click here"

.Font = SystemFonts.IconTitleFont

End With

End Sub

' Define the text bounds so that the text rectangle

' does not include the radio button.

Public ReadOnly Property TextRectangle() As Rectangle

Get

Using g As Graphics = Me.CreateGraphics()

With textRectangleValue

.X = Me.ClientRectangle.X + _

RadioButtonRenderer.GetGlyphSize(g, _

RadioButtonState.UncheckedNormal).Width

.Y = Me.ClientRectangle.Y

.Width = Me.ClientRectangle.Width - _

RadioButtonRenderer.GetGlyphSize(g, _

RadioButtonState.UncheckedNormal).Width

.Height = Me.ClientRectangle.Height

End With

End Using

Return textRectangleValue

End Get

End Property

' Draw the radio button in the current state.

Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)

MyBase.OnPaint(e)

RadioButtonRenderer.DrawRadioButton(e.Graphics, _

Me.ClientRectangle.Location, TextRectangle, Me.Text, _

Me.Font, clicked, state)

End Sub

' Draw the radio button in the checked or unchecked state.

Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)

MyBase.OnMouseDown(e)

If Not clicked Then

clicked = True

Me.Text = "Clicked!"

state = RadioButtonState.CheckedPressed

Invalidate()

Else

clicked = False

Me.Text = "Click here"

state = RadioButtonState.UncheckedNormal

Invalidate()

End If

End Sub

' Draw the radio button in the hot state.

Protected Overrides Sub OnMouseHover(ByVal e As EventArgs)

MyBase.OnMouseHover(e)

If clicked Then

state = RadioButtonState.CheckedHot

Else

state = RadioButtonState.UncheckedHot

End If

Invalidate()

End Sub

' Draw the radio button in the hot state.

Protected Overrides Sub OnMouseUp(ByVal e As MouseEventArgs)

MyBase.OnMouseUp(e)

Me.OnMouseHover(e)

End Sub

' Draw the radio button in the unpressed state.

Protected Overrides Sub OnMouseLeave(ByVal e As EventArgs)

MyBase.OnMouseLeave(e)

If clicked Then

state = RadioButtonState.CheckedNormal

Else

state = RadioButtonState.UncheckedNormal

End If

Invalidate()

End Sub

End Class

End Namespace

注解

RadioButtonRenderer类提供一组 static 可用于呈现选项按钮控件的方法。The RadioButtonRenderer class provides a set of static methods that can be used to render an option button control. 呈现控件是指绘制控件的用户界面。Rendering a control refers to drawing the user interface of a control. 若要绘制选项按钮,请使用其中一种 DrawRadioButton 方法。To draw an option button, use one of the DrawRadioButton methods. 这些方法提供了各种选项,如使用选项按钮绘制文本或图像。These methods provide a variety of options, such as drawing text or an image with the option button.

如果在操作系统中启用了视觉样式,并将视觉样式应用于当前应用程序, DrawRadioButton 将绘制具有当前视觉样式的选项按钮。If visual styles are enabled in the operating system and visual styles are applied to the current application, DrawRadioButton will draw the option button with the current visual style. 否则, DrawRadioButton 将绘制带有经典 Windows 样式的选项按钮。Otherwise, DrawRadioButton will draw the option button with the classic Windows style. 如果要绘制的自定义控件应自动匹配操作系统的当前视觉样式设置,这将很有用。This is useful if you are drawing a custom control that should automatically match the current visual style setting of the operating system.

属性

获取或设置一个值,该值指示呈现器是否使用应用程序状态来确定呈现样式。Gets or sets a value indicating whether the renderer uses the application state to determine rendering style.

方法

在指定区域中绘制控件的父级的背景。Draws the background of a control's parent in the specified area.

在指定状态下和指定位置绘制选项按钮控件(也称为单选按钮)。Draws an option button control (also known as a radio button) in the specified state and location.

在指定状态下和指定位置,用指定文本和可选的聚焦框绘制选项按钮控件(也称为单选按钮)。Draws an option button control (also known as a radio button) in the specified state and location, with the specified text, and with an optional focus rectangle.

在指定状态下和指定位置,用指定文本和图像以及可选的聚焦框,绘制选项按钮控件(也称为单选按钮)。Draws an option button control (also known as a radio button) in the specified state and location, with the specified text and image, and with an optional focus rectangle.

在指定状态下和指定位置,用指定文本和文本格式以及可选的聚焦框,绘制选项按钮控件(也称为单选按钮)。Draws an option button control (also known as a radio button) in the specified state and location, with the specified text and text formatting, and with an optional focus rectangle.

在指定状态下和指定位置,用指定文本、文本格式、图像和可选的聚焦框,绘制选项按钮控件(也称为单选按钮)。Draws an option button control (also known as a radio button) in the specified state and location; with the specified text, text formatting, and image; and with an optional focus rectangle.

返回选项按钮(也称为单选按钮)标志符号的大小(以像素为单位)。Returns the size, in pixels, of the option button (also known as a radio button) glyph.

指示选项按钮(也称为单选按钮)的背景是否有半透明或 alpha 混合块。Indicates whether the background of the option button (also known as a radio button) has semitransparent or alpha-blended pieces.

适用于

另请参阅

可以在 `print_resolution()` 函数中添加设置分辨率的代码,使得选择不同的分辨率后,可以动态调整画面分辨率。 具体实现方法如下: 1. 在 `print_resolution()` 函数中添加以下代码,用于根据选择的分辨率设置画面分辨率: ```python if Resolution == '0': # 设置画面分辨率为 640x480 video.set(cv2.CAP_PROP_FRAME_WIDTH, 640) video.set(cv2.CAP_PROP_FRAME_HEIGHT, 480) elif Resolution == '1': # 设置画面分辨率为 800x600 video.set(cv2.CAP_PROP_FRAME_WIDTH, 800) video.set(cv2.CAP_PROP_FRAME_HEIGHT, 600) elif Resolution == '2': # 设置画面分辨率为 1024x768 video.set(cv2.CAP_PROP_FRAME_WIDTH, 1024) video.set(cv2.CAP_PROP_FRAME_HEIGHT, 768) elif Resolution == '3': # 设置画面分辨率为 1280x720 video.set(cv2.CAP_PROP_FRAME_WIDTH, 1280) video.set(cv2.CAP_PROP_FRAME_HEIGHT, 720) elif Resolution == '4': # 设置画面分辨率为 1920x1080 video.set(cv2.CAP_PROP_FRAME_WIDTH, 1920) video.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080) ``` 2. 在 `video_connect_option()` 函数中添加以下代码,用于初始化默认的分辨率: ```python Resolution = '0' ``` 完整的代码如下: ```python # 视频连接参数面板 def video_connect_option(): global Resolution, ShowMe, Version, AudioOpen # 初始化分辨率为 640x480 Resolution = '0' video_connect_option = tkinter.Toplevel() video_connect_option.wm_geometry('150x450') video_connect_option.title('连接参数') # 分辨率面板 var1 = tkinter.StringVar() label1 = tkinter.Label(video_connect_option, bg='#f0f0f0', width=20, text='分辨率') label1.pack() def print_resolution(): global Resolution Resolution = var1.get() label1.config(text='分辨率 ' + Resolution) # 根据选择的分辨率设置画面分辨率 if Resolution == '0': video.set(cv2.CAP_PROP_FRAME_WIDTH, 640) video.set(cv2.CAP_PROP_FRAME_HEIGHT, 480) elif Resolution == '1': video.set(cv2.CAP_PROP_FRAME_WIDTH, 800) video.set(cv2.CAP_PROP_FRAME_HEIGHT, 600) elif Resolution == '2': video.set(cv2.CAP_PROP_FRAME_WIDTH, 1024) video.set(cv2.CAP_PROP_FRAME_HEIGHT, 768) elif Resolution == '3': video.set(cv2.CAP_PROP_FRAME_WIDTH, 1280) video.set(cv2.CAP_PROP_FRAME_HEIGHT, 720) elif Resolution == '4': video.set(cv2.CAP_PROP_FRAME_WIDTH, 1920) video.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080) r0 = tkinter.Radiobutton(video_connect_option, text='0', variable=var1, value='0', command=print_resolution) r0.pack() r1 = tkinter.Radiobutton(video_connect_option, text='1', variable=var1, value='1', command=print_resolution) r1.pack() r2 = tkinter.Radiobutton(video_connect_option, text='2', variable=var1, value='2', command=print_resolution) r2.pack() r3 = tkinter.Radiobutton(video_connect_option, text='3', variable=var1, value='3', command=print_resolution) r3.pack() r4 = tkinter.Radiobutton(video_connect_option, text='4', variable=var1, value='4', command=print_resolution) r4.pack() ``` 注意:上述代码中使用了 `video` 变量,需要在调用 `video_connect_option()` 之前打开视频流并将其赋值给 `video` 变量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值