|
發表於 2025-6-10 05:07:49
|
顯示全部樓層
非EAC 問題
係本地化問題
可以在任何 對應 跑online 貼入文字
E.G
英文系統 港跑 輸入法 為速成 再貼上
英文系統 韓跑 輸入法 為韓文 再貼上
英文系統 中國創天跑 輸入法 為簡體中文 再貼上
- public static async Task SetClipboardTextAsync(string text)
- {
- // 避免 text 為 null
- if (text == null) text = string.Empty;
- IntPtr hGlobal = IntPtr.Zero;
- try
- {
- // 打開剪貼簿
- if (!OpenClipboard(IntPtr.Zero))
- {
- return;
- }
- // 清空剪貼簿
- EmptyClipboard();
- // 將字串轉為 UTF-16 Little Endian byte[]
- byte[] unicodeBytes = Encoding.Unicode.GetBytes(text);
- // +2 bytes 作為結尾的 "\0\0"
- UIntPtr size = (UIntPtr)(unicodeBytes.Length + 2);
- // 分配全局記憶體
- hGlobal = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, size);
- if (hGlobal == IntPtr.Zero)
- {
- return;
- }
- // 鎖定記憶體
- IntPtr pGlobal = GlobalLock(hGlobal);
- if (pGlobal == IntPtr.Zero)
- {
- return;
- }
- try
- {
- // 複製 byte[] 到全局記憶體
- Marshal.Copy(unicodeBytes, 0, pGlobal, unicodeBytes.Length);
- // 寫入結尾的 2 bytes = 0, 表示 Unicode 結束字元
- Marshal.WriteInt16(pGlobal, unicodeBytes.Length, 0);
- }
- finally
- {
- // 解鎖記憶體
- GlobalUnlock(hGlobal);
- }
- // 設置剪貼簿資料
- IntPtr result = SetClipboardData(CF_UNICODETEXT, hGlobal);
- if (result == IntPtr.Zero)
- {
- // 需要自行釋放記憶體
- GlobalFree(hGlobal);
- return;
- }
- // SetClipboardData 成功後,hGlobal 所指派的記憶體由系統管理,不可再自行釋放
- hGlobal = IntPtr.Zero;
- }
- finally
- {
- CloseClipboard();
- // 如果 hGlobal 不為 0,表示上面 SetClipboardData 失敗,需要釋放
- if (hGlobal != IntPtr.Zero)
- {
- GlobalFree(hGlobal);
- }
- }
- // 防止太快執行貼上動作,可適度延遲
- await Task.Delay(150);
- }
- }
複製代碼 |
|