一个解决VB6中剪贴板Clipboard编程出现中文乱码的方法
最近做个小程序,发现VB6使用剪贴板的一个问题:
问题是:*有时*粘贴到记事本等其它软件中时会出现中文乱码,但复制到VB6或VB6生成的软件中一般不会有乱码。
问题的实质是:上述代码设置了CF_TEXT(ANSI)内容到Clipboard中,对于ANSI软件(如VB6中所用的TextBox控件)读取它没有问题;对于使用Unicode的软件,则受限于做复制时的
输入法状态,即当复制时输入法为非中文(输入法状态栏显示的是非CN,如EN)时,粘贴后出现中文乱码。所以是“有时”出现乱码而有时正常。
在VB6中复制一个字符串到Clipboard中一般用:
Clipboard.Clear
Clipboard.SetText Text1.Text
答案:
用ANSI复制,则会根据输入法状态在UNICODE软件中出现中文乱码问题;如果用UNICODE复制,则无法粘贴到ANSI软件中(如VB6制作的软件中出现"????")。
所以我想到:两种格式一起设置,粘贴时可以各取所需!
Private Declare Function OpenClipboard Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function SetClipboardData Lib "user32" (ByVal Format As Long, ByVal hMem As Long) As Long
Private Declare Function CloseClipboard Lib "user32" () As Long
Private Declare Function EmptyClipboard Lib "user32" () As Long
Private Declare Function GlobalAlloc Lib "kernel32" (ByVal Flags As Long, ByVal length As Long) As Long
Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal pDest As Long, ByVal pSource As Long, ByVal length As Long)
Private Const CF_TEXT = 1
Private Const CF_UNICODETEXT = 13
Private Const GHND = &H42&
' hWnd 一般可用Me.HWnd,也可为0
Public Function ClipboardSetText(ByVal hWnd As Long, ByVal sText$) As Boolean
Dim hMem As Long, pMem As Long, s$
Dim bOk As Boolean
If OpenClipboard(hWnd) = 0 Then Exit Function
EmptyClipboard
' ANSI
s = StrConv(sText, vbFromUnicode)
hMem = GlobalAlloc(GHND, LenB(s) + 1)
pMem = GlobalLock(hMem)
CopyMemory ByVal pMem, ByVal StrPtr(s), LenB(s)
GlobalUnlock hMem
bOk = SetClipboardData(CF_TEXT, hMem) <> 0
If Not bOk Then GlobalFree hMem
' UNICODE:
hMem = GlobalAlloc(GHND, LenB(sText) + 2)
pMem = GlobalLock(hMem)
CopyMemory ByVal pMem, ByVal StrPtr(sText), LenB(sText)
GlobalUnlock hMem
bOk = SetClipboardData(CF_UNICODETEXT, hMem) <> 0
If Not bOk Then GlobalFree hMem
CloseClipboard
ClipboardSetText = bOk
End Function
上一个:vb6中的textbox文本框 控件如何自动换行
下一个:asp中是用chr函数将数字转换为字母的方法