C# 開發桌面應用簡單介紹

来源:https://www.cnblogs.com/codeOnMar/archive/2023/11/28/17861081.html
-Advertisement-
Play Games

一. C#使用場景介紹 C#是微軟公司發佈的一種由C和C++衍生出來的面向對象的編程語言、運行於.NET Framework和.NET Core(完全開源,跨平臺)之上的高級程式設計語言。 二. 開發流程 1. 創建項目:打開Visual Studio後右側選擇“創建新項目”,然後選擇“C# Win ...


一. C#使用場景介紹

  C#是微軟公司發佈的一種由C和C++衍生出來的面向對象的編程語言、運行於.NET Framework和.NET Core(完全開源,跨平臺)之上的高級程式設計語言。

二. 開發流程

  1. 創建項目:打開Visual Studio後右側選擇“創建新項目”,然後選擇“C# Windows窗體應用”即可創建桌面程式

  2. 創建窗體:創建後會自動創建一個Form窗體作為主視窗,可以採用拖拽方式進行項目開發,通過導航欄的《視圖》-->《工具欄》打開內置的工具箱功能

  3. 啟動開發:開發時有兩種模式,可視化開發和編寫代碼。可視化開發選中窗體或者元素右側會彈出屬性欄可以設置樣式以及定義事件;代碼開發可以通過對應的映射文件進行邏輯開發。

  4. 運行程式:開發完成後點擊屏幕上方的綠色箭頭按鈕,“啟動”按鈕運行程式同時進入Debug模式,支持功能調試,“開始運行”按鈕只運行程式,沒有進入Debug模式。

  5. 生成解決方案:點擊導航欄上的“生成”->“生成解決方案”就可以生成項目文件

三. 常用功能

  1. 彈出框和自動關閉彈框:項目開發中經常需要彈出框以及關閉彈出框,建議根據不同的等級封裝統一組件進行調用

// 提示彈框
public static void PopWarning(string tip, string title = "提示")
{
    MessageBox.Show(tip, title);
}

// 錯誤彈框
public static void PopError(string tip, string title = "錯誤")
{
    MessageBox.Show(tip, title);
}

// 自動關閉彈框
public void BoxAutoClose(string tip, int time = 3000)
{
    Form msg = new Form();
    Task.Run(new Action(() =>
    {
        Thread.Sleep(time);
        msg.Invoke((Action)(() =>
        {
            msg.Close();
        }));
    }));
    MessageBox.Show(msg, tip, "提示");
}

  2. 串口的使用和指令的封裝:項目開發中需要和固件通過串口進行數據交互,C#內置Serialport組件可以使用,但是建議不要拖拽,最好是new一個,然後將發送指令、接收指令以及超時機制結合在一起使用

// 命令類,存儲指令和時間對象
class Command {
    // 存儲指令數據集
    public Dictionary<string, Hashtable> data = new Dictionary<string, Hashtable>();

    // 預設超時時間是7秒
    public void Set(string id, int timeUpper = 7)
    {
        if (data.ContainsKey(id))
        {
            data[id]["time"] = DateTime.Now;
        }
        else
        {
            Hashtable ht = new Hashtable();
            ht.Add("time", DateTime.Now);
            ht.Add("timeUpper", timeUpper);
            data[id] = ht;
        }
    }

    public void Delete(string id)
    {
        data.Remove(id);
    }

    public void DeleteAll()
    {
        data.Clear();
    }

    /**
     * 識別超時的命令
     */
    public string[] CheckOverTime()
    {
        if (data == null)
        {
            return new string[0] { };
        }
        string[] coms = new string[data.Count];

        for (int i = 0; i < data.Count; i++)
        {
            DateTime val = (DateTime)data.ElementAt(i).Value["time"];
            int timeUpper = (int)data.ElementAt(i).Value["timeUpper"];
            if (new TimeSpan(DateTime.Now.Ticks - val.Ticks).TotalSeconds > timeUpper)
            {
                coms[i] = data.ElementAt(i).Key;
            }
        }

        return coms.Where(e => e != null).ToArray();
    }
}
創建一個Command類,主要存儲發送指令集以及判斷指令是否超時
class Serial {
    private SerialPort port;
    Form main;
    Command cmd;
    
    public Serial(Form main)
    {
        this.main = main;
        cmd = new Command();
        this.Init();
        this.CreateTimer();
    }

    /**
     * 創建定時器,每秒查詢一次是否有超時的命令
     */
    public void CreateTimer()
    {
        System.Timers.Timer cTimer = new System.Timers.Timer();
        cTimer.Interval = 1000;
        cTimer.Enabled = true;
        cTimer.AutoReset = true;
        cTimer.Elapsed += new System.Timers.ElapsedEventHandler((Object source, ElapsedEventArgs e) =>
        {
            string[] cmds = cmd.CheckOverTime();
            for (int i = 0; i < cmds.Length; i++)
            {
                cmd.Delete(cmds[i]);
                if (cmds[i] != "FF")
                {
                    // 返回主視窗超時標識
                    main.DealRes(new byte[] { (byte)Convert.ToInt32(cmds[i], 16) }, 1);
                }
            }
        });
    }

    /**
     * 初始化創建串口組件
     */
    void Init() {

        port = new SerialPort();
        port.BaudRate = 9600;
        port.Parity = Parity.None;
        port.StopBits = StopBits.One;
        port.DataBits = 8;
        port.DataReceived += new SerialDataReceivedEventHandler(ReceiveData);
    }

    /**
     * 將字元數組轉換成字元串
     */
    public static string ByteToStr(byte[] data)
    {
        string str = "";
        for (int i = 0; i < data.Length; i++)
        {
            str += data[i].ToString("X2");
        }
        return str;
    }

    /**
     * 接收數據並解析,將解析結果返回主視窗
     */
    public void ReceiveData(object sender, SerialDataReceivedEventArgs e) {
        // 接受數據時建議保留延時200毫秒,否則會存在由於接收不及時,一包數據被分成兩段返回的情況
        System.Threading.Thread.Sleep(200);
        main.BeginInvoke((EventHandler)delegate
        {
            if (port.IsOpen)
            {
                int readLength = port.BytesToRead;
                byte[] buff = new byte[readLength];

                port.Read(buff, 0, buff.Length);
                if (buff.Length != 0)
                {
                    
                    cmd.Delete(ByteToStr(new byte[] { buff[0] }));
                    main.DealRes(buff);
                }
            }
        });
    }
}
創建一個串口類,主要用於發送指令和接收返回的數據

  3. 監測串口的插拔狀態:框架內置了方法可以監控串口的變化,只需要重寫該方法即可

protected override void WndProc(ref Message m)
{
    try
    {
        // Windows消息編號
        if (m.Msg == 0x219)
        {
            if ((bool)portButton1.Tag && !port1.IsOpen && (bool)portButton2.Tag && !port2.IsOpen)
            {
                PopError("串口1和串口2狀態有更新");
            }
            else if ((bool)portButton1.Tag && !port1.IsOpen) {
                PopError("串口1狀態有更新");
            } else if ((bool)portButton2.Tag && !port2.IsOpen) {
                PopError("串口2狀態有更新");
            }
        }
    }
    catch
    {
        Util.PopError("監控串口狀態錯誤");
    }
    base.WndProc(ref m);
}

監控串口狀態
監控串口狀態

  4. 安裝NPOI組件實現Excel的讀寫功能:Excel的讀寫依賴三方件nuget。

    安裝NPOI有兩種辦法:

    第一種利用Visual Studio導航欄的“工具”->“NuGet包管理器”進行下載,這種比較簡單方便,下載後項目可以直接引用使用

    第二種則是手動安裝,解決無法線上安裝的情況,比如網路受限等:

    a. Nuget下載:可以從官方https://www.nuget.org/downloads進行下載,將下載的nuget.exe拷貝到某個目錄,同時在該目錄下打開命令視窗。

    b. NPOI安裝:在目錄A打開命令視窗,執行命令nuget install NPOI -SolutionDirectory 項目根目錄 -PackageSaveMode nupkg,安裝後會生成一個packages文件

    

    c. 還需要在項目根目錄下的.csproj目錄下手動添加引入文件,同時註意packages需要和安裝後的目錄對應,否則引用無效,安裝完成後返回VS工具會提示“全部刷新引用”,同意即可。

<Reference Include="BouncyCastle.Cryptography, Version=2.0.0.0, Culture=neutral, PublicKeyToken=072edcf4a5328938, processorArchitecture=MSIL">
      <HintPath>.\packages\BouncyCastle.Cryptography.2.2.1\lib\net461\BouncyCastle.Cryptography.dll</HintPath>
    </Reference>
    <Reference Include="Enums.NET, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7ea1c1650d506225, processorArchitecture=MSIL">
      <HintPath>.\packages\Enums.NET.4.0.1\lib\net45\Enums.NET.dll</HintPath>
    </Reference>
    <Reference Include="ICSharpCode.SharpZipLib, Version=1.3.3.11, Culture=neutral, PublicKeyToken=1b03e6acf1164f73, processorArchitecture=MSIL">
      <HintPath>.\packages\SharpZipLib.1.3.3\lib\net45\ICSharpCode.SharpZipLib.dll</HintPath>
    </Reference>
    <Reference Include="MathNet.Numerics, Version=4.15.0.0, Culture=neutral, PublicKeyToken=cd8b63ad3d691a37, processorArchitecture=MSIL">
      <HintPath>.\packages\MathNet.Numerics.Signed.4.15.0\lib\net461\MathNet.Numerics.dll</HintPath>
    </Reference>
    <Reference Include="Microsoft.IO.RecyclableMemoryStream, Version=2.3.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
      <HintPath>.\packages\Microsoft.IO.RecyclableMemoryStream.2.3.2\lib\net462\Microsoft.IO.RecyclableMemoryStream.dll</HintPath>
    </Reference>
    <Reference Include="NPOI.Core, Version=2.6.2.0, Culture=neutral, PublicKeyToken=0df73ec7942b34e1, processorArchitecture=MSIL">
      <HintPath>.\packages\NPOI.2.6.2\lib\net472\NPOI.Core.dll</HintPath>
    </Reference>
    <Reference Include="NPOI.OOXML, Version=2.6.2.0, Culture=neutral, PublicKeyToken=0df73ec7942b34e1, processorArchitecture=MSIL">
      <HintPath>.\packages\NPOI.2.6.2\lib\net472\NPOI.OOXML.dll</HintPath>
    </Reference>
    <Reference Include="NPOI.OpenXml4Net, Version=2.6.2.0, Culture=neutral, PublicKeyToken=0df73ec7942b34e1, processorArchitecture=MSIL">
      <HintPath>.\packages\NPOI.2.6.2\lib\net472\NPOI.OpenXml4Net.dll</HintPath>
    </Reference>
    <Reference Include="NPOI.OpenXmlFormats, Version=2.6.2.0, Culture=neutral, PublicKeyToken=0df73ec7942b34e1, processorArchitecture=MSIL">
      <HintPath>.\packages\NPOI.2.6.2\lib\net472\NPOI.OpenXmlFormats.dll</HintPath>
    </Reference>
    <Reference Include="SixLabors.Fonts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d998eea7b14cab13, processorArchitecture=MSIL">
      <HintPath>.\packages\SixLabors.Fonts.1.0.0\lib\netstandard2.0\SixLabors.Fonts.dll</HintPath>
    </Reference>
    <Reference Include="SixLabors.ImageSharp, Version=2.0.0.0, Culture=neutral, PublicKeyToken=d998eea7b14cab13, processorArchitecture=MSIL">
      <HintPath>.\packages\SixLabors.ImageSharp.2.1.4\lib\net472\SixLabors.ImageSharp.dll</HintPath>
    </Reference>
    <Reference Include="System.ComponentModel.DataAnnotations" />
    <Reference Include="System.Configuration" />
    <Reference Include="System.Core" />
    <Reference Include="System.DirectoryServices" />
    <Reference Include="System.Memory, Version=4.0.1.2, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
      <HintPath>.\packages\System.Memory.4.5.5\lib\net461\System.Memory.dll</HintPath>
    </Reference>
    <Reference Include="System.Numerics" />
    <Reference Include="System.Numerics.Vectors, Version=4.1.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
      <HintPath>.\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll</HintPath>
    </Reference>
    <Reference Include="System.Runtime.CompilerServices.Unsafe, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
      <HintPath>.\NPOISystem.Runtime.CompilerServices.Unsafe.5.0.0\lib\net45\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
    </Reference>
    <Reference Include="System.Runtime.Serialization" />
    <Reference Include="System.Security" />
    <Reference Include="System.Security.AccessControl, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
      <HintPath>.\packages\System.Security.AccessControl.6.0.0\lib\net461\System.Security.AccessControl.dll</HintPath>
    </Reference>
    <Reference Include="System.Security.Cryptography.Xml, Version=6.0.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
      <HintPath>.\packages\System.Security.Cryptography.Xml.6.0.1\lib\net461\System.Security.Cryptography.Xml.dll</HintPath>
    </Reference>
    <Reference Include="System.Security.Principal.Windows, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
      <HintPath>.\packages\System.Security.Principal.Windows.5.0.0\lib\net461\System.Security.Principal.Windows.dll</HintPath>
    </Reference>
    <Reference Include="System.Text.Encoding.CodePages, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
      <HintPath>.\packages\System.Text.Encoding.CodePages.5.0.0\lib\net461\System.Text.Encoding.CodePages.dll</HintPath>
    </Reference>
配置引用內容

    d. 導出Excel和讀取Excel網上案列較多你再擴展。


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 原文 : https://openaigptguide.com/chatgpt-aigc-difference/ AIGC 和 ChatGPT 都是人工智慧技術,但它們的功能和應用場景不同。 AIGC(AI-GeneratedContent,人工智慧自動生成內容)是人工智慧、電腦圖形學和深度學習等 ...
  • 你可以使用 Python 中的列表拼接操作來合併兩個或多個列表。Python 提供了幾種方式來實現列表的拼接,包括使用 + 運算符、extend() 方法和列表解析。 以下是這些方法的示例: 1.使用 + 運算符: list1 = [1, 2, 3] list2 = [4, 5, 6] concat ...
  • Python還提供了許多其他用於數據處理和轉換的內置函數和模塊。以下是一些常用的數據處理函數和模塊: sorted sorted(iterable, key=func, reverse=False) 用於對可迭代對象進行排序。你可以指定一個可選的 key 函數來自定義排序規則,以及一個可選的 rev ...
  • HTTP(Hypertext Transfer Protocol)是一種用於傳輸超文本的協議。它是一種無狀態的、應用層的協議,用於在電腦之間傳輸超文本文檔,通常在 Web 瀏覽器和 Web 伺服器之間進行數據通信。HTTP 是由互聯網工程任務組(IETF)定義的,它是基於客戶端-伺服器模型的協議,... ...
  • 通過實現一個簡易版本的跳錶,可以加深了對Python編程的理解。跳錶是一種跳躍式的數據結構,通過索引層提供快速查找的能力,提高了查找的效率。在實現跳錶的過程中,會更加熟悉了Python的語法和特性,並且可以更加靈活地運用它來解決實際問題。 ...
  • 1、定義 Cron表達式是一種用於定義定時任務的格式化字元串。它被廣泛用於Unix、Linux和類Unix系統中,用於在指定的時間執行預定的任務。Cron表達式由6個欄位組成,每個欄位通過空格分隔開。 在本文中,我們將學習如何理解和編寫Cron表達式。 Cron表達式的格式如下: * * * * * ...
  • 一般在會議、教學或培訓活動中,我們都會選擇PPT文檔來進行內容展示。與PDF文檔相比,PPT文檔具有較強的可編輯性,可以隨時增刪元素,並且還可以設置豐富多樣的動畫效果來吸引觀眾註意。那麼如何通過C#將PDF文檔轉為PPT文檔呢?本文將教大家僅使用3行代碼就實現這一功能。 PDF轉PPT所需工具: S ...
  • 本文簡介 MixCoreCMS是一個基於.NET Core框架的開源內容管理系統(CMS),提供了豐富的的基礎功能和插件,是一款面向未來的企業 Web CMS,可輕鬆構建任何類型的應用程式。集成了Google Analytics分析,以及友好的Seo功能,非常適合用於創建企業網站、內容系統、個人博客 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...