[SOLVED] Problem with TIcon.LoadFromResourceName lazarus / 39-Graphics ---------------------------------------------------------------------- Dibo · 2011-03-03 19:09 Hi, I can't load icon from TIcon.LoadFromResourceName or LoadFromResourceHandle method. Look at this code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);var  ico: TIcon;  r: TResourceStream;  s: String;  h: TFPResourceHandle;begin  ico := TIcon.Create;  r := TResourceStream.Create(HINSTANCE, 'STATUS_DEF', 'RT_GROUP_ICON');  try    { This method can't find resource (exception raised) }    ico.LoadFromResourceName(HINSTANCE, 'STATUS_DEF');     { This method return some handle but LoadFromResourceHandle raise SIGSEGV}    h := FindResource(HINSTANCE, 'STATUS_DEF', 'RT_GROUP_ICON');    if h>0 then ico.LoadFromResourceHandle(HINSTANCE, h);     { Only this work }    ico.LoadFromStream(r);  finally    ico.Free;    r.Free;  end;end; Am I doing something wrong? Regards ---------------------------------------------------------------------- fabienwang · 2011-03-03 19:37 implementation   {$I /path/to/your/file.lrs}   ico.LoadFromLazarusResource('name_of_icon_in_lrs_file'); ---------------------------------------------------------------------- Dibo · 2011-03-03 21:06 *lrs file is loaded corectly because with TResourceStream it works fine. Something wrong is with TIcon methods Edit: I am using .res files instead of .lrs ---------------------------------------------------------------------- Paul Ishenin · 2011-03-04 01:11 You can't load icon from resource the way you do this because icon consists of more than 1 resource. use the next code instead: Code: [Select]ico := TIcon.Create;  ico.LoadFromResourceName(HINSTANCE, 'STATUS_DEF'); If you still want to load it from the stream then look at the implementation of TCustomIcon.LoadFromResourceHandle ---------------------------------------------------------------------- Dibo · 2011-03-04 12:14 @Paul, in my code above I'm calling your methods too but they doesn't work (see comments). Only TResourceStream and ico.LoadFromStream works without errors. ---------------------------------------------------------------------- fabienwang · 2011-03-04 12:31 Dibo, have you declared ico as a TCustomIcon to use LoadFromResourceHandle as Paul explained? ---------------------------------------------------------------------- Dibo · 2011-03-04 13:55 Yes, but have this same problem. I think I just use TResourceStream, this works fine ---------------------------------------------------------------------- jixian.yang · 2011-03-04 15:39 The following two function are from the Unit Graphic: function TGraphic.GetResourceType: TResourceType; begin   Result := nil; end;                    procedure TGraphic.LoadFromResourceName(Instance: THandle; const ResName: String); var   Stream: TResourceStream;   ResType: TResourceType; begin   ResType := GetResourceType;   if ResType = nil then Exit;   Stream := TResourceStream.Create(Instance, ResName, ResType);   try     LoadFromStream(Stream);   finally     Stream.Free;   end; end;              The ResType returns nil so it is impossible to load the ICON stream. And the same function GetResourceType in the LoadFromResourceHandle function too. ---------------------------------------------------------------------- Dibo · 2011-03-04 16:00 Yes but TIcon override this function: function TIcon.GetResourceType: TResourceType; begin   Result := RT_GROUP_ICON; end; ---------------------------------------------------------------------- Paul Ishenin · 2011-03-05 01:42 Look at TApplication.Initialize - it uses the same code to load an application icon. If you see an icon of your application / main form - then it is something wrong in your code / your icon. ---------------------------------------------------------------------- jixian.yang · 2011-03-05 02:34 Thanks, I learnt it is override. Seems the RT_GROUP_ICON is not "RT_GROUP_ICON", but "ICON". So if we change "RT_GROUP_ICON" into "ICON" in res file and include Windows unit, it can be loaded correctly. RC file: STATUS_DEF   ICON    "abc.ico" STATUS_D      ICON2   "abc.ico" implementation uses Windows; {$R *.lfm} {$R abc.res} { TForm1 } procedure TForm1.Button1Click(Sender: TObject); var   ico: TIcon;   r: TResourceStream;   s: string;   h: TFPResourceHandle; begin   ico := TIcon.Create;   { Only this work }   r := TResourceStream.Create(HINSTANCE, 'STATUS_D', 'ICON2');   ico.LoadFromStream(r);   Image1.Picture.Assign(ico);   ShowMessage('TResourceStream.Create');   Image1.Canvas.FillRect(0, 0, Image1.Width, Image1.Height);   try     { This method can't find resource (exception raised) }     ico.LoadFromResourceName(HINSTANCE, 'STATUS_DEF');     Image1.Picture.Assign(ico);     ShowMessage('LoadFromResourceName');     Image1.Canvas.FillRect(0, 0, Image1.Width, Image1.Height);     { This method return some handle but LoadFromResourceHandle raise SIGSEGV}     h := FindResource(HINSTANCE, 'STATUS_DEF', RT_GROUP_ICON);     if h > 0 then       ico.LoadFromResourceHandle(HINSTANCE, h);     Image1.Picture.Assign(ico);     ShowMessage('LoadFromResourceName');     Image1.Canvas.FillRect(0, 0, Image1.Width, Image1.Height);   finally     ico.Free;     r.Free;   end; end; ---------------------------------------------------------------------- Dibo · 2011-03-05 14:48 Quote from: jixian.yang on March 05, 2011, 03:34:21 amSeems the RT_GROUP_ICON is not "RT_GROUP_ICON", but "ICON". Ehh, thats right. RT_GROUP_ICON type name misled me. Thanks, problem solved