GetClipboardData 始终返回 NULL

GetClipboardData 始终返回 NULL

我有以下代码:

Public Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Long) As Long
Public Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function CloseClipboard Lib "user32" () As Long

Sub test()
Dim wat As String

OpenClipboard (0&)
wat = GetClipboardData(CF_TEXT)
MsgBox wat
CloseClipboard

End Sub

这总是在消息框中返回 0 (NULL)。有人能告诉我这段代码有什么问题吗?

答案1

GetClipboardData 将传递全局内存块的句柄。您必须锁定此块并从那里检索字符串。

基于Windows API/从剪贴板检索信息根据您的问题快速给出一个肮脏的例子:

Public Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Long) As Long
Public Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function CloseClipboard Lib "user32" () 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 lstrcpy Lib "kernel32" (ByVal lpString1 As Any, ByVal lpString2 As Any) As Long
Public Const CF_TEXT = 1
Public Const MAXSIZE = 4096






Sub test()
Dim wat As String
Dim hClipMemory As Long
Dim lpClipMemory As Long
Dim Dummy As Long
If OpenClipboard(0&) <> 0 Then    
  hClipMemory = GetClipboardData(CF_TEXT)
  If Not IsNull(hClipMemory) Then
    lpClipMemory = GlobalLock(hClipMemory)
    If Not IsNull(lpClipMemory) Then
        wat = Space$(MAXSIZE)
        Dummy = lstrcpy(wat, lpClipMemory)
        GlobalUnlock (hClipMemory)
    End If
  End If      
  MsgBox wat
CloseClipboard
End If

End Sub

相关内容