当前位置:编程学习 > VC++ >>

进程间通信(剪切板)(附实例)

By 闲鸟归来 , Posted in 死亡笔记
01 /*...客户端*/
02 void CClipboardClientDlg::OnBtnCopytext()
03 {
04     // TODO: Add your control notification handler code here
05     UpdateData(TRUE);    // 更新
06     if ( m_strMessage.IsEmpty() )
07     {
08         ::AfxMessageBox("文本框的内容不能为空~");
09         return ;
10     }
11     BOOL bSucceed = OpenClipboard();    // 返回0代表打开剪切板失败~
12     if ( !bSucceed )
13     {
14         return ;
15     }
16     EmptyClipboard();    // 清空剪切板并释放剪切板内数据的句柄
17     HANDLE hMemClip = GlobalAlloc(GMEM_MOVEABLE, m_strMessage.GetLength() + 1);    // 指定如何分配内存
18     TCHAR* pBuffer = (TCHAR*)GlobalLock(hMemClip);    // 目的是为了保证内存管理时真的是用【内存】而不是【虚拟内存的磁盘镜像】,否则效率会降低
19     strcpy(pBuffer, m_strMessage);    // m_strMessage对应IDC_EDIT
20     GlobalUnlock(hMemClip);    // 解除锁定的内存块,使指向该内存块的指针无效
21     SetClipboardData(CF_TEXT, hMemClip);
22     CloseClipboard();    // 关闭剪切板
23     ::AfxMessageBox("剪切数据成功~");
24 }
25
26 如图:

27
28
29 /*...服务端*/
30 void CClipboardServerDlg::OnBtnPaste()
31 {
32     // TODO: Add your control notification handler code here
33     BOOL bSucceed = OpenClipboard();
34     if ( !bSucceed )
35     {
36         return ;
37
38     }
39     if ( !IsClipboardFormatAvailable(CF_TEXT) )
40     {
41         return ;
42     }
43     HANDLE hMemClip = GetClipboardData(CF_TEXT);
44     TCHAR* pBuffer = (TCHAR*)GlobalLock(hMemClip);
45     GlobalUnlock(hMemClip);
46     m_strMessage = pBuffer;
47     CloseClipboard();
48
49     UpdateData(FALSE);
50 }
51
52 如图:
 
 

补充:软件开发 , Vc ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,