-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyClip.cls
130 lines (115 loc) · 4.55 KB
/
myClip.cls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "myClip"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Attribute VB_Ext_KEY = "SavedWithClassBuilder" ,"Yes"
Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
Option Explicit
Private Declare Function IsClipboardFormatAvailable Lib "user32" (ByVal wFormat As Long) As Long
Private Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Long) As Long
Private Declare Function CloseClipboard Lib "user32" () As Long
Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function EmptyClipboard Lib "user32" () As Long
Private Declare Function SetClipboardData Lib "user32" (ByVal wFormat As Long, ByVal hMem As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, source As Any, ByVal Length As Long)
Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalSize Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function lstrcpy Lib "kernel32.dll" Alias "lstrcpyW" (ByVal lpString1 As Long, ByVal lpString2 As Long) As Long
Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As Long) As Long
Private Const CF_UNICODETEXT As Long = 13
Private Const CF_TEXT = 1
Public Function getText() As String
Dim myStrPtr As Long, myLen As Long, sBuffer As String, myLock As Long
Dim useFormat As Long
#If dbg = 1 Then
On Error GoTo ErrorHandler
#End If
sBuffer = vbNullString
OpenClipboard 0&
If IsClipboardFormatAvailable(CF_UNICODETEXT) <> 0 Then
myStrPtr = GetClipboardData(CF_UNICODETEXT)
If myStrPtr <> 0 Then
myLock = GlobalLock(myStrPtr)
myLen = GlobalSize(myStrPtr)
sBuffer = String$(myLen \ 2 - 1, vbNullChar)
lstrcpy StrPtr(sBuffer), myLock
GlobalUnlock myStrPtr
End If
Else
If IsClipboardFormatAvailable(CF_TEXT) <> 0 Then
myStrPtr = GetClipboardData(CF_TEXT)
If myStrPtr <> 0 Then
myLen = lstrlen(myStrPtr)
If myLen > 0 Then
sBuffer = Space$(myLen)
CopyMemory ByVal sBuffer, ByVal myStrPtr, myLen
End If
End If
End If
End If
CloseClipboard
getText = sBuffer
#If dbg = 1 Then
ErrorHandler:
If Err.Number <> 0 Then
MsgBox "Function: getText" & vbCrLf & "Error:" & Err.Number & vbCrLf & Err.Description
Err.Clear
End If
#End If
End Function
Public Sub Clear()
OpenClipboard 0&
EmptyClipboard
CloseClipboard
End Sub
Public Function SetText(OuStr) As Boolean
Dim hData As Long
Dim lpData As Long
Dim Buffer() As Byte
#If dbg = 1 Then
On Error GoTo ErrorHandler
#End If
WorkingNow = True
If OpenClipboard(0&) Then
' Convert data to ANSI byte array.
Buffer = StrConv(OuStr & vbNullChar, vbFromUnicode)
hData = GlobalAlloc(GMEM_FIXED, UBound(Buffer) + 1)
If hData Then
' Copy data to alloc'd memory.
lpData = GlobalLock(hData)
Call CopyMemory(ByVal lpData, Buffer(0), UBound(Buffer) + 1)
Call GlobalUnlock(hData)
SetText = CBool(SetClipboardData(CF_TEXT, hData))
End If
' Place Unicode text on clipboard, too.
' Not strictly necessary, as Windows will
' convert by default, with above code.
' Already null-terminated, so just
' allocate sufficient space for copy.
hData = GlobalAlloc(GMEM_FIXED, LenB(OuStr) + 2)
If hData Then
' Copy data to alloc'd memory.
lpData = GlobalLock(hData)
Call CopyMemory(ByVal lpData, ByVal StrPtr(OuStr), LenB(OuStr) + 2)
Call GlobalUnlock(hData)
' Hand data off to clipboard
Call SetClipboardData(CF_UNICODETEXT, hData)
End If
Call CloseClipboard
End If
WorkingNow = False
#If dbg = 1 Then
ErrorHandler:
If Err.Number <> 0 Then
MsgBox "Function: SetText" & vbCrLf & "Error:" & Err.Number & vbCrLf & Err.Description
Err.Clear
End If
#End If
End Function