发送端代码:
if (OpenClipboard()) //打开一个剪贴板
{
CString str;
HANDLE hClip;
char *pBuf;
EmptyClipboard(); //清空剪贴板,并将剪贴板的所有权分配给打开它的窗口
GetDlgItemText(IDC_EDIT_SEND, str);
hClip = GlobalAlloc(GMEM_MOVEABLE, str.GetLength()+1); //见说明1
pBuf = (char*)GlobalLock(hClip); //见说明2
strcpy(pBuf, str);
GlobalUnlock(hClip); //decrements the lock count
SetClipboardData(CF_TEXT, hClip); //见说明3
CloseClipboard(); //关闭剪贴板,使别的窗口可以通过OpenClipboard访问剪贴板
}
接收端代码:
if (OpenClipboard())
{
if (IsClipboardFormatAvailable(CF_TEXT)) //判断剪贴板包含的数据的格式是否为CF_TEXT
{
HANDLE hClip;
char *pBuf;
hClip = GetClipboardData(CF_TEXT); //返回剪贴板中以CF_TEXT格式数据的句柄
pBuf = (char*)GlobalLock(hClip);
GlobalUnlock(hClip);
SetDlgItemText(IDC_EDIT_RECV, pBuf);
CloseClipboard();
}
}
说明:
1、The
GlobalAlloc function allocates the specified number of bytes from the heap.If the function succeeds, the return value is
a handle to the newly allocated memory object.
HGLOBAL GlobalAlloc(
UINT uFlags, // allocation attributes
SIZE_T dwBytes // number of bytes to allocate
);
-
uFlags
-
[in] Specifies how to allocate memory. If zero is specified, the default is GMEM_FIXED. This parameter can be one or more of the following values, except for the incompatible combinations that are specifically noted.
Value |
Meaning |
GHND |
Combines GMEM_MOVEABLE and GMEM_ZEROINIT. |
GMEM_FIXED |
Allocates fixed memory. The return value is a pointer. |
GMEM_MOVEABLE |
Allocates movable memory. Memory blocks are never moved in physical memory, but they can be moved within the default heap. The return value is a handle to the memory object. To translate the handle into a pointer, use the GlobalLock function. This value cannot be combined with GMEM_FIXED. |
GMEM_ZEROINIT |
Initializes memory contents to zero. |
GPTR |
Combines GMEM_FIXED and GMEM_ZEROINIT. |
2、The
GlobalLock function locks a global memory object and returns
a pointer to the first byte of the object's memory block.
The internal data structures for each memory object include a lock count that is initially zero. For movable memory objects,
GlobalLock increments the count by one, and the
GlobalUnlock function decrements the count by one. For each call that a process makes to
GlobalLock for an object, it must eventually call
GlobalUnlock. Locked memory will not be moved or discarded, unless the memory object is reallocated by using the
GlobalReAlloc function. The memory block of a locked memory object remains locked until its lock count is decremented to zero, at which time it can be moved or discarded.
Memory objects allocated with GMEM_FIXED always have a lock count of zero. For these objects, the value of the returned pointer is equal to the value of the specified handle.
If the specified memory block has been discarded or if the memory block has a zero-byte size, this function returns NULL.
Discarded objects always have a lock count of zero.
3、The
SetClipboardData function places data on the clipboard in a specified clipboard format. The window must be the current clipboard owner, and the application must have called the
OpenClipboard function. (When responding to the
WM_RENDERFORMAT and
WM_RENDERALLFORMATS messages, the clipboard owner must not call
OpenClipboard before calling
SetClipboardData.),If the function succeeds, the return value is the handle to the data.如果将该函数的第二个参数设置为
NULL,则采用剪贴板的延迟提交技术:
SetClipboardData先在内存中放置空的内存块,当提交进程结束,或者外部进程需要剪贴板的数据时,发出
WM_RENDERFORMAT and
WM_RENDERALLFORMATS messages,所以在这两个消息的处理函数中,完成实际防止数据到内存块中的操作,当然这里就没有必要在重新调用
OpenClipboard函数了。