转载:剪贴板大观园

来源:互联网 发布:windows 3d图形编程 编辑:程序博客网 时间:2024/04/25 19:25
<script type="text/javascript"><!--google_ad_client = "pub-7042941598565911";google_ad_width = 728;google_ad_height = 90;google_ad_format = "728x90_as";google_ad_type = "image";//2007-04-04: 页首横幅728x90google_ad_channel = "5368100986";//--></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script><iframe width="728" scrolling="no" height="90" frameborder="0" allowtransparency="true" hspace="0" vspace="0" marginheight="0" marginwidth="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-7042941598565911&amp;dt=1179197246484&amp;lmt=1178868077&amp;format=728x90_as&amp;output=html&amp;channel=5368100986&amp;url=http%3A%2F%2Fwww.fortime.net%2Fhtml%2Fsystem%2F20061130%2F2049_3.html&amp;ad_type=image&amp;ref=http%3A%2F%2Fwww.google.cn%2Fsearch%3Fq%3DCString%2B%25E5%25AD%2597%25E4%25B8%25B2%26complete%3D1%26hl%3Dzh-CN%26newwindow%3D1%26start%3D10%26sa%3DN&amp;flash=9&amp;u_h=1024&amp;u_w=1280&amp;u_ah=957&amp;u_aw=1280&amp;u_cd=32&amp;u_tz=480&amp;u_his=1&amp;u_java=true&amp;u_nplug=27&amp;u_nmime=148" name="google_ads_frame"></iframe>
文章正文

剪贴板大观园

来源:翻译:陈贵敏  Keith Rule  2006-11-30 23:40:52 网友评论 0 条 字体:[大 中 小] ~我要投稿!

<script type="text/javascript"><!--google_ad_client = "pub-7042941598565911";google_ad_width = 300;google_ad_height = 250;google_ad_format = "300x250_as";google_ad_type = "text_image";//2007-03-06: 文章页300x250google_ad_channel = "7512124852";google_color_border = "F9FCFD";google_color_bg = "F9FCFD";google_color_link = "000000";google_color_text = "333333";google_color_url = "666666";//--></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script><iframe width="300" scrolling="no" height="250" frameborder="0" allowtransparency="true" hspace="0" vspace="0" marginheight="0" marginwidth="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-7042941598565911&amp;dt=1179197246578&amp;lmt=1178868077&amp;prev_fmts=728x90_as&amp;format=300x250_as&amp;output=html&amp;channel=7512124852&amp;url=http%3A%2F%2Fwww.fortime.net%2Fhtml%2Fsystem%2F20061130%2F2049_3.html&amp;color_bg=F9FCFD&amp;color_text=333333&amp;color_link=000000&amp;color_url=666666&amp;color_border=F9FCFD&amp;ad_type=text_image&amp;ref=http%3A%2F%2Fwww.google.cn%2Fsearch%3Fq%3DCString%2B%25E5%25AD%2597%25E4%25B8%25B2%26complete%3D1%26hl%3Dzh-CN%26newwindow%3D1%26start%3D10%26sa%3DN&amp;cc=100&amp;flash=9&amp;u_h=1024&amp;u_w=1280&amp;u_ah=957&amp;u_aw=1280&amp;u_cd=32&amp;u_tz=480&amp;u_his=1&amp;u_java=true&amp;u_nplug=27&amp;u_nmime=148" name="google_ads_frame"></iframe>

在我们自己的VC++ / MFC应用程序中增加剪贴板功能其实是很简单的事情。为了让你的程序可以与剪贴板对话,本文就其实现中的一些基本问题做一些实例讲解。

字串1

字串3

拷贝与粘贴文本

 

下边的源代码演示了如何将文本(包含在CString对象“source”中)拷贝到剪贴板上。 字串4

CString source;  

字串1

//put your text in source 
字串2
if(OpenClipboard()) 
字串3
{ 字串4 
         HGLOBAL clipbuffer; 字串9 
         char * buffer; 字串7 
         EmptyClipboard(); 字串8 
         clipbuffer = GlobalAlloc(GMEM_DDESHARE, source.GetLength()+1); 

字串6

         buffer = (char*)GlobalLock(clipbuffer); 

字串8

         strcpy(buffer, LPCSTR(source)); 字串6 
         GlobalUnlock(clipbuffer); 

字串1

         SetClipboardData(CF_TEXT,clipbuffer); 字串1 
         CloseClipboard(); 

字串7

} 字串2 
  

字串4

反过来,下面的代码是用来实现从剪贴板上取得文本的。 字串8

char * buffer = NULL; 
字串4
//open the clipboard 字串9 
CString fromClipboard; 
字串1
if ( OpenClipboard() )  
字串6
{ 字串7 
         HANDLE hData = GetClipboardData( CF_TEXT ); 
字串9
         char * buffer = (char*)GlobalLock( hData ); 
字串5
         fromClipboard = buffer; 

字串9

         GlobalUnlock( hData ); 字串1 
         CloseClipboard(); 

字串9

} 

字串6

  字串5

拷贝与粘贴WMF(enhanced)数据

 

你想在你的程序中往剪贴板上“画”以及向剪贴板读取图形吗?请放心,这个?D?D不难!示范代码如下,其实现的是往剪贴板上写一enhanced metafile。

字串4
if ( OpenClipboard() ) 字串1 
{ 

字串3

         EmptyClipboard(); 字串8 
  字串9 
         //create the metafile DC 字串2 
         CMetaFileDC * cDC = new CMetaFileDC(); 

字串6

         cDC->CreateEnhanced(GetDC(),NULL,NULL,"the_name"); 字串4 
  

字串9

         //call draw routine here that makes GDI calls int cDC 

字串4

  字串2 
         //close meta CMetafileDC and get its handle 字串4 
         HENHMETAFILE handle = cDC->CloseEnhanced(); 

字串4

  
字串7
         //place it on the clipboard 

字串8

         SetClipboardData(CF_ENHMETAFILE,handle); 字串6 
         CloseClipboard(); 
字串6
  字串9 
         //delete the dc 

字串1

         delete cDC; 
字串2
} 
字串1

 

字串2

好啦,该演示反过来怎么做的代码了。我们从剪贴板上取得metafile并将其画到自己的应用程序的客户区DC(设备上下文)上(仅仅是个试验而已,实际上你可能更想将它拷贝一份儿)。 字串6

if ( OpenClipboard() ) 字串4 
{ 字串7 
         //Get the clipboard data 
字串1
         HENHMETAFILE handle = (HENHMETAFILE)GetClipboardData(CF_ENHMETAFILE); 
字串7
  字串9 
         //play it into a DC (our own DC in this example) 
字串4
         CClientDC dc(this); 字串7 
         CRect client(0,0,200,200); 字串4 
         dc.PlayMetaFile(handle,client);              字串8 
  

字串2

         //close the clipboard 

字串8

         CloseClipboard(); 
字串6
} 
字串9

拷贝与粘贴一张位图(BitMap)

  字串3

拷贝和粘贴位图可是需要一些微妙的处理的,不过基本的思想还是一样。请看下面的代码。 字串2

if ( OpenClipboard() ) 
字串7
{ 字串4 
         EmptyClipboard(); 
字串5
         //create some data 字串2 
         CBitmap * junk = new CBitmap(); 
字串2
         CClientDC cdc(this); 字串8 
         CDC dc; 字串9 
         dc.CreateCompatibleDC(&cdc); 字串9 
         CRect client(0,0,200,200); 字串3 
         junk->CreateCompatibleBitmap(&cdc,client.Width(),client.Height()); 字串8 
         dc.SelectObject(junk); 
字串5
  

字串4

         //call draw routine here that makes GDI calls 字串3 
         DrawImage(&dc,CString("Bitmap")); 
字串2
  

字串6

         //put the data on the clipboard 

字串2

         SetClipboardData(CF_BITMAP,junk->m_hObject); 
字串7
         CloseClipboard(); 
字串5
  

字串4

         //copy has been made on clipboard so we can delete 字串8 
         delete junk; 字串1 
} 

字串5

  字串7 
如下示例代码是从剪贴板上取得一张位图,将它粘贴到客户区DC中。 字串5 
if ( OpenClipboard() ) 
字串8
{ 

字串5

         //Get the clipboard data 字串4 
         HBITMAP handle = (HBITMAP)GetClipboardData(CF_BITMAP); 字串7 
         CBitmap * bm = CBitmap::FromHandle(handle); 字串1 
  
字串1
         CClientDC cdc(this); 
字串9
         CDC dc; 
字串6
         dc.CreateCompatibleDC(&cdc); 字串3 
         dc.SelectObject(bm); 字串5 
         cdc.BitBlt(0,0,200,200,&dc,0,0,SRCCOPY); 

字串5

  字串7 
         CloseClipboard(); 字串6 
} 字串5 

  字串3

建立并使用你自己定做的数据格式

 

如果你要拷贝、粘贴其它格式的数据,可以用RegisterClipboardFormat() API函数先将此格式注册,然后就可以“为所欲为”了。这简直是太有用了,尤其是在我们自己的应用程序中拷贝资料。假设我们有下面的结构: 字串4

struct MyFormatData 
字串5
{ 字串6 
         long val1; 

字串1

         int val2; 
字串4
}; 
字串6

  字串2

想将此结构的数据拷贝到剪贴板上。可以这样实现: 字串7

UINT format = RegisterClipboardFormat("MY_CUSTOM_FORMAT"); 字串3 
if(OpenClipboard()) 
字串5
{ 字串2 
         //make some dummy data 字串5 
         MyFormatData data; 字串8 
         data.val1 = 100; 
字串5
         data.val2 = 200; 字串7 
  
字串9
         //allocate some global memory 
字串9
         HGLOBAL clipbuffer; 

字串7

         EmptyClipboard(); 字串4 
         clipbuffer = GlobalAlloc(GMEM_DDESHARE, sizeof(MyFormatData)); 字串8 
         MyFormatData * buffer = (MyFormatData*)GlobalLock(clipbuffer); 字串7 
  

字串7

         //put the data into that memory 字串7 
         *buffer = data; 字串5 
  
字串1
         //Put it on the clipboard 字串1 
         GlobalUnlock(clipbuffer); 
字串9
         SetClipboardData(format,clipbuffer); 

字串7

         CloseClipboard(); 字串6 
} 
字串6

 

字串9

想把它从剪贴板上读下来的话,也容易: 字串6

  字串2

//第二次调用时,此格式已经注册过了,读下来就行了 字串8 
UINT format = RegisterClipboardFormat("MY_CUSTOM_FORMAT"); 字串2 
MyFormatData data; 

字串7

if ( OpenClipboard() )  字串6 
{ 字串7 
         //get the buffer 字串5 
         HANDLE hData = GetClipboardData(format); 字串7 
         MyFormatData * buffer = (MyFormatData *)GlobalLock( hData ); 
字串1
  

字串8

         //留一份儿当地拷贝 字串2 
         data = *buffer; 

字串6

  

字串9

         GlobalUnlock( hData ); 字串9 
         CloseClipboard(); 
字串2
} 字串2 

  字串9

字串2

取得剪贴板变化通知(Getting notified of clipboard changes)

 

一旦剪贴板上的内容发生改变,我们都希望能够获知(经由windows消息),这是很有用的。你可以用函数SetClipboardViewer()来捕获WM_DRAWCLIPBOARD消息。

字串9

  
字串9
在你的初始化代码中调用: 
字串5
         SetClipboardViewer();  //add us to clipboard change notification chain 字串2 
  字串7 
在你的消息映射(message map)中添加: 字串5 
         ON_MESSAGE(WM_DRAWCLIPBOARD, OnClipChange)  //clipboard change notification 

字串7

  字串1 
将其定义为: 字串6 
         afx_msg void OnClipChange();  //clipboard change notification 字串5 
  字串8 
实现为: 字串3 
void CDetectClipboardChangeDlg::OnClipChange()  
字串7
{ 字串3 
         //do something here, for example 
字串6
         CTime time = CTime::GetCurrentTime(); 
字串2
         SetDlgItemText(IDC_CHANGED_DATE,time.Format("%a, %b %d, %Y -- %H:%M:%S")); 
字串4
  字串9 
         DisplayClipboardText(); 字串4 
} 
字串9

  字串7

将数据粘贴到其它应用程序窗口中的方法

 

我觉得如果能把文本拷贝到剪贴板上(参见上面的代码),然后再在另外一个应用程序中将这些文本粘贴过来,那样才有用。我写了一个很不错的本地应用程序,此程序使用了含有此技术的第三方的语言翻译包。很简单,仅是取得目标窗口的句柄,并向它发送“PASTE”消息就OK了。 字串9

         SendMessage(m_hTextWnd, WM_PASTE, 0, 0); 字串5 

共3页: 上一页 [1] [2] 3 下一页

[新浪ViVi] [POCO网摘] [365KEY] [百度搜藏] [天极网摘] [和讯网摘]
上一篇:在VC++通过汇编实现获取代码运行时间
下一篇:WDM驱动程序设计
用户名:新注册) 密码: 匿名评论 [所有评论]
评论内容:不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
<script type="text/javascript"><!--google_ad_client = "pub-7042941598565911";google_ad_width = 180;google_ad_height = 60;google_ad_format = "180x60_as_rimg";google_cpa_choice = "CAAQs8X8zwEaCF3TsUAWCLVXKPu_93M";google_ad_channel = "6952510771";//--></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script><iframe width="180" scrolling="no" height="60" frameborder="0" allowtransparency="true" hspace="0" vspace="0" marginheight="0" marginwidth="0" src="http://pagead2.googlesyndication.com/cpa/ads?client=ca-pub-7042941598565911&amp;cpa_choice=CAAQs8X8zwEaCF3TsUAWCLVXKPu_93M&amp;oe=GB2312&amp;dt=1179197247671&amp;lmt=1178868077&amp;format=180x60_as_rimg&amp;output=html&amp;channel=6952510771&amp;url=http%3A%2F%2Fwww.fortime.net%2Fhtml%2Fsystem%2F20061130%2F2049_3.html&amp;region=_google_cpa_region_&amp;ref=http%3A%2F%2Fwww.google.cn%2Fsearch%3Fq%3DCString%2B%25E5%25AD%2597%25E4%25B8%25B2%26complete%3D1%26hl%3Dzh-CN%26newwindow%3D1%26start%3D10%26sa%3DN&amp;cc=100&amp;flash=9&amp;u_h=1024&amp;u_w=1280&amp;u_ah=957&amp;u_aw=1280&amp;u_cd=32&amp;u_tz=480&amp;u_his=1&amp;u_java=true&amp;u_nplug=27&amp;u_nmime=148" name="google_ads_frame"></iframe>
快捷搜索
推荐文章
     
热点文章
  • ·WDM驱动程序设计
  • ·VC如何禁止自动屏保,休眠等
  • ·在C++中使用cpuid指令获得C
  • ·vc调用ExitWindowsEx关闭计
  • ·VC实现光驱、软驱、USB的禁
  • ·VC如何禁止自动屏保,休眠等
  • ·在VC6集成环境中开发设备驱
  • ·检测文件存在的三种方法
  • ·VC++中如何遍历整个目录树
  • ·文件操作API函数的介绍
相关文章
  • ·WDM驱动程序设计
  • ·在VC++通过汇编实现获取
  • ·VC++中如何遍历整个目录树
  • ·如何让应用程序只存在一个
  • ·在Visual C++中使用fopen()
  • ·VC中调用CHM帮助文件
  • ·vc调用ExitWindowsEx关闭计
  • ·VC++中进程与多进程管理的
  • ·用CFile类读取大文件
  • ·关闭计算机就这几招
   网站首页 -  网站地图 -  RSS订阅 -  高级搜索 -  友情链接 -  版权声明 -  联系我们 -  帮助中心
与时电脑知识库
我要统计 <script type="text/javascript" src="http://www.google-analytics.com/urchin.js"></script>
 
原创粉丝点击