VB6中剪切至剪贴板,粘贴unicode中文出现乱码的解决方法

VB6中剪切至剪贴板,粘贴unicode中文出现乱码的解决方法

    VB6设置了CF_TEXT(ANSI)内容到Clipboard中,对于ANSI软件(如VB6中所用的TextBox控件)读取它没有问题;对于使用Unicode的软件,则受限于做复制时的输入法状态,即当复制时输入法为非中文(输入法状态栏显示的是非CN,如EN)时,粘贴后出现中文乱码。

    解决的方法一是,在剪切时,先将输入法设置在中文状态时,既可简单解决此问题。
    第二是对Clipboard应用API编程

要进行剪切扣件的控件TEXT2中代码如下:

1
2
3
4
5
6
7
Private  Sub  Text2_KeyPress(KeyAscii  As  Integer )
    If  KeyAscii = 3  Then                  ' 是Ctrl C
         Dim  bPaste  As  Boolean
         bPaste = ClipboardSetText( Me .hWnd, Text2.SelText)
         KeyAscii = 0                     '取消 Ctrl + C, 避免进行剪切至剪贴板的自动操作
     End  If
End  Sub

  参考代码 http://www.xtremevbtalk.com/general/265879-clipboard-unicode.html

复制代码
 1 Private Declare Function OpenClipboard Lib "user32" (ByVal hWnd As Long) As Long
 2 Private Declare Function SetClipboardData Lib "user32" (ByVal Format As Long, ByVal hMem As Long) As Long
 3 Private Declare Function CloseClipboard Lib "user32" () As Long
 4 Private Declare Function EmptyClipboard Lib "user32" () As Long
 5 Private Declare Function GlobalAlloc Lib "kernel32" (ByVal Flags As Long, ByVal length As Long) As Long
 6 Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
 7 Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
 8 Private Declare Sub RtlMoveMemory Lib "kernel32" (ByVal pDest As Long, ByVal pSource As Long, ByVal length As Long)
 9 
10 Private Const CF_UNICODETEXT = &HD&
11 Private Const GMEM_MOVEABLE = &O2&
12 Private Const GMEM_ZEROINIT = &O40&
13 
14 ' hWnd 一般可用Me.HWnd,也可为0
15 '  StringToCopy = "Whatever blah blah blah"
16 Function ClipboardSetText(ByVal hWnd As Long, ByVal StringToCopy As String) As Boolean
17     Dim hMem As Long, pMem As Long
18 
19     Call OpenClipboard(hWnd)
20     Call EmptyClipboard
21     
22     hMem = GlobalAlloc(GMEM_MOVEABLE Or GMEM_ZEROINIT, LenB(StringToCopy))
23     pMem = GlobalLock(hMem)
24     
25     Call RtlMoveMemory(pMem, StrPtr(StringToCopy), LenB(StringToCopy))
26     Call GlobalUnlock(hMem)
27     Call SetClipboardData(CF_UNICODETEXT, hMem)
28     Call CloseClipboard
29 End Function
复制代码

你可能感兴趣的:(VB,API函数,控件)