C#软件开发实例.私人订制自己的屏幕截图工具(六)添加配置管理功能

本实例全部文章目录

添加设置窗口

在解决方案资源管理器窗口中,右键单击项目名称,在弹出的菜单中选择:添加》Windows窗体:


输入窗体名称“frmSetup”:


设置窗体的Text属性为“设置”,设置窗体的Size为“472,276”,StartPosition属性为“CenterScreen”。

添加设置标签页:

左侧工具箱》窗器:双击“TabControl”


设置的Dock属性为“Top”,Size属性为“456,200”;

添加标签页:


添加三个标签页,Text分别设置为“基本设置,自动上传,自动保存”


添加确定和取消按钮;

基本设置标签页:


从工具箱中添加两个GroupBox,分别为“热键、截图选项”;

添加两个RadioButton,用于热键选择;

添加四个CheckBox用于截图选项;

添加两个TextBox用于设置放大镜的尺寸;

添加两个PictureBox用于显示X和锁的图片;

添加图片资源:

双击Properties中的“Resources.resx”


切换到图像视图:


将图片复制,粘贴到这里,分别命名为“Lock,X”;

设置PictureBox的Image属性为对应的资源:


自动上传标签页:


自动保存标签页:


其中文件名称需要使用两个ComboBox,Items集合分别设置为:



编写代码:

双击设置窗体,切换到代码视图,添加私有变量:

        /// <summary>
        /// 保存Form1的句柄
        /// </summary>
        private IntPtr frm1Handle = IntPtr.Zero;

修改构造函数:

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="frm1_Handle"></param>
        public frmSetup(IntPtr frm1_Handle)
        {
            InitializeComponent();
            this.frm1Handle = frm1_Handle;
        }

为托盘菜单中的设置添加事件处理

打开主窗体Form1的设计视图,选中“contextMenuStrip1”


修改设置菜单的Name为“tsmi_Set”,双击设置菜单,添加代码:

        /// <summary>
        /// 托盘菜单设置事件处理程序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmi_Set_Click(object sender,EventArgs e)
        {
            frmSetup frm = new frmSetup(this.Handle);
            frm.ShowDialog();
        }
编译,调试一下,通过托盘图标的右键菜单》设置 可以打开刚刚添加的设置窗口了。

添加项目引用:

主窗体添加相关配置项变量:

        #region 基本设置参数
        /// <summary>
        /// 截图时是否显示截图信息栏
        /// </summary>
        public bool InfoBoxVisible = true;
        /// <summary>
        /// 截图时是否显示编辑工具栏
        /// </summary>
        public bool ToolBoxVisible = true;
        /// <summary>
        /// 截图中是否包含鼠标指针形状
        /// </summary>
        public bool IsCutCursor = true;
        /// <summary>
        /// 截图时是否显示放大镜
        /// </summary>
        public bool ZoomBoxVisible = true;
        /// <summary>
        /// 放大镜的尺寸——宽度
        /// </summary>
        public int ZoomBoxWidth = 120;
        /// <summary>
        /// 放大镜的尺寸——高度
        /// </summary>
        public int ZoomBoxHeight = 100;
        #endregion

        #region 图片上传参数
        public string PicDescFieldName = "pictitle";
        public string ImageFieldName = "upfile";
        public string PicDesc = "cutImage";
        public string UploadUrl = "http://";
        public bool DoUpload = false;
        #endregion

        #region 自动保存参数
        /// <summary>
        /// 是否自动保存到硬盘
        /// </summary>
        public bool AutoSaveToDisk = false;
        /// <summary>
        /// 自动保存目录
        /// </summary>
        public string AutoSaveDirectory = string.Empty;
        /// <summary>
        /// 是否启用日期格式“2013_02_22”的子目录
        /// </summary>
        public bool AutoSaveSubDir = false;
        /// <summary>
        /// 自动保存文件名前缀
        /// </summary>
        public string AutoSaveFileName1 = "屏幕截图";
        /// <summary>
        /// 自动文件名规则:日期时间,日期_序号,序号
        /// </summary>
        public string AutoSaveFileName2 = "日期时间";
        /// <summary>
        /// 自动保存文件格式:.png,.jpg,.jpeg,.gif,.bmp
        /// </summary>
        public string AutoSaveFileName3 = ".png";
        /// <summary>
        /// 自动保存文件名序号
        /// </summary>
        public int autoSaveFileIndex = 0;
        #endregion 自动保存参数

添加“AppSettingKeys”类:

    /// <summary>
    /// 提供配置文件中AppSettings节中对应的Key名称
    /// </summary>
    public static class AppSettingKeys
    {
        //基本设置
        public static string HotKeyMode = "HotKeyMode";
        public static string InfoBoxVisible = "InfoBoxVisible";
        public static string ToolBoxVisible = "ToolBoxVisible";
        public static string ZoomBoxVisible = "ZoomBoxVisible";
        public static string ZoomBoxWidth = "ZoomBoxWidth";
        public static string ZoomBoxHeight = "ZoomBoxHeight";
        public static string IsCutCursor = "IsCutCursor";
        //图片上传
        public static string PicDescFieldName = "PicDescFieldName";
        public static string ImageFieldName = "ImageFieldName";
        public static string PicDesc = "PicDesc";
        public static string UploadUrl = "UploadUrl";
        public static string DoUpload = "DoUpload";
        //自动保存
        public static string AutoSaveToDisk = "AutoSaveToDisk";
        public static string AutoSaveSubDir = "AutoSaveSubDir";
        public static string AutoSaveDirectory = "AutoSaveDirectory";
        public static string AutoSaveFileName1 = "AutoSaveFileName1";
        public static string AutoSaveFileName2 = "AutoSaveFileName2";
        public static string AutoSaveFileName3 = "AutoSaveFileName3";

    }

Program.cs文件添加枚举类型:

    /// <summary>
    /// 控制键的类型
    /// </summary>
    public enum KeyModifiers : uint
    {
        None = 0,Alt = 1,Control = 2,Shift = 4,Windows = 8
    }

警告:由于xxx是引用封送类的字段,访问上面的成员可能导致运行时异常

设置窗口完整代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Screenshot
{
    public partial class frmSetup : Form
    {
        /// <summary>
        /// 保存Form1的句柄
        /// </summary>
        private IntPtr frm1Handle = IntPtr.Zero;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="frm1_Handle"></param>
        public frmSetup(IntPtr frm1_Handle)
        {
            InitializeComponent();
            this.frm1Handle = frm1_Handle;
        }

        /// <summary>
        /// 确定按钮单击事件处理程序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_ok_Click(object sender,EventArgs e)
        {
            if (checkBox_autoSave.Checked && textBox_saveDir.Text.Trim().Length == 0)
            {
                MessageBox.Show("您选择了“自动保存屏幕截图到磁盘”\n但还没有设置存储目录!");
                return;
            }
            if (checkBox_autoSave.Checked && textBox_saveDir.Text.Trim().Length > 0)
            {
                if (!System.Text.RegularExpressions.Regex.IsMatch(textBox_saveDir.Text.Trim(),"^[a-zA-Z]:\\\\[^/:\\*\\?\"<>\\|]*$",System.Text.RegularExpressions.RegexOptions.IgnoreCase))
                {
                    MessageBox.Show("您选择了“自动保存屏幕截图到磁盘”\n但设置的存储目录不是有效的目录!");
                    return;
                }
                if (!System.IO.Directory.Exists(textBox_saveDir.Text.Trim()))
                {
                    MessageBox.Show("您选择了“自动保存屏幕截图到磁盘”\n但设置的存储目录不存在!");
                    return;
                }
            }
            Form1 frm = (Form1)Form.FromHandle(frm1Handle);
            if (frm != null)
            {
                //基本设置
                if (radioButton1.Checked) // && frm.HotKeyMode != 0 无论是否改变都重新注册热键,解决有时热键失效的问题
                {
                    Form1.UnregisterHotKey(frm1Handle,frm.hotKeyId);
                    Form1.RegisterHotKey(frm1Handle,frm.hotKeyId,(uint)KeyModifiers.Control | (uint)KeyModifiers.Alt,Keys.A);
                    frm.HotKeyMode = 0;
                }

                if (radioButton2.Checked) // && frm.HotKeyMode != 1 无论是否改变都重新注册热键,解决有时热键失效的问题
                {
                    Form1.UnregisterHotKey(frm1Handle,(uint)KeyModifiers.Control | (uint)KeyModifiers.Shift,Keys.A);
                    frm.HotKeyMode = 1;
                }

                frm.InfoBoxVisible = ckb_InfoBox.Checked;
                frm.ToolBoxVisible = ckb_ToolBox.Checked;
                frm.IsCutCursor = ckb_CutCursor.Checked;
                frm.ZoomBoxVisible = ckb_ZoomBox.Checked;


                frm.ZoomBoxWidth1 = Convert.ToInt32(tb_zoomBoxWidth.Text);
                frm.ZoomBoxHeight1 = Convert.ToInt32(tb_zoomBoxHeight.Text);

                if (frm.ZoomBoxWidth1 < 120)
                {
                    frm.ZoomBoxWidth1 = 120;
                    tb_zoomBoxWidth.Text = frm.ZoomBoxWidth1.ToString();
                }
                if (frm.ZoomBoxHeight1 < 100)
                {
                    frm.ZoomBoxHeight1 = 100;
                    tb_zoomBoxHeight.Text = frm.ZoomBoxHeight1.ToString();
                }

                //图片上传
                frm.PicDescFieldName = textBox_fieldDesc.Text;
                frm.ImageFieldName = textBox_fieldFile.Text;
                frm.PicDesc = textBox_desc.Text;
                frm.UploadUrl = textBox_uploadUrl.Text;
                frm.DoUpload = checkBox_upload.Checked;

                //自动保存
                frm.AutoSaveToDisk = checkBox_autoSave.Checked;
                frm.AutoSaveSubDir = chb_subDir.Checked;
                frm.AutoSaveDirectory = textBox_saveDir.Text;

                frm.AutoSaveFileName1 = textBox_fileName1.Text;
                if (comboBox_fileName2.SelectedItem != null)
                {
                    frm.AutoSaveFileName2 = comboBox_fileName2.Text;
                }
                else
                {
                    frm.AutoSaveFileName2 = "日期时间";
                }
                if (comboBox_Extn.SelectedItem != null)
                {
                    frm.AutoSaveFileName3 = comboBox_Extn.Text;
                }
                else
                {
                    frm.AutoSaveFileName3 = ".png";
                }
            }

            SaveConfiguration();

            this.Close();
        }

        /// <summary>
        /// 保存配置信息到配置文件
        /// </summary>
        private void SaveConfiguration()
        {
            System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(null);

            //基本设置
            SetConfigAppSetting(ref config,AppSettingKeys.HotKeyMode,radioButton1.Checked ? "1" : "0");
            SetConfigAppSetting(ref config,AppSettingKeys.InfoBoxVisible,ckb_InfoBox.Checked ? "1" : "0");
            SetConfigAppSetting(ref config,AppSettingKeys.ToolBoxVisible,ckb_ToolBox.Checked ? "1" : "0");
            SetConfigAppSetting(ref config,AppSettingKeys.IsCutCursor,ckb_CutCursor.Checked ? "1" : "0");
            SetConfigAppSetting(ref config,AppSettingKeys.ZoomBoxVisible,ckb_ZoomBox.Checked ? "1" : "0");
            SetConfigAppSetting(ref config,AppSettingKeys.ZoomBoxWidth,tb_zoomBoxWidth.Text);
            SetConfigAppSetting(ref config,AppSettingKeys.ZoomBoxHeight,tb_zoomBoxHeight.Text);

            //图片上传
            SetConfigAppSetting(ref config,AppSettingKeys.PicDescFieldName,textBox_fieldDesc.Text.Trim());
            SetConfigAppSetting(ref config,AppSettingKeys.ImageFieldName,textBox_fieldFile.Text.Trim());
            SetConfigAppSetting(ref config,AppSettingKeys.PicDesc,textBox_desc.Text.Trim());
            SetConfigAppSetting(ref config,AppSettingKeys.UploadUrl,textBox_uploadUrl.Text.Trim());
            SetConfigAppSetting(ref config,AppSettingKeys.DoUpload,checkBox_upload.Checked ? "1" : "0");

            //自动保存
            SetConfigAppSetting(ref config,AppSettingKeys.AutoSaveToDisk,checkBox_autoSave.Checked ? "1" : "0");
            SetConfigAppSetting(ref config,AppSettingKeys.AutoSaveSubDir,chb_subDir.Checked ? "1" : "0");
            SetConfigAppSetting(ref config,AppSettingKeys.AutoSaveDirectory,textBox_saveDir.Text.Trim());
            SetConfigAppSetting(ref config,AppSettingKeys.AutoSaveFileName1,textBox_fileName1.Text.Trim());
            if (comboBox_fileName2.SelectedItem != null)
            {
                SetConfigAppSetting(ref config,AppSettingKeys.AutoSaveFileName2,comboBox_fileName2.Text);
            }
            else
            {
                SetConfigAppSetting(ref config,"日期时间");
            }
            if (comboBox_Extn.SelectedItem != null)
            {
                SetConfigAppSetting(ref config,AppSettingKeys.AutoSaveFileName3,comboBox_Extn.Text);
            }
            else
            {
                SetConfigAppSetting(ref config,".png");
            }

            config.Save(System.Configuration.ConfigurationSaveMode.Modified);
        }

        /// <summary>
        /// 设置配置信息
        /// </summary>
        /// <param name="config"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        private bool SetConfigAppSetting(ref System.Configuration.Configuration config,string key,string value)
        {
            try
            {
                if (config.AppSettings.Settings[key] != null)
                {
                    config.AppSettings.Settings[key].Value = value;
                }
                else
                {
                    config.AppSettings.Settings.Add(key,value);
                }
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.Source + ex.StackTrace);
                return false;
            }
        }

        /// <summary>
        /// 获取配置信息
        /// </summary>
        /// <param name="config"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        private string GetConfigAppSetting(ref System.Configuration.Configuration config,string key)
        {
            try
            {
                if (config.AppSettings.Settings[key] != null)
                {
                    return config.AppSettings.Settings[key].Value;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.Source + ex.StackTrace);
            }
            return string.Empty;
        }

        /// <summary>
        /// 取消按钮单击事件处理程序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_cancel_Click(object sender,EventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// 窗口加载事件处理程序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmSetup_Load(object sender,EventArgs e)
        {
            chb_subDir.Text = "启用(按日期命名,格式:" + DateTime.Now.Date.ToString("yyyy_MM_dd") + ")";

            Form1 frm = (Form1)Form.FromHandle(frm1Handle);
            if (frm != null)
            {
                //基本设置
                if (frm.HotKeyMode == 0)
                {
                    radioButton1.Checked = true;
                    radioButton2.Checked = false;
                }
                else
                {
                    radioButton1.Checked = false;
                    radioButton2.Checked = true;
                }

                ckb_InfoBox.Checked = frm.InfoBoxVisible;
                ckb_ToolBox.Checked = frm.ToolBoxVisible;
                ckb_CutCursor.Checked = frm.IsCutCursor;
                ckb_ZoomBox.Checked = frm.ZoomBoxVisible;

                //图片上传
                textBox_fieldDesc.Text = frm.PicDescFieldName;
                textBox_fieldFile.Text = frm.ImageFieldName;
                textBox_desc.Text = frm.PicDesc;
                textBox_uploadUrl.Text = frm.UploadUrl;
                checkBox_upload.Checked = frm.DoUpload;

                //自动保存
                checkBox_autoSave.Checked = frm.AutoSaveToDisk;
                chb_subDir.Checked = frm.AutoSaveSubDir;
                textBox_saveDir.Text = frm.AutoSaveDirectory;
                textBox_fileName1.Text = frm.AutoSaveFileName1;
                comboBox_fileName2.SelectedItem = frm.AutoSaveFileName2;
                comboBox_Extn.SelectedItem = frm.AutoSaveFileName3;


            }
        }
        /// <summary>
        /// 浏览按钮事件处理程序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_browse_Click(object sender,EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.Description = "请选择屏幕截图的保存目录:";
            fbd.ShowNewFolderButton = true;
            fbd.RootFolder = Environment.SpecialFolder.MyComputer;
            fbd.SelectedPath = textBox_saveDir.Text;
            if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                textBox_saveDir.Text = fbd.SelectedPath;
            }
        }
        /// <summary>
        /// 更新自动保存文件名称示例
        /// </summary>
        private void UpdateFileNameExmple()
        {
            string AutoSaveFileName2 = string.Empty;
            if (comboBox_fileName2.SelectedItem != null)
            {
                AutoSaveFileName2 = comboBox_fileName2.Text;
            }
            string AutoSaveFileName3 = ".png";
            if (comboBox_Extn.SelectedItem != null)
            {
                AutoSaveFileName3 = comboBox_Extn.Text;
            }

            switch (AutoSaveFileName2)
            {
                case "日期_序号":
                    textBox_exmple.Text = textBox_fileName1.Text + DateTime.Now.ToString("yyyy-MM-dd_") + "0001" + AutoSaveFileName3;
                    break;
                case "序号":
                    textBox_exmple.Text = textBox_fileName1.Text + "0001" + AutoSaveFileName3;
                    break;
                default:
                    textBox_exmple.Text = textBox_fileName1.Text + DateTime.Now.ToString("yyyy-MM-dd_HHmmss") + AutoSaveFileName3;
                    break;
            }
        }

        private void comboBox_fileName2_SelectedIndexChanged(object sender,EventArgs e)
        {
            UpdateFileNameExmple();
        }

        private void comboBox_Extn_SelectedIndexChanged(object sender,EventArgs e)
        {
            UpdateFileNameExmple();
        }

        private void textBox_fileName1_TextChanged(object sender,EventArgs e)
        {
            UpdateFileNameExmple();
        }

        // Boolean flag used to determine when a character other than a number is entered.
        private bool nonNumberEntered = false;

        private void tb_zoomBoxWidth_KeyDown(object sender,KeyEventArgs e)
        {
            // Initialize the flag to false.
            nonNumberEntered = false;

            // Determine whether the keystroke is a number from the top of the keyboard.
            if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
            {
                // Determine whether the keystroke is a number from the keypad.
                if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
                {
                    // Determine whether the keystroke is a backspace.
                    if (e.KeyCode != Keys.Back)
                    {
                        // A non-numerical keystroke was pressed.
                        // Set the flag to true and evaluate in KeyPress event.
                        nonNumberEntered = true;
                    }
                }
            }
            //If shift key was pressed,it's not a number.
            if (Control.ModifierKeys == Keys.Shift)
            {
                nonNumberEntered = true;
            }
        }

        private void tb_zoomBoxHeight_KeyDown(object sender,it's not a number.
            if (Control.ModifierKeys == Keys.Shift)
            {
                nonNumberEntered = true;
            }
        }

        private void tb_zoomBoxWidth_KeyPress(object sender,KeyPressEventArgs e)
        {

            // Check for the flag being set in the KeyDown event.
            if (nonNumberEntered == true)
            {
                // Stop the character from being entered into the control since it is non-numerical.
                e.Handled = true;
            }
        }


        private void tb_zoomBoxHeight_KeyPress(object sender,KeyPressEventArgs e)
        {
            // Check for the flag being set in the KeyDown event.
            if (nonNumberEntered == true)
            {
                // Stop the character from being entered into the control since it is non-numerical.
                e.Handled = true;
            }
        }

        /// <summary>
        /// 放大镜宽度改变事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tb_zoomBoxWidth_TextChanged(object sender,EventArgs e)
        {
            int zoomWidth = Convert.ToInt32(tb_zoomBoxWidth.Text);
            if (zoomWidth < 120) { zoomWidth = 120; }

            tb_zoomBoxHeight.Text = ((int)(zoomWidth * 100 / 120)).ToString();
        }

        /// <summary>
        /// 放大镜高度改变事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tb_zoomBoxHeight_TextChanged(object sender,EventArgs e)
        {
            int zoomHeight = Convert.ToInt32(tb_zoomBoxHeight.Text);
            if (zoomHeight < 100) { zoomHeight = 100; }

            tb_zoomBoxWidth.Text = ((int)(zoomHeight * 120 / 100)).ToString();
        }
    }
}

源码下载:http://download.csdn.net/detail/testcs_dn/7263143

下一篇:C#软件开发实例.私人订制自己的屏幕截图工具(七)添加放大镜的功能

原文地址:https://blog.csdn.net/testcs_dn/article/details/24466763

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


项目中经常遇到CSV文件的读写需求,其中的难点主要是CSV文件的解析。本文会介绍CsvHelper、TextFieldParser、正则表达式三种解析CSV文件的方法,顺带也会介绍一下CSV文件的写方法。 CSV文件标准 在介绍CSV文件的读写方法前,我们需要了解一下CSV文件的格式。 文件示例 一
简介 本文的初衷是希望帮助那些有其它平台视觉算法开发经验的人能快速转入Halcon平台下,通过文中的示例开发者能快速了解一个Halcon项目开发的基本步骤,让开发者能把精力完全集中到算法的开发上面。 首先,你需要安装Halcon,HALCON 18.11.0.1的安装包会放在文章末尾。安装包分开发和
这篇文章主要简单记录一下C#项目的dll文件管理方法,以便后期使用。 设置dll路径 参考C#开发奇技淫巧三:把dll放在不同的目录让你的程序更整洁中间的 方法一:配置App.config文件的privatePath : &lt;runtime&gt; &lt;assemblyBinding xml
在C#中的使用JSON序列化及反序列化时,推荐使用Json.NET——NET的流行高性能JSON框架,当然也可以使用.NET自带的 System.Text.Json(.NET5)、DataContractJsonSerializer、JavaScriptSerializer(不推荐)。
事件总线是对发布-订阅模式的一种实现,是一种集中式事件处理机制,允许不同的组件之间进行彼此通信而又不需要相互依赖,达到一种解耦的目的。&#xA;EventBus维护一个事件的字典,发布者、订阅者在事件总线中获取事件实例并执行发布、订阅操作,事件实例负责维护、执行事件处理程序。
通用翻译API的HTTPS 地址为https://fanyi-api.baidu.com/api/trans/vip/translate,使用方法参考通用翻译API接入文档 。&#xA;请求方式可使用 GET 或 POST 方式(Content-Type 请指定为:application/x-www-for
词云”由美国西北大学新闻学副教授、新媒体专业主任里奇·戈登(Rich Gordon)于2006年最先使用,是通过形成“关键词云层”或“关键词渲染”,对文本中出现频率较高的“关键词”的视觉上的突出。词云图过滤掉大量的文本信息,使浏览者只要一眼扫过文本就可以领略文本的主旨。&#xA;网上大部分文章介绍的是使用P
微软在.NET中对串口通讯进行了封装,我们可以在.net2.0及以上版本开发时直接使用SerialPort类对串口进行读写操作。&#xA;为操作方便,本文对SerialPort类做了一些封装,暂时取名为**SerialPortClient**。
简介 管道为进程间通信提供了平台, 管道分为两种类型:匿名管道、命名管道,具体内容参考.NET 中的管道操作。简单来说,匿名管道只能用于本机的父子进程或线程之间,命名管道可用于远程主机或本地的任意两个进程,本文主要介绍命名管道的用法。 匿名管道在本地计算机上提供进程间通信。 与命名管道相比,虽然匿名
目录自定义日志类NLog版本的日志类Serilog版本的日志类 上个月换工作,新项目又要重新搭建基础框架,把日志实现部分单独记录下来方便以后参考。 自定义日志类 代码大部分使用ChatGPT生成,人工进行了测试和优化,主要特点: 线程安全,日志异步写入文件不影响业务逻辑 支持过期文件自动清理,也可自
[TOC] # 原理简介 本文参考[C#/WPF/WinForm/程序实现软件开机自动启动的两种常用方法](https://blog.csdn.net/weixin_42288432/article/details/120059296),将里面中的第一种方法做了封装成**AutoStart**类,使
简介 FTP是FileTransferProtocol(文件传输协议)的英文简称,而中文简称为“文传协议”。用于Internet上的控制文件的双向传输。同时,它也是一个应用程序(Application)。基于不同的操作系统有不同的FTP应用程序,而所有这些应用程序都遵守同一种协议以传输文件。 FTP
使用特性,可以有效地将元数据或声明性信息与代码(程序集、类型、方法、属性等)相关联。 将特性与程序实体相关联后,可以在运行时使用反射这项技术查询特性。&#xA;在 C# 中,通过用方括号 ([]) 将特性名称括起来,并置于应用该特性的实体的声明上方以指定特性。
# 简介 主流的识别库主要有ZXing.NET和ZBar,OpenCV 4.0后加入了QR码检测和解码功能。本文使用的是ZBar,同等条件下ZBar识别率更高,图片和部分代码参考[在C#中使用ZBar识别条形码](https://www.cnblogs.com/w2206/p/7755656.htm
C#中Description特性主要用于枚举和属性,方法比较简单,记录一下以便后期使用。 扩展类DescriptionExtension代码如下: using System; using System.ComponentModel; using System.Reflection; /// &lt;
本文实现一个简单的配置类,原理比较简单,适用于一些小型项目。主要实现以下功能:保存配置到json文件、从文件或实例加载配置类的属性值、数据绑定到界面控件。&#xA;一般情况下,项目都会提供配置的设置界面,很少手动更改配置文件,所以选择以json文件保存配置数据。
前几天用SerialPort类写一个串口的测试程序,关闭串口的时候会让界面卡死。网上大多数方法都是定义2个bool类型的标记Listening和Closing,关闭串口和接受数据前先判断一下。我的方法是DataReceived事件处理程序用this.BeginInvoke()更新界面,不等待UI线程
约束告知编译器类型参数必须具备的功能。 在没有任何约束的情况下,类型参数可以是任何类型。 编译器只能假定 System.Object 的成员,它是任何 .NET 类型的最终基类。 如果客户端代码使用不满足约束的类型,编译器将发出错误。 通过使用 where 上下文关键字指定约束。&#xA;最常用的泛型约束为
protobuf-net是用于.NET代码的基于契约的序列化程序,它以Google设计的“protocol buffers”序列化格式写入数据,适用于大多数编写标准类型并可以使用属性的.NET语言。&#xA;protobuf-net可通过NuGet安装程序包,也可直接访问github下载源码:https:/
工作中经常遇到需要实现TCP客户端或服务端的时候,如果每次都自己写会很麻烦且无聊,使用SuperSocket库又太大了。这时候就可以使用SimpleTCP了,当然仅限于C#语言。&#xA;SimpleTCP是一个简单且非常有用的 .NET 库,用于处理启动和使用 TCP 套接字(客户端和服务器)的重复性任务