?
Solved

Clipboard Q - probably asked 1000+ times before :o)

Posted on 1999-07-06
23
Medium Priority
?
276 Views
Last Modified: 2010-04-06
Hi all,
What i need to do is:
I have an handle to an icon, and i need to put it in the clipboard in few formats..
my code look something like this, and guess what - its not working :o)

 Image2.Picture.Bitmap.Handle:= ExtractIcon(hInstance, PutDoIkona, x);
     try
        if OpenClipboard(0)=true then
        begin
             EmptyClipboard();
             SetClipboardData(CF_DIB, ???); //if i put a handle ti icon - nothing happens, if i put 0 - the program like paint has problem with opening it...
        end
     except
     end;
     CloseClipboard();

what i need is an answer :

1. How to put a picture in clipboard?
2. How to put it in diferent formats, like CF_DIB, CF_METAFILEPICT, and CF_ICO?
3. Since CF_ICO is not registered format how to register it ?

I need examples for all those questions...
If 100 points is not enough i will give more :o)
Robert
0
Comment
Question by:rpetruni
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 7
  • 4
  • +1
23 Comments
 
LVL 13

Expert Comment

by:Epsylon
ID: 1386765
I am not sure what you are trying to do but just take a look at this:


uses ClipBrd;

ClipBoard.Assign(Bitmap1);  // ClipBoard is a global var, no need to initialize;

Bitmap1.Assign(Clipboard);  // is the other way around

ClipBoard.Assign(Icon1);  // copies icon

ClipBoardSetAsHandle(CF_XXX, Handle); // writes a handle to the clipboard

ClipBoard.HasFormat(CF_XXX) returns a boolean telling you if the clipboard contains a specific format.

CF_TEXT              Text
CF_BITMAP      A Windows bitmap graphic.
CF_METAFILEPICT      A Windows metafile graphic.
CF_PICTURE      An object of type TPicture.
CF_OBJECT      Any persistent object.


Check the Delphi help files for more info on TClipBoard.

0
 
LVL 2

Expert Comment

by:CalvinDay
ID: 1386766
In the clipbrd unit they register formats like this:

initialization
  CF_PICTURE := RegisterClipboardFormat('Delphi Picture');
  CF_COMPONENT := RegisterClipboardFormat('Delphi

0
 
LVL 13

Expert Comment

by:Epsylon
ID: 1386767
D*nm, that with the icon does not work. I hate dejanews...
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 1

Author Comment

by:rpetruni
ID: 1386768
Yep, it does not :-(...

Epsylon, i was trying to use windows api's for this, but your example is o.k., but i need more :-(

 - need icon in clipboard - probably a have to declare an icon as new clipboard format - HOW?
- need conversion from icon to bmp, and vice versa

CALVINDAY - i see only two lines of your post ???!!!
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 1386769
I found something about TPicture.RegisterClipboardFormat but did not get it to work..... bad docs   :o(
0
 
LVL 2

Expert Comment

by:CalvinDay
ID: 1386770
This works for metafiles too:
  ClipBoard.Assign(Image1.Picture);

0
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 1386771
Hi Robert,

The problem is (as it appears you alread know), the Clipboard does not support Icons.  The only way that you can paste an Icon into the clipboard is to first convert it to a Bitmap.  Try this:

Uses ClibBrd; {Plus the rest of your uses clauses}

var ImgFormat: Word;
    Data : THandle;
    PalInfo: HPalette;
    BMP: TBitmap;
    Ico : TIcon;

begin
   Ico := TIcon.Create;
   Ico.LoadFromFile('C:\TEST\GRAPHICS\ICON1.ICO');
   BMP := TBitmap.Create;
   BMP.Width := Ico.Width;
   BMP.Height := Ico.Height;
   BMP.Canvas.Draw(0,0, Image1.Picture.Icon);
   Clipboard.Assign(BMP);
   BMP.SaveToClipboardFormat(ImgFormat, Data, PalInfo);
   ClipBoard.SetAsHandle(ImgFormat,Data);
   BMP.Free;
   ICO.Free;
end;

This worked fine for me.  It allowed me to paste it into any application as well.

Best of luck,

Stu.

0
 
LVL 1

Author Comment

by:rpetruni
ID: 1386772
Hi all,

To Stuart,

ok, this is ico to bmp conversion that i want, but i also need to place an icon in Clipboard, thats why i was trying to do it with api calls - to declare new clipboard format, and to convert this icon to bmp, so i could place an icon and bmp in clipboard at the same time, and application will take only format it knows...

To Epsylon,

you gave me an easear way to place a things in clipboard (assign), and it helped me :o), but i did not received an ansver for declaring new clipboard format :-(

I don't know what to do with points....
I will give them to one with answer (in code) how to declare  new clipboard format, but this goes only for Epsylon & Stuart...
Since i'm on trip tomarrow, i will evaluate answers and comment on Fridey eavning :o)
C.U.
0
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 1386773
Hi Guys,

I'm not 100% sure on this, and I'm quite open to critisism (so long as it's not too bad <G>).  The reason the Clipboard can not accept an Icon as a format is because it does not have physical properties like a bitmap, DIB or JPG.  Its a handle to an image.  The handle can not, therefore, be saved in the clipboard.

As for registering a new type of format, I honestly do not think this will work for the reason I mentioned above.  You can not place something in something else if it doesn't exist in the first place.

If you run an icon editor (such as Borlands' Image Editor which is shipped with Delphi) and copy an Icon to the clipboard, it actually saves it as a bitmap.

I'll have a read up on the RegisterClipboardFormat Epsylon mentioned and see if I can work something out.  Regardless of the outcome, I'll let you both know tomorrow.

All the best,


Stuart.
0
 
LVL 1

Author Comment

by:rpetruni
ID: 1386774
hi,

if its impossible to put an icon (or anything else) in clipboard not registered before, why does this funcion exists?

The RegisterClipboardFormat function registers a new clipboard format. This format can then be used as a valid clipboard format.

UINT RegisterClipboardFormat(

    LPCTSTR  lpszFormat       // address of name string
   );      
Parameters

lpszFormat

Points to a null-terminated string that names the new format.

Return Value

If the function succeeds, the return value identifies the registered clipboard format.

My problem is taht i'm not good programer, and i don't untherstand lots of things, but i'm learning (four years ago i no nothing about computers, now i'm working with networks, hardware and tying to learn how to program :o)...

This function should register ANY clipboard format, and you could use it as long your application is activ - i think...

Robert


0
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 1386775
Robert,

The clipboard can accept many formats, its just the format has to have content.  As I said, "you can not place something in something else if it doesn't exist in the first place".

I dont know a great deal about icons either.  All I know is what I have done and experienced in the past.  This has led me to believe that there is absolutely no way of putting an icon into the clipboard unless it is first converted to a supported format.

What you COULD do is try pasting the handle of the icon into the clipboard as text, and then retrieving that later - so long as this copying and pasting is only done between your program.

I honestly cant see how this registerclipboardformat works anyway.  There is no structures passed to it which define the new clipboard content.  There must be something else to it.  I'll let you know.

Stu.


0
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 1386776
Robert,

OK.  I have played with this, and I have gotten nowhere.  I am almost 100% confident in saying that an Icon can not be pasted to the clipboard.

I think the only way you can do it is the way I said before.

I tried a combination of things, heres one of them:

var P: TPicture;
    Ico_Fmt: Word;
    Data: Cardinal;
    Pal: HPalette;

begin
   P := TPicture.Create;
   P.Icon.LoadFromFile('C:\TEST.ICO');
   Ico_Fmt := RegisterClipboardFormat('STUS ICON');
   P.RegisterClipboardFormat(Ico_Fmt, TIcon);
   Data := SetClipboardData(CF_DSPBITMAP, P.Icon.Handle);
   P.SaveToClipboardFormat(Ico_Fmt, Data, Pal);
   P.Free;
end;

This was the only way I could really see it working, and it didn't.

Epsylon:  Your try :)

Stu.

0
 
LVL 2

Expert Comment

by:CalvinDay
ID: 1386777
Sorry, text was chopped. Look at the last few lines in the clipbrd unit for registration examples. If you can't copy an icon to the clipboard, copy the bitmap instead and use your new cf_icon format. Then, when you need it, convert the bitmap to an icon. This site shows how to do that.

http://www.borland.com/devsupport/delphi/qanda/1669.html

It needs two images, so put them side by side on the clipboard. You can use GetIconInfo(Icon, IconInfo) to
get the IconInfo.hbmMask and IconInfo.hbmColor bitmaps when copying to the clipboard.


0
 
LVL 13

Expert Comment

by:Epsylon
ID: 1386778
Robert, may I ask why you want icons on the clipboard? Other apps don't know what to do with them anyway.
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 1386779
Stu, that's like what I tried. No success eighter  :o(
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 1386780
I have a solution. It puts the icon's data on the clipboard as a uuencoded string. I used Netmasters Internet component TNMUUProcessor.

procedure TForm1.Button1Click(Sender: TObject);
var s1: TMemoryStream;
    s2: TStringStream;
begin
  s1 := TMemoryStream.Create;
  s2 := TStringStream.Create('');
  Application.Icon.SaveToStream(s1);
  NMUUProcessor1.InputStream := s1;
  NMUUProcessor1.OutputStream := s2;
  NMUUProcessor1.Encode;    // convert binary to 6-bit string
  Clipboard.AsText := s2.DataString;
  s2.Free;
  s1.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
var s1: TMemoryStream;
    s2: TStringStream;
begin
  s1 := TMemoryStream.Create;
  s2 := TStringStream.Create(Clipboard.AsText);
  NMUUProcessor1.InputStream := s2;
  NMUUProcessor1.OutputStream := s1;
  NMUUProcessor1.Decode;  // convert 6-bit string back to binary
  s1.Position := 0;
  Image1.Picture.Icon.LoadFromStream(s1);
  s2.Free;
  s1.Free;
end;

0
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 1386781
Epsylon.  OK.  Im asking this in all honesty because I dont really know why Robert wants to put an Icon in the clipboard anyway, but why would you go to that length if the data is not in iconic format anyway?  It would still require a UUEDecode at the other end to be able to be used.  I'm not picking, I'm actually quite impressed with your resourcefulness, its just surely keeping the icon in some form of image format would be perferable over a text based interpretation of it?

I hope you don't think I am nit picking :)

Stu.
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 1386782
Stu, you are right. That's why I asked Robert why he needs the icon on the clipboards. The are no other apps that can do anything with it.
0
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 1386783
Yeah I know.  I cant really see why.  He surely must have a good reason, or else he wouldnt be asking.  I honestly can't find a single solitary thing on Icons and Clipboards apart from a thing called FORMATETC which was found on in a MSDN article on M$'s web site.  I couldn't really work it out, and there was no mention of any of the things they were talking about in any Delphi help file, so I didn't bother with it.

If you are interested, give it a look.  Just search their web site for FORMATETC.

Cheers,

Stu.
0
 
LVL 1

Author Comment

by:rpetruni
ID: 1386784
Ok, Guys,

i was thinking that the Clipboard is yust SOMETHING where you could put ENYTHING, in any format.... i was wrong :-)
Why i need an icon in Clipboard?
Good Question...
I'm yust trying to make an icon extracted from exe or dll available to other application in few formats AT THE SAME TIME (it should work - so it stays in win32.hlp :o) ), and application has to deceide wich one to use (again win32.hlp says so - or did i untherstandit wrong?)... - it will use first format it untherstand...
Anywy, the 90% of what i need is given by your code, and i will give you points for your efforts....
One of you (the first one) who yust answer anything will have those 100 pts, and the other one - with less luck on lottery will receive 50 points with another question yust for hem... OK?
This goes for STUART and EPSYLON...
So, here we go....
Robert
0
 
LVL 13

Accepted Solution

by:
Epsylon earned 400 total points
ID: 1386785
That's me!
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 1386786
I forgot to say: Thanks a lot Robert   :o)
0
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 1386787
You have to be quick!  Pays not to go to sleep anymore :)

Stu.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question