编辑
2025-10-10
C#
00

简介

在工业控制、监控系统等应用中,指示灯是一个常用的可视化控件。本文将详细介绍如何使用C#和GDI+开发一个功能丰富的指示灯自定义控件。这个控件将支持多种颜色、闪烁效果、3D效果等特性。

控件特性

  • 支持多种颜色设置
  • 可配置的闪烁效果
  • 3D立体效果
  • 可自定义大小
  • 支持不同的形状(圆形、方形)
  • 鼠标悬停效果
  • 支持禁用状态显示
  • 可配置的边框样式

3. 完整代码实现

image.png

编辑
2025-10-10
C#
00

简介

异型窗体是指非矩形的窗体,可以是圆形、多边形或任意不规则形状。在C#中,我们可以通过GDI+结合Windows Forms来实现各种异型窗体效果。本文将详细介绍异型窗体的实现方法和相关技巧。

基础知识

GDI+ 关键类

C#
using System.Drawing; using System.Drawing.Drawing2D;

主要用到的类:

  • Graphics: 用于绘制图形
  • GraphicsPath: 定义复杂路径
  • Region: 定义窗体形状区域

窗体透明化

要创建异型窗体,首先需要设置窗体的特定属性:

C#
public partial class CustomForm : Form { public CustomForm() { InitializeComponent(); // 设置窗体样式 this.FormBorderStyle = FormBorderStyle.None; // 无边框 this.BackColor = Color.White; // 背景色 this.TransparencyKey = Color.White; // 透明色 } }

实现异型窗体的方法

1. 圆形窗体

C#
public class CircleForm : Form { public CircleForm() { InitializeComponent(); this.FormBorderStyle = FormBorderStyle.None; // 创建圆形路径 GraphicsPath path = new GraphicsPath(); path.AddEllipse(0, 0, this.Width, this.Height); // 设置窗体区域 this.Region = new Region(path); // 添加鼠标拖动支持 this.MouseDown += Form_MouseDown; this.MouseMove += Form_MouseMove; } private Point lastPoint; private void Form_MouseDown(object sender, MouseEventArgs e) { lastPoint = e.Location; } private void Form_MouseMove(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { this.Left += e.X - lastPoint.X; this.Top += e.Y - lastPoint.Y; } } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); // 使用抗锯齿 e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; // 绘制边框 using (Pen pen = new Pen(Color.Blue, 2)) { e.Graphics.DrawEllipse(pen, 0, 0, this.Width - 1, this.Height - 1); } } }

image.png

编辑
2025-10-10
C#
00

SSH.NET 是一个功能丰富的 .NET SSH 客户端库,它提供了一个简单而强大的 API 来执行各种 SSH 操作。本文将详细介绍 SSH.NET 的使用方法,并提供多个实用的例子。

安装

首先,通过 NuGet 包管理器安装 SSH.NET

C#
Install-Package SSH.NET

image.png

基本用法

连接到 SSH 服务器

C#
using Renci.SshNet; using System; class Program { static void Main(string[] args) { string host = "127.0.0.1"; string username = "rick"; string password = "123456"; using (var client = new SshClient(host, username, password)) { try { client.Connect(); Console.WriteLine("Connected to the SSH server!"); // 执行其他操作... client.Disconnect(); Console.WriteLine("Disconnected from the SSH server."); } catch (Exception ex) { Console.WriteLine($"An error occurred: {ex.Message}"); } } } }

image.png

编辑
2025-10-09
C#
00

随着.NET技术的不断发展,越来越多的开发者正在将应用程序从传统的WinForm迁移到更现代化的WPF(Windows Presentation Foundation)框架。这种转变不仅是技术栈的更新,更是UI开发理念的革新。本文将通过TextBox控件这一常用元素,深入剖析WinForm到WPF的转型过程,为正在或即将进行技术迁移的开发者提供实用指南。

为什么从WinForm迁移到WPF?

界面设计的革命性变化

WPF采用XAML(可扩展应用标记语言)来描述用户界面,这种声明式的UI设计方式为开发者提供了前所未有的灵活性:

C#
<!-- WPF中使用XAML定义UI --> <Grid> <TextBox x:Name="txtInput" Width="200" Height="30" /> </Grid>

相比之下,WinForm的界面设计主要依赖于设计器生成的代码:

C#
// WinForm中通过代码定义UI this.txtInput = new System.Windows.Forms.TextBox(); this.txtInput.Location = new System.Drawing.Point(12, 12); this.txtInput.Size = new System.Drawing.Size(200, 20); this.Controls.Add(this.txtInput);

数据绑定的强大优势

WPF的数据绑定机制远超WinForm,可以轻松实现UI与数据的自动同步:

C#
<!-- XAML中的数据绑定 --> <TextBox Text="{Binding UserName, UpdateSourceTrigger=PropertyChanged}" />

更现代的图形渲染

WPF基于DirectX,提供硬件加速的2D和3D图形渲染,界面表现力大幅提升。

TextBox控件:从WinForm到WPF的具体对比

基础创建与属性设置

WinForm中的TextBox

C#
// WinForm中创建TextBox TextBox txtName = new TextBox(); txtName.Location = new Point(20, 20); txtName.Size = new Size(200, 20); txtName.Text = "请输入姓名"; txtName.Font = new Font("微软雅黑", 10F); this.Controls.Add(txtName);

WPF中的TextBox

XML
<!-- WPF中创建TextBox --> <TextBox x:Name="txtName" Width="200" Height="30" Margin="20" Text="请输入姓名" FontFamily="微软雅黑" FontSize="12" />

image.png

编辑
2025-10-09
C#
00

在传统WinForm开发中,Label是非常常见的控件之一,用于显示文本内容、提示信息等。WPF中同样提供了Label控件,但它功能更加强大,支持更多样式和布局。本篇文章介绍如何将WinForm中的Label用法迁移到WPF环境下,并带来一些常用示例和完整代码示例示范。

WinForm中的Label

在WinForm中,Label的使用相对简单。我们只需要将Label控件拖到窗体上,或者动态在C#代码中实例化:

C#
// 通过设计器或代码在Form上放置Label Label label1 = new Label(); label1.Text = "这是一个WinForm Label"; label1.AutoSize = true; // 设置位置 label1.Location = new Point(10, 10); // 设置字体 label1.Font = new Font("Microsoft YaHei", 12); // 将label添加到Form控件集合 this.Controls.Add(label1);

使用时需要注意布局和位置。在WinForm中,控件的样式大部分依赖系统外观,以及通过手动设置FontForeColorBackColor等属性来完成。