2022年5月19日 星期四

[C#]使用json取得資料/取得多層

 使用環境VS2019
NuGet先加入Newtonsoft.json 
並且使用以下宣告

using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

在mamespace中 宣告json要使用的結構類別

    public class cJsonDataItem

    {

        public string server { get; set; }

        public string database { get; set; }

        public string user { get; set; }

        public string password { get; set; }

        public string linkedserver { get; set; }

    }

在取得json 字串(下方用temp代替)後使用以下方式即可取得數值

string temp = "{    "accountingAck": {        "server": "127.0.0.1",        "database": "Accounting_Ack_AZ",        "user": "sa",        "password": "123456789",        "linkedserver": "REMOTE_LINK"    }}";

var dynamic_obj = JObject.Parse(temp);

string s = string.Empty;

StringBuilder strb = new StringBuilder();

StringBuilder strbb = new StringBuilder();

 

foreach (JProperty item in dynamic_obj.Children())

{

    strb.Append(item.Name);

    strbb.Append(item.Value);

}

StringBuilder strbc = new StringBuilder();

StringBuilder strbd = new StringBuilder();

dynamic darray = JsonConvert.DeserializeObject(strbb.ToString());   //此行為動態拿法 不用宣告最上面的類別

cJsonDataItem cjs = JsonConvert.DeserializeObject<cJsonDataItem>(strbb.ToString()); //此行需要使用最上面的類別cJsonDataItem

最終透過cjs 可以輕鬆拿取到各項的字串結果

temp = cjs.server;

temp = cjs.database;

            


2022年5月9日 星期一

[C#]程式焦點切換後恢復焦點的方式 foucs

 使用user32.dll 的Api,應用情境如以下:
在程式A上點擊按鈕後啟動B程式在背景執行,此時的焦點需要回到A程式
正常流程焦點會在B,透過以下方式可以將焦點設定為A


範例:

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

namespace Test
{
    public partial class PlayVoice : Form
    {
        public PlayVoice()
        {
            InitializeComponent();
        }


        [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetForegroundWindow", CharSet = System.Runtime.InteropServices.CharSet.Auto, ExactSpelling = true)]
        public static extern IntPtr GetF();             //獲得本窗體的控制代碼
        [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]
        public static extern bool SetF(IntPtr hWnd);    //設定此窗體為活動窗體

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.Handle != GetF())           //如果本視窗沒有獲得焦點
                SetF(this.Handle);                //設定本視窗獲得焦點
        }
    }
}

2022年5月4日 星期三

[C#]視窗在工具列點擊出現提示選單

 新增工具元件"notifyIcon" 與 "contextMenuStrip" 加到專案中

1.在Form.cs 的UI上選擇contextMenuStrip加入事件

2.到notifyIcon的屬性contextMenuStrip添加contextMenuStrip1元件名稱

3.到notifyIcon的事件 加入Click


using System.Reflection;

        private void notifyIcon1_Click(object sender, EventArgs e)

        {

            Type t = typeof(NotifyIcon);

            MethodInfo mi = t.GetMethod("ShowContextMenu", BindingFlags.NonPublic | BindingFlags.Instance);

            mi.Invoke(this.notifyIcon1, null);

        }

即可完成選單與通知

2022年4月7日 星期四

[C++]CString字符串分割

 #include <iostream>

#include <cstring> #include <atlcomtime.h> using namespace std; //最後一個參數返回的是子字符串的數量 CString * SplitString(CString str, char split, int& iSubStrs); int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) { int nRetCode = 0; // initialize MFC and print and error on failur CString str = "sssdf sfds jieuri lj122 98098"; // CString str = "sfsfsf"; CString* pStr; int iSubStrs; pStr = SplitString(str, ' ', iSubStrs); //如果子字符串的數量爲1 if (iSubStrs == 1) { //Convert CString to char char* pCh = (LPSTR)(LPCTSTR)str; printf("%s\n", pCh); } else { //輸出所有子字符串 for (int i = 0; i < iSubStrs; i++) { //Convert CString to char char* pCh = (LPSTR)(LPCTSTR)pStr[i]; printf("%s\n", pCh); } delete []pStr; } system("pause"); return nRetCode; } CString * SplitString(CString str, char split, int& iSubStrs) { int iPos = 0; //分割符位置 int iNums = 0; //分割符的總數 CString strTemp = str; CString strRight; //先計算子字符串的數量 while (iPos != -1) { iPos = strTemp.Find(split); if (iPos == -1) { break; } strRight = strTemp.Mid(iPos + 1, str.GetLength()); strTemp = strRight; iNums++; } if (iNums == 0) //沒有找到分割符 { //子字符串數就是字符串本身 iSubStrs = 1; return NULL; } //子字符串數組 iSubStrs = iNums + 1; //子串的數量 = 分割符數量 + 1 CString* pStrSplit; pStrSplit = new CString[iSubStrs]; strTemp = str; CString strLeft; for (int i = 0; i < iNums; i++) { iPos = strTemp.Find(split); //左子串 strLeft = strTemp.Left(iPos); //右子串 strRight = strTemp.Mid(iPos + 1, strTemp.GetLength()); strTemp = strRight; pStrSplit[i] = strLeft; } pStrSplit[iNums] = strTemp; return pStrSplit; }

[C++]UTF8轉wide character

 中文轉UTF8 在轉換為wide character 透過此方式轉換即可將文字轉為數字傳送出去

再透過解譯數字長度,將數字組回文字

---------------------------------------------------------------------

宣告以下資訊"

---.h

static const char trailingBytesForUTF8[256] = {
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
    2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5
};

static const unsigned int offsetsFromUTF8[6] = {
    0x00000000UL, 0x00003080UL, 0x000E2080UL,
    0x03C82080UL, 0xFA082080UL, 0x82082080UL
};

int u8_toucs(unsigned int* dest, int sz, char* src, int srcsz);
int u8_toutf8(char* dest, int sz, unsigned int* src, int srcsz);
int u8_wc_toutf8(char* dest, unsigned int ch);

-----.cpp

int u8_toucs(unsigned int* dest, int sz, char* src, int srcsz){
    unsigned int ch;
    char* src_end = src + srcsz;
    int nb;
    int i = 0;

    while (i < sz - 1) {
        nb = trailingBytesForUTF8[(unsigned char)*src];
        if (srcsz == -1) {
            if (*src == 0)
                goto done_toucs;
        }
        else {
            if (src + nb >= src_end)
                goto done_toucs;
        }
        ch = 0;
        switch (nb) {
            /* these fall through deliberately */
        case 3: ch += (unsigned char)*src++; ch <<= 6;
        case 2: ch += (unsigned char)*src++; ch <<= 6;
        case 1: ch += (unsigned char)*src++; ch <<= 6;
        case 0: ch += (unsigned char)*src++;
        }
        ch -= offsetsFromUTF8[nb];
        dest[i++] = ch;
    }
done_toucs:
    dest[i] = 0;
    return i;
}

int u8_toutf8(char* dest, int sz, unsigned int* src, int srcsz)
{
    unsigned int ch;
    int i = 0;
    char* dest_end = dest + sz;

    while (srcsz < 0 ? src[i] != 0 : i < srcsz) {
        ch = src[i];
        if (ch < 0x80) {
            if (dest >= dest_end)
                return i;
            *dest++ = (char)ch;
        }
        else if (ch < 0x800) {
            if (dest >= dest_end - 1)
                return i;
            *dest++ = (ch >> 6) | 0xC0;
            *dest++ = (ch & 0x3F) | 0x80;
        }
        else if (ch < 0x10000) {
            if (dest >= dest_end - 2)
                return i;
            *dest++ = (ch >> 12) | 0xE0;
            *dest++ = ((ch >> 6) & 0x3F) | 0x80;
            *dest++ = (ch & 0x3F) | 0x80;
        }
        else if (ch < 0x110000) {
            if (dest >= dest_end - 3)
                return i;
            *dest++ = (ch >> 18) | 0xF0;
            *dest++ = ((ch >> 12) & 0x3F) | 0x80;
            *dest++ = ((ch >> 6) & 0x3F) | 0x80;
            *dest++ = (ch & 0x3F) | 0x80;
        }
        i++;
    }
    if (dest < dest_end)
        *dest = '\0';
    return i;
}

-----------------------------------------------

Sample: 範例

char result_str2[100] = {};
uint32_t b_ucs[100] = {}; // plenty of space
int b_chars = 0;

char cbuffdata[100] = u8"哈摟哈!";
uint32_t utf_len2 = strlen(cbuffdata);
b_chars = u8_toucs(b_ucs, (utf_len2 + 1) * 4, cbuffdata, utf_len2);
//此動作已完成轉換
b_chars = u8_toutf8(result_str2, (utf_len2 + 1) * 4, b_ucs, utf_len2);
//此動作為轉換回UTF8


----可再搭配其他轉換方式將UTF8轉成ansi顯示
    CString csbuf;
    ConvertUTF8toANSI(result_str2, &csbuf);
//此函式連結

-----------------------------------------------------------------------

參考:https://www.cprogramming.com/tutorial/unicode.html

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

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