[VB6]API 이용 클립보드 제어
다음은 API를 이용한 예 입니다.
Option Explicit
'-----------------------------------------------------------------------------
'Win32 API functions: related to Clipboard manipulaton
'-----------------------------------------------------------------------------
Private Declare Function OpenClipboard Lib "user32" _
(ByVal hWnd As Long) As Long
Private Declare Function CloseClipboard Lib "user32" () As Long
Private Declare Function EmptyClipboard Lib "user32" () As Long
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 SetClipboardData Lib "user32" _
(ByVal wFormat As Long, ByVal hMem As Long) As Long
'-----------------------------------------------------------------------------
'Win32 API functions: related to Memory manipulaton
'-----------------------------------------------------------------------------
Private Declare Function GlobalSize Lib "kernel32" _
(ByVal hMem 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 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
'-----------------------------------------------------------------------------
'Constants:
'-----------------------------------------------------------------------------
Private Const CF_TEXT = 1 'Constant for Text clipboard format
Private Const GMEM_SHARE = &H2000& 'Allocates memory to be used by the dynamic data
'exchange (DDE) functions for a DDE conversation.
'Unlike Windows version 3. x, this memory is not
'shared globally. However, this flag is available
'for compatibility purposes. It may be used by some
'applications to enhance the performance of DDE
'operations and should, therefore, be specified if
'the memory is to be used for DDE. Only processes that
'use DDE or the clipboard for interprocess communications
'should specify this flag.
Private Const GMEM_MOVEABLE = &H2 'Allocates movable memory. This flag cannot be combined
'with the GMEM_FIXED flag. The return value is the handle
'of the memory object. The handle is a 32-bit quantity that
'is private to the calling process. To translate the handle
'into a pointer, use the GlobalLock function.
Private Const GMEM_ZEROINIT = &H40 'Initializes memory contents to zero.
Public Function PutText(strClipText As String) As Boolean
'This function puts the strClipText string in the clipboard
Dim MemoryType As Long
Dim MemoryHeap As Long
Dim VirtualTextLocation As Long
'Initiate the function's result as False
PutText = False
'Get a handle to a memory object allocating to it the number of bytes that is required
'to store the strClipText string
MemoryType = GMEM_MOVEABLE Or GMEM_SHARE Or GMEM_ZEROINIT
MemoryHeap = GlobalAlloc(0&, LenB(strClipText)) 'LenB function returns the length
'of the strClipText string
'in bytes, i.e. the memory
'size that it requires
'Get a pointer to the first byte of the object's memory block handles by the MemoryHeap
VirtualTextLocation = GlobalLock(MemoryHeap)
'Copy the strClipText string from the local variable to the memory
CopyMemory ByVal VirtualTextLocation, ByVal strClipText, LenB(strClipText)
'Release the memory handle
GlobalUnlock MemoryHeap
'Open the clipboard in order to gain access to setting the copied data
If Not OpenClipboard(0&) = 0 Then
EmptyClipboard 'clear the existing clipboard contents
SetClipboardData CF_TEXT, MemoryHeap 'pass the Text data from the memory
'to the clipboard
CloseClipboard 'close the clipboard, releasing it for use from other
'applications
PutText = True
End If
End Function
Public Function GetText() As String
'On success it returns a string containg the text currently in the clipboard;
'on failure (and if there is no text in the clipboard) it returns a null string
Dim strClipText As String
Dim VirtualText As Long
Dim VirtualTextLocation As Long
Dim VirtualTextSize As Long
'Initiate the function's result as a null string
GetText = vbNullString
'Verify that the Clipboard contains data of Text format;
'if not exit the function returning vbNullString
If IsClipboardFormatAvailable(CF_TEXT) = 0 Then
Exit Function
End If
'Open the clipboard in order to get access to the data inside
If Not OpenClipboard(0&) = 0 Then
VirtualText = GetClipboardData(CF_TEXT) 'get a handle to the Clipboard
'data of Text format
'On failure, i.e. if there is no text, close clipboard and exit the
'function returning vbNullString. On success,proceed with passing the data
'in the strClipText local variable
If VirtualText = 0 Then
CloseClipboard
Exit Function
Else
'Get a pointer to the memory location containing the text data
VirtualTextLocation = GlobalLock(VirtualText)
'Get the memory size of the text data
VirtualTextSize = GlobalSize(VirtualText)
'Initiate a null terminated local string variable for receiving
'the Clipboard text data
strClipText = String(VirtualTextSize, 0)
'Copy the clipboard text from the memory to the local variable
CopyMemory ByVal strClipText, ByVal VirtualTextLocation, VirtualTextSize
'Release the handle to the clipboard data
GlobalUnlock VirtualText
'Close the clipboard, releasing it for use from other applications
CloseClipboard
'assign the value of the strClipText to the result of the function
'after cleaning any undesired text data
GetText = ClearClipText(strClipText)
End If
End If
End Function
Public Function ClearAll() As Boolean
'Clears all data from the Clipboard
'returns TRUE in success; FALSE on failure
ClearAll = False
If Not OpenClipboard(0&) = 0 Then
If Not EmptyClipboard = 0 Then
ClearAll = True
End If
CloseClipboard
End If
End Function
Private Function ClearClipText(strClipText As String) As String
'returns the input string after cleaning any text data at the right of the
'first Null character
Dim NullCharPos As Integer
ClearClipText = vbNullString
NullCharPos = InStr(strClipText, Chr(0))
If NullCharPos > 0 Then
ClearClipText = Left(strClipText, NullCharPos - 1)
Else
ClearClipText = strClipText
End If