2025-10-13
C#
00

项目概述

在本教程中,我们将使用 Windows Forms 开发一个功能完整的截图应用程序。这个应用程序将支持以下核心功能:

  • 全屏截图
  • 区域选择截图
  • 保存截图到指定位置
  • 复制截图到剪贴板

项目准备

创建 Windows Forms 应用程序

  1. 打开 Visual Studio
  2. 创建新项目 -> Windows Forms 应用程序
  3. 项目名称:ScreenshotApp

核心代码实现

2025-10-13
C#
00

引言

在现代软件开发中,解耦是构建可维护、可扩展系统的关键。事件驱动架构(Event-Driven Architecture,简称 EDA)为我们提供了一种强大的解耦方案,允许组件之间通过事件进行松散耦合的通信。本文将深入探讨如何在 C# 中利用事件驱动架构实现应用解耦。

事件驱动架构的核心概念

事件驱动架构基于以下关键概念:

  • 事件发布者(Publisher):产生事件的组件
  • 事件订阅者(Subscriber):对特定事件感兴趣并进行处理的组件
  • 事件总线(Event Bus):负责事件的分发和路由

实践示例:电子商务订单系统

我们将通过一个电子商务订单系统的例子,展示事件驱动架构的实际应用。

订单类

C#
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AppEvent { public class Order { public string Id { get; set; } public string CustomerId { get; set; } public decimal TotalAmount { get; set; } public DateTime CreatedAt { get; set; } = DateTime.UtcNow; // 可以根据需要添加其他属性,例如订单状态、商品列表等 } }
2025-10-13
C#
00

快捷键注册概述

快捷键注册是一种在 Windows 应用程序中允许全局热键捕获的技术。通过正确注册快捷键,开发者可以让应用程序在任何情况下都能响应特定按键组合。

实现快捷键注册的关键 API

在 C# 中,我们主要使用 Windows API 中的以下方法来实现快捷键注册:

  • RegisterHotKey(): 注册全局热键
  • UnregisterHotKey(): 取消注册热键
  • WndProc(): 处理消息循环中的快捷键消息

完整代码示例

C#
using System.Runtime.InteropServices; namespace AppHotKey { public partial class Form1 : Form { // 定义快捷键消息常量 private const int WM_HOTKEY = 0x0312; // 导入 Windows API 函数 [DllImport("user32.dll")] public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk); [DllImport("user32.dll")] public static extern bool UnregisterHotKey(IntPtr hWnd, int id); // 快捷键修饰符枚举 [Flags] public enum KeyModifiers { None = 0, Alt = 1, Ctrl = 2, Shift = 4, Windows = 8 } // 快捷键 ID private const int HOTKEY_ID = 1; public Form1() { InitializeComponent(); // 注册快捷键:Ctrl + Shift + A RegisterHotKey( this.Handle, HOTKEY_ID, (uint)(KeyModifiers.Ctrl | KeyModifiers.Shift), (uint)Keys.A ); } // 重写消息处理方法 protected override void WndProc(ref Message m) { // 检查是否为快捷键消息 if (m.Msg == WM_HOTKEY) { // 获取快捷键 ID int id = m.WParam.ToInt32(); if (id == HOTKEY_ID) { // 快捷键触发时的处理逻辑 HandleHotkeyTriggered(); } } base.WndProc(ref m); } // 快捷键触发处理方法 private void HandleHotkeyTriggered() { MessageBox.Show("快捷键 Ctrl + Shift + A 被按下!"); // 在这里添加您想要执行的具体操作 } // 程序关闭时取消注册快捷键 protected override void OnFormClosing(FormClosingEventArgs e) { UnregisterHotKey(this.Handle, HOTKEY_ID); base.OnFormClosing(e); } } }

image.png

2025-10-13
C#
00

什么是环境变量?

环境变量是操作系统中存储的动态值,可以被应用程序和系统进程使用。在C#中,我们可以通过多种方式读取、设置和管理环境变量。

读取环境变量

获取系统环境变量

C#
using System.Collections; namespace AppEnvironment { internal class Program { static void Main(string[] args) { // 获取特定的系统环境变量 string systemPath = Environment.GetEnvironmentVariable("PATH"); Console.WriteLine("系统PATH:" + systemPath); // 获取所有环境变量 foreach (DictionaryEntry env in Environment.GetEnvironmentVariables()) { Console.WriteLine($"{env.Key} = {env.Value}"); } } } }

image.png

2025-10-13
C#
00

Windows 通知是与用户进行交互的重要方式。通过 Microsoft.Toolkit.Uwp.Notifications,我们可以轻松实现丰富的通知功能,包括基本通知、图片通知、交互式通知等。

环境准备

NuGet 包安装

PowerShell
Install-Package Microsoft.Toolkit.Uwp.Notifications

必要的 using 语句

C#
using Microsoft.Toolkit.Uwp.Notifications; using Windows.UI.Notifications; using Windows.Data.Xml.Dom;

项目中对版本要求,一定是Windows10以上版本。

image.png

基础实现

首先,我们创建一个通知辅助类来封装常用的通知功能:

C#
public class NotificationHelper { /// <summary> /// 发送基本通知 /// </summary> /// <param name="title">通知标题</param> /// <param name="content">通知内容</param> public static void SendBasicNotification(string title, string content) { var builder = new ToastContentBuilder() .AddText(title) .AddText(content); builder.Show(); } }

通知类型

基本文本通知

C#
public static void SendBasicNotification(string title, string content) { var builder = new ToastContentBuilder() .AddText(title) .AddText(content); builder.Show(); }

image.png