2022年12月8日 星期四

[C#]一般事件E EventArgs也能變成其他事件

 EventArgs是一個主要事件的基底基層,所以我們只要將EventArgs強制轉型成MouseEventArgs就好了

參考來源: https://cyfangnotepad.blogspot.com/2012/08/cnet-eventargs.html


private void lkl_ForgetPsw_DoubleClick(object sender, EventArgs e)
{
  MouseEventArgs Mouse = (MouseEventArgs)e;
  if (Mouse.Button == MouseButtons.Left)
  {
     MessageBox.Show("Ok");
   }

}

2022年10月23日 星期日

[C#]NuGet套件推薦-ILMerge

 使用WindowsForm開發時很常會引用許多dll或其他相關套件,

編譯成執行檔時,除執行檔外還會有許多dll或.Json的文件,不美觀外也不便給他人使用


使用此套件即可解決此問題


https://github.com/dotnet/ILMerge

2022年10月17日 星期一

[C#]語法混合應用

一些C# 的基礎應用範例

 int iData = 123;
string str = $"Hello{iData}!!";

Action<string> greet = name =>
{
    string greeting = $"hello {name}!";
    Console.WriteLine(greeting);
};
greet("world");



其他執行續範例說明:
正常寫法如以下兩段:
private void setText(Object str) { using (StreamWriter sw = new StreamWriter(@"D:\test.txt",true,Encoding.UTF8)) { sw.WriteLine(str.ToString()); } } protected void Button1_Click(object sender, EventArgs e) { string str = "測試四"; ThreadPool.QueueUserWorkItem(new WaitCallback(setText),(Object) str); }

透握委派方式簡化程式碼:
protected void Button1_Click(object sender, EventArgs e) { string str = "測試三"; ThreadPool.QueueUserWorkItem(delegate { using (StreamWriter sw = new StreamWriter(@"D:\test.txt", true, Encoding.UTF8)); //路徑 , 不複寫檔案 , 編碼 { sw.WriteLine(str); } }); }

透過Lambda寫法:
protected void Button1_Click(object sender, EventArgs e) { string str = "測試五"; ThreadPool.QueueUserWorkItem(callback => { using (StreamWriter sw = new StreamWriter(@"D:\test.txt", true, Encoding.UTF8)) { sw.WriteLine(str.ToString()); } }); }




參考來源:
https://learn.microsoft.com/zh-tw/dotnet/csharp/language-reference/operators/lambda-expressions
https://dotblogs.com.tw/ken74114/2010/12/08/19988

2022年10月14日 星期五

[C#]圖形比對

public static bool ImageCompareString(Bitmap firstImage, Bitmap secondImage)
{
    MemoryStream ms = new MemoryStream();
    firstImage.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
    String firstBitmap = Convert.ToBase64String(ms.ToArray());
    ms.Position = 0;

    secondImage.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
    String secondBitmap = Convert.ToBase64String(ms.ToArray());

    if (firstBitmap.Equals(secondBitmap))
    {
        return true;
    }
    else
    {
        return false;
    }
}

使用範例"指定路徑圖片判斷是否一致"

PictureBox pb = new PictureBox();
PictureBox pb2 = new PictureBox();
string str = Directory.GetCurrentDirectory() + "\\1.jpg";
string str2 = Directory.GetCurrentDirectory() + "\\2.jpg";
pb.Load(str);
pb2.Load(str2);
Bitmap bmp1 = new Bitmap(pb.Image);
Bitmap bmp2 = new Bitmap(pb2.Image);
bool Result = false;
Result = ImageCompareString(bmp1, bmp2);



2022年8月10日 星期三

[C#]除錯專用,當程式出現非預期錯誤或閃退

 在Main加入以下可在程式閃退前,提示訊息

static class Program
{
    /// <summary>
    /// 應用程式的主要進入點。
    /// </summary>
    [STAThread]
    static void Main()
    {
        AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);


        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());


    }
    private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        Console.WriteLine(e.ExceptionObject.ToString());
        MessageBox.Show(e.ExceptionObject.ToString());
    }
}

[C#]限定只開啟一個程序,防止重複啟動

 Main 中加入 以下

static void Main()
{
  //Other
  bool ret;
  System.Threading.Mutex mutex = new System.Threading.Mutex(true, Application.ProductName, out ret);
  if (ret)
  {
     Application.EnableVisualStyles();
     Application.SetCompatibleTextRenderingDefault(false);
     Application.Run(new Form1());
  }
  else
  {
    MessageBox.Show(null, "This process is running.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
    //   提示信息,可以刪除。  
    Application.Exit();//退出程式  
  }

}

[SQL]顯示千分位與小數顯示

  CONVERT ( data_type [ ( length ) ] , expression [ , style ] ) CONVERT style參數說明 1  (expression為 money 或 smallmoney型別): 0 : 預設,保留小數位後兩位,並四捨...