Use clipboard [2]: Assign, hasformat

Source: Internet
Author: User
Preparations:
Place a tpanel on the form; put a timage on the tpanel; three buttons are required.
In this example:


First version Code :
Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls, extctrls; Type tform1 = Class (tform) button1: tbutton; button2: tbutton; button3: tbutton; Panel1: tpanel; image1: timage; Procedure alert (Sender: tobject); Procedure button2click (Sender: tobject); Procedure alert (Sender: tobject); end; vaR form1: tform1; implementation {$ R *. DFM} uses clipbrd; Procedure tform1.button1click (Sender: tobject); begin image1.left: = 0; image1.top: = 0; panel1.autosize: = true; image1.autosize: = true; image1.picture. loadfromfile ('C: \ temp \ test.bmp '); tbutton (sender ). caption: = 'import'; end; Procedure tform1.button2click (Sender: tobject); begin clipboard. assign (image1.picture); {put the image in image1 into the clipboard} {now you can paste it in the image software. You can use the Windows Image board to try} tbutton (sender ). caption: = 'copy'; end; Procedure tform1.button3click (Sender: tobject); var bit: tbitmap; {prepare to end the image from the clipboard with a tbitmap} X, Y: integer; begin bit: = tbitmap. create; bit. assign (Clipboard); {Get from clipboard} X: = panel1.width + panel1.left * 2; {X, Y is the position to be pasted on the form} y: = panel1.top; canvas. draw (X, Y, bit); {paste is drawn} bit. free; tbutton (sender ). caption: = 'pause'; end.
 
   
 

But nowProgramThere is also a vulnerability: if there is nothing in the clipboard, what should I paste? If the clipboard is not an image, how can I paste it?

In fact, we only use the tclipboard. hasformat function to determine whether the clipboard is an image.

Version 2 code:

Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls, extctrls; Type tform1 = Class (tform) button1: tbutton; button2: tbutton; button3: tbutton; Panel1: tpanel; image1: timage; Procedure alert (Sender: tobject); Procedure button2click (Sender: tobject); Procedure alert (Sender: tobject); end; vaR form1: tform1; implementation {$ R *. DFM} uses clipbrd; Procedure tform1.button1click (Sender: tobject); begin image1.left: = 0; image1.top: = 0; panel1.autosize: = true; image1.autosize: = true; image1.picture. loadfromfile ('C: \ temp \ test.bmp '); tbutton (sender ). caption: = 'import'; end; Procedure tform1.button2click (Sender: tobject); begin {If image1 does not have any image, do not copy it. Exit.} If image1.picture = nil then exit; clipboard. assign (image1.picture); tbutton (sender ). caption: = 'copy'; end; Procedure tform1.button3click (Sender: tobject); var bit: tbitmap; X, Y: integer; begin {if something in the current clipboard is not an image, exit.} if not clipboard. hasformat (cf_bitmap) Then exit; bit: = tbitmap. create; bit. assign (Clipboard); X: = panel1.width + panel1.left * 2; Y: = panel1.top; canvas. draw (X, Y, bit); bit. free; tbutton (sender ). caption: = 'pause'; end.
 
   
 

Now there is a new problem: the cf_bitmap constant represents the image. How does one represent other formats? How many formats can be used for clipboard?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.