[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Set a TIcon image from a string variable

Posted on 2005-04-14
8
Medium Priority
?
294 Views
Last Modified: 2010-04-05
I need to be able to setup the image of a TIcon at execution time.
The content of the equivalent icon file (something.ico) will be in a string variable, but I don't want to go the steps : write the string to a file and loadfromfile method.
Is there a way ?
0
Comment
Question by:LeTay
  • 4
  • 2
  • 2
8 Comments
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 13782916
You could store the icon or a selection of icons in a .res file if you dont want to load it directly from a file.

Then you could call
Image1.Picture.Icon.Handle := LoadIcon(HInstance ,'ICON_1');

Make sure you list the res file under the {$R *.dfm}
{$R myres.res}
0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 13782988
You can also store icons in a TImageList and assign it at runtime to the index of the imagelist

var
  icTest: TIcon;
begin
  icTest := TIcon.Create;
  ImageList1.GetIcon(0, icTest);
  Application.Icon := icTest;

You can change the 0 to whatever the position of the icon in the list is
0
 

Author Comment

by:LeTay
ID: 13783049
In my case I need a string as it will be a kind of constant somewhere in a DLL
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 15

Expert Comment

by:mikelittlewood
ID: 13783063
Well if you used the .res file method, you can pass in the name of the string (without .ico)

Image1.Picture.Icon.Handle := LoadIcon(HInstance ,<YourIconName>);
0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 13783072
0
 
LVL 17

Accepted Solution

by:
geobul earned 2000 total points
ID: 13783080
Hi,

Try something like:

var
  st: TStringStream;
  s: string;
  icon: TIcon;
...
begin
  s := 'asd'; // assign the contents here

  st:= TStringStream.Create(s);
  try
   icon.LoadFromStream(st);
  finally
    st.Free;
  end;
end;

Regards, Geo
0
 

Author Comment

by:LeTay
ID: 13788682
Thank you geobul, your code works perfectly well and it is exactly what I need..
0
 
LVL 17

Expert Comment

by:geobul
ID: 13788983
You are welcome. I'm glad to hear that. Have a nice day :-)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Screencast - Getting to Know the Pipeline
Suggested Courses

830 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