Clipboard

出自ProgWiki
跳至導覽 跳至搜尋

Clipboard(剪貼簿),參照:『維基百科~Clipboard_(computing)

應用

JavaScript

VBA

  • 讀取剪貼簿裡的字串
Declare Function OpenClipboard Lib "User32" (ByVal hwnd As Long) As Long
Declare Function CloseClipboard Lib "User32" () As Long
Declare Function GetClipboardData Lib "User32" (ByVal wFormat As Long) As Long
Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags&, ByVal dwBytes As Long) As Long
Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
Declare Function GlobalSize Lib "kernel32" (ByVal hMem As Long) As Long
Declare Function lstrcpy Lib "kernel32" (ByVal lpString1 As Any, ByVal lpString2 As Any) As Long
 
Public Const GHND = &H42
Public Const CF_TEXT = 1
Public Const MAXSIZE = 4096
 
Function ClipBoard_GetData()
   Dim hClipMemory As Long
   Dim lpClipMemory As Long
   Dim MyString As String
   Dim RetVal As Long
 
   If OpenClipboard(0&) = 0 Then
      MsgBox "Cannot open Clipboard. Another app. may have it open"
      Exit Function
   End If
 
   ' Obtain the handle to the global memory
   ' block that is referencing the text.
   hClipMemory = GetClipboardData(CF_TEXT)
   If IsNull(hClipMemory) Then
      MsgBox "Could not allocate memory"
      GoTo OutOfHere
   End If
 
   ' Lock Clipboard memory so we can reference
   ' the actual data string.
   lpClipMemory = GlobalLock(hClipMemory)
 
   If Not IsNull(lpClipMemory) Then
      MyString = Space$(MAXSIZE)
      RetVal = lstrcpy(MyString, lpClipMemory)
      RetVal = GlobalUnlock(hClipMemory)
 
      ' Peel off the null terminating character.
      MyString = Mid(MyString, 1, InStr(1, MyString, Chr$(0), 0) - 1)
   Else
      MsgBox "Could not lock memory to copy string from."
   End If
 
OutOfHere:
 
   RetVal = CloseClipboard()
   ClipBoard_GetData = MyString
 
End Function

Windows

void _CopyToClipboard(UINT uFormat, HANDLE hMem)
  if (OpenClipboard(NULL)) 
      EmptyClipboard();
      SetClipboardData(uFormat, hMem);
      CloseClipboard();
   }
}
 
void CopyStringToClipboard(LPTSTR pStr)
{
    int size = strlen(pStr); 
 
    // Allocate a global memory object for the text. 
    HGLOBAL hglbCopy = GlobalAlloc(GMEM_MOVEABLE, (size + 1) * sizeof(TCHAR)); 
    if (hglbCopy == NULL) 
       return; 
 
    // Lock the handle and copy the text to the buffer. 
    LPTSTR pStrCopy = GlobalLock(hglbCopy); 
       memcpy(pStrCopy, pStr, size * sizeof(TCHAR)); 
       pStrCopy[size] = (TCHAR) 0;    // null character 
    GlobalUnlock(hglbCopy); 
 
   _CopyToClipboard(CF_TEXT, hglbCopy); 
}
 
void CopyBitmapToClipboard(HBITMAP hBitmap, int w, int h)
{
   // Create a new bitmap
   HBITMAP hBitmapCopy = CreateBitmap(w, h, 1, 24, NULL);
 
   // Copy the source bitmap to the new one    
   HDC srcDC = CreateCompatibleDC(GetDC(NULL));
   HDC newDC = CreateCompatibleDC(GetDC(NULL));
 
   HBITMAP srcBitmap = (HBITMAP)SelectObject(srcDC, hBitmap);
   HBITMAP newBitmap = (HBITMAP)SelectObject(newDC, hBitmapCopy);
 
   BitBlt(newDC, 0, 0, w, h, srcDC, 0, 0, SRCCOPY);
 
   SelectObject(srcDC, srcBitmap);
   SelectObject(newDC, newBitmap);
 
   DeleteDC(srcDC);
   DeleteDC(newDC);
 
   _CopyToClipboard(CF_BITMAP, hBitmapCopy); 
}