VB.Net 個人不知
這裡適用的是
VB 6以下
一般來講VB要用
剪貼簿複製、貼上 的功能是
Clipboard.GetText
Clipboard.SetText
但如果你要複製的是 日文、簡中...等非 Big5碼 文字
一去一來下 就會多出幾個 ?號
因為
使用Windows 2000/XP 時
系統 <-剪貼簿-> VB APP
剪貼簿會把文字在Unicode 跟ANSI 之間轉來轉去。
如此一些在 Big5字碼裡沒有的字碼
就會變成 ?號
(比如 ウェブ全体から検索 會變成 ???全体??検索)
那如何避免呢?
此時我們選擇捨棄VB的 剪貼簿類
改用API 來個記憶體運作
先看程式
PHP 語法:
Public Function ClipboardCopy_String(hwnd As Long, strText As String, Mode As Integer)
Dim hMemory As Long 'handle
Dim lpMemory As Long '位址
If Mode = 0 Then GoTo SETCLIP
'**獲取剪貼簿裡的字串
If OpenClipboard(hwnd) Then '開啟剪貼簿
hMemory = GetClipboardData(CF_UNICODETEXT) '以UniCode文字格式獲取剪貼簿裡的字串
If hMemory Then '成功獲取的話
lpMemory = GlobalLock(hMemory) '獲取該記憶體位址 (藉由handle),並鎖住該記憶體空間
strText = Space(lstrlenW(lpMemory) * 2) '配置給字串變數 一固定長度
lstrcpyW StrPtr(strText), ByVal lpMemory '將前面記憶體內容 複製到 字串變數裡
GlobalUnlock hMemory '解鎖記憶體位址
End If
End If
GoTo EXITFUN
'**將字串放入剪貼簿裡
SETCLIP:
If OpenClipboard(hwnd) Then '如果可以開啟剪貼簿的話
EmptyClipboard '清空剪貼簿
strText = strText & vbNullChar '字串尾接一個API字串用 結束識別字元
Mode = LenB(strText) '該字串的長度
hMemory = GlobalAlloc(GHND, Mode) '配置一個 記憶體空間,並獲得 控管ID
If hMemory Then '前一行可行的話...
lpMemory = GlobalLock(hMemory) '與之前敘述雷同
lstrcpyW ByVal lpMemory, StrPtr(strText) '與前敘述雷同,StrPtr是獲取字串之記憶體位址 之VB保留字
GlobalUnlock hMemory '與前敘述雷同
SetClipboardData CF_UNICODETEXT, hMemory '設定hMemory控管的內容 給 剪貼簿,格式是UniCode文字
End If
EXITFUN:
CloseClipboard '關閉剪貼簿的使用
End If
End Function
如何使用
Dim ts As String
ClipboardCopy_String 0, ts, 1 '得到剪貼板的字 到 ts
ClipboardCopy_String 0, ts, 0 '貼ts 到 剪貼板
而
ClipboardCopy_String 0, ts, 0
可以
Clipboard.SetText ts
替換之
P.S. 因為 ts 是Unicode格式, Unicode字串傳給 剪貼板 同樣都是Unicode,正常來說不會有 ?號 字元之問題產生,當然如要避免萬一,建議直接用 ClipboardCopy_String 會比較保險
這裡有用到不少 API函式 及 常數
以下請自行比對
PHP 語法:
Public Const GMEM_SHARE = &H2000
Public Const GMEM_MOVEABLE = &H2
Public Const GMEM_ZEROINIT = &H40
Public Const GHND = (GMEM_MOVEABLE Or GMEM_ZEROINIT)
Public Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function CloseClipboard Lib "user32" () As Long
Public Declare Function EmptyClipboard Lib "user32" () As Long
Public Declare Function SetClipboardData Lib "user32" ( _
ByVal wFormat As Long, _
ByVal hMem As Long) As Long
Public Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Long) As Long
Public Declare Function GlobalAlloc Lib "kernel32" ( _
ByVal wFlags As Long, _
ByVal dwBytes As Long) As Long
Public Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Public Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
Public Declare Function lstrcpyW Lib "kernel32" ( _
ByVal lpString1 As Long, _
ByVal lpString2 As Long) As Long
Public Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Long) As Long
Public Const CF_TEXT = 1
Public Const CF_ANSIONLY = &H400&
Public Const CF_APPLY = &H200&
Public Const CF_BITMAP = 2
Public Const CF_DIB = 8
Public Const CF_DIF = 5
Public Const CF_DSPBITMAP = &H82
Public Const CF_DSPENHMETAFILE = &H8E
Public Const CF_DSPMETAFILEPICT = &H83
Public Const CF_DSPTEXT = &H81
Public Const CF_EFFECTS = &H100&
Public Const CF_ENABLEHOOK = &H8&
Public Const CF_ENABLETEMPLATE = &H10&
Public Const CF_ENABLETEMPLATEHANDLE = &H20&
Public Const CF_ENHMETAFILE = 14
Public Const CF_FIXEDPITCHONLY = &H4000&
Public Const CF_FORCEFONTEXIST = &H10000
Public Const CF_GDIOBJFIRST = &H300
Public Const CF_GDIOBJLAST = &H3FF
Public Const CF_HDROP = 15
Public Const CF_INITTOLOGFONTSTRUCT = &H40&
Public Const CF_LIMITSIZE = &H2000&
Public Const CF_LOCALE = 16
Public Const CF_MAX = 17
Public Const CF_METAFILEPICT = 3
Public Const CF_NOFACESEL = &H80000
Public Const CF_NOSCRIPTSEL = &H800000
Public Const CF_NOSIMULATIONS = &H1000&
Public Const CF_NOSIZESEL = &H200000
Public Const CF_NOSTYLESEL = &H100000
Public Const CF_NOVECTORFONTS = &H800&
Public Const CF_NOOEMFONTS = CF_NOVECTORFONTS
Public Const CF_NOVERTFONTS = &H1000000
Public Const CF_OEMTEXT = 7
Public Const CF_OWNERDISPLAY = &H80
Public Const CF_PALETTE = 9
Public Const CF_PENDATA = 10
Public Const CF_PRINTERFONTS = &H2
Public Const CF_PRIVATEFIRST = &H200
Public Const CF_PRIVATELAST = &H2FF
Public Const CF_RIFF = 11
Public Const CF_SCALABLEONLY = &H20000
Public Const CF_SCREENFONTS = &H1
Public Const CF_BOTH = (CF_SCREENFONTS Or CF_PRINTERFONTS)
Public Const CF_SCRIPTSONLY = CF_ANSIONLY
Public Const CF_SELECTSCRIPT = &H400000
Public Const CF_SHOWHELP = &H4&
Public Const CF_SYLK = 4
Public Const CF_TIFF = 6
Public Const CF_TTONLY = &H40000
Public Const CF_UNICODETEXT = 13
Public Const CF_USESTYLE = &H80&
Public Const CF_WAVE = 12
Public Const CF_WYSIWYG = &H8000