UILight
控件在 SunnyUI 中用于模拟指示灯的效果,可以用来显示状态信息。UILight
支持不同的状态,这些状态可以通过 UILightState
枚举来表示。下面是如何使用 UILight
和 UILightState
的示例:
1. 安装 SunnyUI
首先,确保您已经安装了 SunnyUI。可以通过 NuGet 包管理器安装 SunnyUI:
Bash
深色版本
1dotnet add package Sunny.UI
2. 引用 SunnyUI 命名空间
在您的项目中引用 SunnyUI 命名空间:
Csharp
深色版本
1using Sunny.UI;
3. 创建 UILight
控件
在您的窗体上添加一个 UILight
控件。可以通过设计视图拖放控件,或者通过代码创建:
Csharp
深色版本
1public Form1()
2{
3 InitializeComponent();
4
5 // 创建 UILight 控件
6 UILight lightControl = new UILight();
7 lightControl.Location = new Point(50, 50); // 设置位置
8 lightControl.Size = new Size(50, 50); // 设置大小
9 lightControl.Color = UIColor.Green; // 设置颜色
10 lightControl.State = UILightState.On; // 设置状态
11
12 // 将控件添加到窗体
13 this.Controls.Add(lightControl);
14}
4. 使用 UILightState
UILight
控件的 State
属性可以设置为 UILightState
枚举中的一个值。UILightState
包含以下成员:
Off
:指示灯关闭。On
:指示灯开启。Blink
:指示灯闪烁。
5. 示例代码
下面是一个完整的示例代码,演示如何使用 UILight
控件和 UILightState
:
Csharp
深色版本
1using System;
2using System.Windows.Forms;
3using Sunny.UI;
4
5namespace SunnyUIExample
6{
7 public partial class Form1 : Form
8 {
9 private UILight lightControl;
10
11 public Form1()
12 {
13 InitializeComponent();
14
15 // 创建 UILight 控件
16 lightControl = new UILight();
17 lightControl.Location = new Point(50, 50); // 设置位置
18 lightControl.Size = new Size(50, 50); // 设置大小
19 lightControl.Color = UIColor.Green; // 设置颜色
20 lightControl.State = UILightState.On; // 设置状态
21
22 // 将控件添加到窗体
23 this.Controls.Add(lightControl);
24
25 // 注册事件处理器
26 lightControl.CheckedChanged += LightControl_CheckedChanged;
27 }
28
29 private void LightControl_CheckedChanged(object sender, EventArgs e)
30 {
31 if (lightControl.State == UILightState.On)
32 {
33 Console.WriteLine("Light is on.");
34 }
35 else if (lightControl.State == UILightState.Off)
36 {
37 Console.WriteLine("Light is off.");
38 }
39 else if (lightControl.State == UILightState.Blink)
40 {
41 Console.WriteLine("Light is blinking.");
42 }
43 }
44 }
45}
注意事项
- 确保您的项目是针对 .NET Framework 或 .NET Core 5+ 的,因为 SunnyUI 支持这两个平台。
- 如果您在设计视图中看不到控件,请确保您已经正确安装了 SunnyUI,并且在项目中引用了 SunnyUI 命名空间。
- 考虑为您的控件设置合适的样式和布局,以适应您的应用程序需求。