📄 c++ builder 程序设计实用教程 - 9_1_4 建立自己的剪贴板观察程序 - 张晋 - 17xie.htm
字号:
lang=EN-US style="FONT-FAMILY: 'Times New Roman'"><o:p></o:p></SPAN></P>
<P class=a3><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'"><SPAN
style="mso-spacerun: yes">
</SPAN>Clipboard()->AsText=RichEdit1->Text;<o:p></o:p></SPAN></P>
<P class=a3><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'"><SPAN
style="mso-spacerun: yes"> </SPAN>//</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">把当前显示文本复制到剪贴板上</SPAN><SPAN
lang=EN-US style="FONT-FAMILY: 'Times New Roman'"><o:p></o:p></SPAN></P>
<P class=a3><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'"><SPAN
style="mso-spacerun: yes"> </SPAN>if
(Form1->BitBtn1->Visible)<o:p></o:p></SPAN></P>
<P class=a3><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'"><SPAN
style="mso-spacerun: yes"> </SPAN>//</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">如果</SPAN><SPAN
lang=EN-US style="FONT-FAMILY: 'Times New Roman'">BitBtn1</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">控件可见</SPAN><SPAN
lang=EN-US style="FONT-FAMILY: 'Times New Roman'"><o:p></o:p></SPAN></P>
<P class=a3><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'"><SPAN
style="mso-spacerun: yes">
</SPAN>Clipboard()->Assign(Form1->BitBtn1->Glyph);<o:p></o:p></SPAN></P>
<P class=a3><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'"><SPAN
style="mso-spacerun: yes"> </SPAN>//</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">把当前显示图像复制到剪贴板上</SPAN><SPAN
lang=EN-US style="FONT-FAMILY: 'Times New Roman'"><o:p></o:p></SPAN></P>
<P class=a3><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">}<o:p></o:p></SPAN></P>
<P class=a3><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">//-----------------------------------------------------------------------<o:p></o:p></SPAN></P>
<P class=MsoNormalIndent><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">程序说明:</SPAN></P>
<P class=MsoNormalIndent><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">在程序运行的过程中,用户用鼠标左键单击按钮“拷贝数据”时。如果显示类控件上的文本为空,那么通过语句</SPAN><SPAN
lang=EN-US>clipboard()->Assign(form1->bitbtn1->glyph)</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">来把当前按钮上的图像复制到剪贴板上。否则通过语句</SPAN><SPAN
lang=EN-US>clipboard()->AsText=Richedit1->Text</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">;把当前按钮上的文本复制到剪贴板上。</SPAN></P>
<H4><SPAN lang=EN-US>5</SPAN><SPAN
style="FONT-FAMILY: 楷体_GB2312; mso-ascii-font-family: 'Times New Roman'">.运行程序</SPAN></H4>
<P class=MsoNormalIndent><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">按照附后的源程序,添加剩余的程序代码后,选择菜单“</SPAN><SPAN
lang=EN-US>File</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">”中的“</SPAN><SPAN
lang=EN-US>Save All</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">”选项,在弹出的对话框中选择合适的文件名保存文件。然后按</SPAN><SPAN
lang=EN-US>F9</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">键运行程序,程序运行的初始画面如图</SPAN><SPAN
lang=EN-US>9-2</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">所示。</SPAN></P>
<P class=a><SPAN lang=EN-US><IMG height=189
src="C++ Builder 程序设计实用教程 - 9_1_4 建立自己的剪贴板观察程序 - 张晋 - 17xie.files/df6d600e63.jpg"
width=238 v:shapes="_x0000_i1026"></SPAN></P>
<P class=MsoCaption><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">图</SPAN><SPAN
lang=EN-US>9-2<SPAN style="mso-spacerun: yes"> </SPAN></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">程序运行的初始画面</SPAN></P>
<P class=MsoNormalIndent><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">在程序运行的过程中,用户可以通过单击各个按钮来完成对剪贴板的查看和编辑操作,比如我们在</SPAN><SPAN
lang=EN-US>Windows</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的“画笔”程序中,将一个打开的图像文件某一区域复制下来。如图</SPAN><SPAN
lang=EN-US>9-3</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">所示。</SPAN></P>
<P class=a><SPAN lang=EN-US><IMG height=240
src="C++ Builder 程序设计实用教程 - 9_1_4 建立自己的剪贴板观察程序 - 张晋 - 17xie.files/ca81964974.jpg"
width=320 v:shapes="_x0000_i1027"></SPAN></P>
<P class=MsoCaption><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">图</SPAN><SPAN
lang=EN-US>9-3<SPAN style="mso-spacerun: yes"> </SPAN></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">程序运行的初始画面</SPAN></P>
<P class=MsoNormalIndent><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">然后,回到上面设计的剪贴板程序中,单击“查看剪贴板”按钮,程序运行结果如图</SPAN><SPAN
lang=EN-US>9-4</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">所示,刚才被复制的一块区域显示在了我们自己的剪贴板查看区域中。</SPAN></P>
<P class=a><SPAN lang=EN-US><IMG height=256
src="C++ Builder 程序设计实用教程 - 9_1_4 建立自己的剪贴板观察程序 - 张晋 - 17xie.files/dc2b0f6f42.jpg"
width=321 v:shapes="_x0000_i1028"></SPAN></P>
<P class=MsoCaption><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">图</SPAN><SPAN
lang=EN-US>9-4<SPAN style="mso-spacerun: yes"> </SPAN></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">程序运算的结果</SPAN></P>
<P class=MsoNormalIndent><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">在这个示例应用程序中,不但可以观察当前剪贴板上的图像数据,同时还可以显示文本数据,这里我们就不再演示了,请读者自己制作并练习。</SPAN></P>
<P class=MsoNormalIndent><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">通过对这个程序的介绍,以下几点是应该注意的:</SPAN></P>
<P class=MsoNormalIndent><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">(</SPAN><SPAN
lang=EN-US>1</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">)提供了一种自己截获和处理剪贴板上内容的方法。读者可以根据需要进一步扩充;</SPAN></P>
<P class=MsoNormalIndent><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">(</SPAN><SPAN
lang=EN-US>2</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">)提供了响应</SPAN><SPAN
lang=EN-US>Windows</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">消息的方法。</SPAN></P>
<P class=MsoNormalIndent><SPAN lang=EN-US><SPAN
style="mso-tab-count: 1">
</SPAN></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">(</SPAN><SPAN
lang=EN-US>3</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">)最后的一点启示是:在</SPAN><SPAN
lang=EN-US>CBuilder 5</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">程序开发中巧妙应用传统的</SPAN><SPAN
lang=EN-US>Windows</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">方法</SPAN><SPAN
lang=EN-US>(</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">如消息处理、</SPAN><SPAN
lang=EN-US>API</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数等</SPAN><SPAN
lang=EN-US>)</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">仍是很有必要的。而在应用这些方法中所体现的方便之处,正是</SPAN><SPAN
lang=EN-US>CBuilder 5</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">胜过其他可视化开发工具的一个重要方面。</SPAN></P>
<P class=MsoNormalIndent><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">程序完整的源代码如下所示,供读者参考学习:</SPAN></P>
<DIV
style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 0cm; BORDER-TOP: windowtext 1pt solid; PADDING-LEFT: 0cm; PADDING-BOTTOM: 1pt; BORDER-LEFT: windowtext 1pt solid; PADDING-TOP: 1pt; BORDER-BOTTOM: windowtext 1pt solid; mso-element: para-border-div; mso-border-alt: solid windowtext .5pt">
<P class=a2
style="BORDER-RIGHT: medium none; PADDING-RIGHT: 0cm; BORDER-TOP: medium none; PADDING-LEFT: 0cm; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; PADDING-TOP: 0cm; BORDER-BOTTOM: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 1.0pt 0cm 1.0pt 0cm"><SPAN
style="FONT-FAMILY: 仿宋_GB2312; mso-ascii-font-family: 'Times New Roman'">程序清单</SPAN></P></DIV>
<P class=a3><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">//-----------------------------------------------------------------------<o:p></o:p></SPAN></P>
<P class=a3><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'">#include
<vcl.h><o:p></o:p></SPAN></P>
<P class=a3><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'">#pragma
hdrstop<o:p></o:p></SPAN></P>
<P class=a3><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'">#include
"Clipbrd.hpp"<o:p></o:p></SPAN></P>
<P class=a3><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'">#include
"Clip.h"<o:p></o:p></SPAN></P>
<P class=a3><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">//-----------------------------------------------------------------------<o:p></o:p></SPAN></P>
<P class=a3><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'">#pragma
package(smart_init)<o:p></o:p></SPAN></P>
<P class=a3><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'">#pragma
resource "*.dfm"<o:p></o:p></SPAN></P>
<P class=a3><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'">TForm1
*Form1;<o:p></o:p></SPAN></P>
<P class=a3><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">//-----------------------------------------------------------------------<o:p></o:p></SPAN></P>
<P class=a3><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'">__fastcall
TForm1::TForm1(TComponent* Owner)<o:p></o:p></SPAN></P>
<P class=a3><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'"><SPAN
style="mso-spacerun: yes"> </SPAN>:
TForm(Owner)<o:p></o:p></SPAN></P>
<P class=a3><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">{<o:p></o:p></SPAN></P>
<P class=a3><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">}<o:p></o:p></SPAN></P>
<P class=a3><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">//-----------------------------------------------------------------------<o:p></o:p></SPAN></P>
<P class=a3><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'"><o:p> </o:p></SPAN></P>
<P class=a3><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'">void
__fastcall TForm1::Button1Click(TObject *Sender)<o:p></o:p></SPAN></P>
<P class=a3><SPAN lang=EN-US
style="FONT-FAMILY: 'Times New Roman'">{<o:p></o:p></SPAN></P>
<P class=a3><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'"><SPAN
style="mso-spacerun: yes"> </SPAN>if
(Clipboard()->HasFormat(CF_TEXT))<o:p></o:p></SPAN></P>
<P class=a3><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'"><SPAN
style="mso-spacerun: yes"> </SPAN>//</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">如果剪贴板上的数据为文本格式</SPAN><SPAN
lang=EN-US style="FONT-FAMILY: 'Times New Roman'"><o:p></o:p></SPAN></P>
<P class=a3><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'"><SPAN
style="mso-spacerun: yes"> </SPAN>{<o:p></o:p></SPAN></P>
<P class=a3><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'"><SPAN
style="mso-spacerun: yes">
</SPAN>Form1->RichEdit1->Visible=true;<o:p></o:p></SPAN></P>
<P class=a3><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'"><SPAN
style="mso-spacerun: yes">
</SPAN>Form1->BitBtn1->Visible=false;<o:p></o:p></SPAN><
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -