怎么编写复制命令(30分)

  • 主题发起人 主题发起人 qsdl
  • 开始时间 开始时间
Q

qsdl

Unregistered / Unconfirmed
GUEST, unregistred user!
<br>就是在edit,memo或dbgrid中用buttonclick把一段选定字符或图片复制到剪贴板,即ctrl+C
 
edit1.SelStart := 2;<br>edit1.SelLength := 2;<br>edit1.CopyToClipboard;
 
edit1.CopyToClipboard<br>memo1.CopyToClipboard<br>dbgrid用sendmessage()<br>。
 
你看这个资料对你有没有帮助吧<br>这个技巧是参考Delphi的剪贴板类的实现来完成的。将一个流的内容放入剪贴板,<br>首先要注册你自已的格式,使用RegisterClipboardFormat()函数<br>然后做下面三步:<br>&nbsp; &nbsp; 1.创建一个内容流,并将内容写进去<br>&nbsp; &nbsp; 2.创建一个全局的内容区,并将流的内容写入<br>&nbsp; &nbsp; 3.调用ClipBoard.SetAsHandle()将内容写入剪贴板<br><br><br>将内容写入剪贴板中<br>var<br>&nbsp; hbuf &nbsp; &nbsp;: THandle;<br>&nbsp; bufptr &nbsp;: Pointer;<br>&nbsp; mstream : TMemoryStream;<br>begin<br>&nbsp; mstream := TMemoryStream.Create;<br>&nbsp; try<br>&nbsp; &nbsp; {-- 处理流的代码 --}<br>&nbsp; &nbsp; hbuf := GlobalAlloc(GMEM_MOVEABLE, mstream.size);<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; bufptr := GlobalLock(hbuf);<br>&nbsp; &nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; &nbsp; Move(mstream.Memory^, bufptr^, mstream.size);<br>&nbsp; &nbsp; &nbsp; &nbsp; Clipboard.SetAsHandle(CF_MYFORMAT, hbuf);<br>&nbsp; &nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; &nbsp; GlobalUnlock(hbuf);<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; except<br>&nbsp; &nbsp; &nbsp; GlobalFree(hbuf);<br>&nbsp; &nbsp; &nbsp; raise;<br>&nbsp; &nbsp; end;<br>&nbsp; finally<br>&nbsp; &nbsp; mstream.Free;<br>&nbsp; end;<br>end;<br>请注意不要将分配的全局缓冲区释放,这个工作由剪贴板来完成,在读出数据中<br>你应该将它复制后处理。<br><br>将剪贴板内容读出来<br>var<br>&nbsp; hbuf &nbsp; &nbsp;: THandle;<br>&nbsp; bufptr &nbsp;: Pointer;<br>&nbsp; mstream : TMemoryStream;<br>begin<br>&nbsp; hbuf := Clipboard.GetAsHandle(CF_MYFORMAT);<br>&nbsp; if hbuf &lt;&gt; 0 then begin<br>&nbsp; &nbsp; bufptr := GlobalLock(hbuf);<br>&nbsp; &nbsp; if bufptr &lt;&gt; nil then begin<br>&nbsp; &nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; &nbsp; mstream := TMemoryStream.Create;<br>&nbsp; &nbsp; &nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mstream.WriteBuffer(bufptr^, GlobalSize(hbuf));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mstream.Position := 0;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {-- 处理流的代码 --}<br>&nbsp; &nbsp; &nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mstream.Free;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; &nbsp; GlobalUnlock(hbuf);<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>end;<br>********************************************<br>gototop99 (巴蒂) :怎样拷贝 特殊记录? 200分相送!!!<br>如:拷贝<br>&nbsp; TMyddd=record<br>&nbsp; &nbsp; &nbsp;a1:string;<br>&nbsp; &nbsp; &nbsp;a2:integer;<br>&nbsp; end;<br><br>类型的记录到 剪贴板中,怎么做啊??<br><br>回复人: laihecongxi(兴哥) ( ) 信誉:100 &nbsp;2002-12-02 11:22:00 &nbsp;得分:0 <br>&nbsp;<br>&nbsp;<br>&nbsp; //delphi6测试通过<br>type<br>&nbsp; TMyddd=record<br>&nbsp; &nbsp; &nbsp;a1:string;<br>&nbsp; &nbsp; &nbsp;a2:integer;<br>&nbsp; end;<br>................<br>var <br>&nbsp; Myddd &nbsp;: &nbsp;Tmyddd;<br>&nbsp; pMyddd : ^Tmyddd;<br>procedure .............................<br>var<br>&nbsp; mHnd : Thandle;<br>begin<br>&nbsp; //记录赋值<br>&nbsp; Myddd.a1 := 'AAA';<br>&nbsp; Myddd.a2 := 222;<br>&nbsp; //存数据<br>&nbsp; OurFormat:=RegisterClipboardFormat('CF_TMyddd');<br>&nbsp; &nbsp; if OpenClipboard(Handle) then<br>&nbsp; &nbsp; &nbsp;EmptyClipboard;<br>&nbsp; mHnd := GlobalAlloc(GMEM_DDESHARE or GMEM_MOVEABLE,SizeO(TMyddd));<br>&nbsp; pMyddd := GlobalLock(mHnd);<br>&nbsp; new(pMyddd);<br>&nbsp; pMyddd^.a1 &nbsp;:= &nbsp;Myddd.a1;<br>&nbsp; pMyddd^.a2 &nbsp;:= &nbsp;Myddd.a2;<br>&nbsp; GlobalUnLock(mHnd);<br>&nbsp; SetClipboardData(OurFormat,mHnd);<br>end;<br><br>var MemberInClip : Thandle;<br>begin &nbsp;//读数据<br>&nbsp;if Clipboard.HasFormat(OurFormat) then begin<br>&nbsp;if OpenClipboard(Handle) then<br>&nbsp; begin<br>&nbsp; &nbsp; &nbsp; MemberInClip:=GetClipboardData(OurFormat);<br>&nbsp; &nbsp; &nbsp; //new(PMyddd);<br>&nbsp; &nbsp; &nbsp; //pMyddd := GlobalLock(MemberInClip);<br>&nbsp; &nbsp; &nbsp; Myddd.a1 &nbsp;:= pMyddd^.a1;<br>&nbsp; &nbsp; &nbsp; Myddd.a2 &nbsp;:= pMyddd^.a2;<br>&nbsp; GlobalUnLock(MemberInClip);<br>&nbsp; CloseClipboard();<br>&nbsp; with Memo1.Lines do begin<br>&nbsp; &nbsp;Clear;<br>&nbsp; &nbsp;Add('Clipboard has TMember data:');<br>&nbsp; &nbsp;Add(Myddd.a1);<br>&nbsp; &nbsp;Add(inttostr(Myddd.a2));<br>&nbsp; end;<br>&nbsp;end;<br>&nbsp;end; &nbsp;<br>end;<br>&nbsp; <br>
 
后退
顶部