Recent

Author Topic: [SOLVED] BCButton - AssignStyle from resource  (Read 601 times)

pcurtis

  • Sr. Member
  • ****
  • Posts: 377
[SOLVED] BCButton - AssignStyle from resource
« on: November 02, 2020, 07:14:58 am »
Hi All,

I am trying to assign a button style to a BCButton using

Code: Pascal  [Select][+][-]
  1. AssignFromFile('d:\close.btn');

This works fine.

My question is how to do this from a resource?

Thanks in advance.
« Last Edit: November 02, 2020, 11:39:19 am by pcurtis »
Windows 10 / Linux Mint 20
Laz 2.10.0
FPC 3.2.0

pcurtis

  • Sr. Member
  • ****
  • Posts: 377
Re: [SOLVED] BCButton - AssignStyle from resource
« Reply #1 on: November 02, 2020, 11:40:40 am »
Quote
Modify ...\bgracontrols-master\bcbutton.pas

Add  (line 268)

    procedure AssignFromResource(AResourceName: string);

After (line 267)

    procedure AssignFromFile(AFileName: string); override;

Add (line 1652)

procedure TCustomBCButton.AssignFromResource(AResourceName : string);
var
  AStream : TResourceStream;
  AButton : TBCButton;
begin
  AButton := TBCButton.Create(nil);
  try
    AStream := TResourceStream.Create(HINSTANCE, AResourceName, RT_RCDATA);
    ReadComponentFromTextStream(AStream, TComponent(AButton), OnFindClass);
    Assign(AButton);
  finally
    AStream.Free;
    AButton.Free;
  end;
end;

After (line 1635)

procedure TCustomBCButton.AssignFromFile(AFileName: string);
var
  AStream: TMemoryStream;
  AButton: TBCButton;
begin
  AButton := TBCButton.Create(nil);
  AStream := TMemoryStream.Create;
  try
    AStream.LoadFromFile(AFileName);
    ReadComponentFromTextStream(AStream, TComponent(AButton), OnFindClass);
    Assign(AButton);
  finally
    AStream.Free;
    AButton.Free;
  end;
end;
Windows 10 / Linux Mint 20
Laz 2.10.0
FPC 3.2.0

lainz

  • Hero Member
  • *****
  • Posts: 3914
  • Leandro Diaz
Re: [SOLVED] BCButton - AssignStyle from resource
« Reply #2 on: November 02, 2020, 01:19:35 pm »
Hi, please open a bug request at GitHub. GitHub is down here for me so I can't open it.

lainz

  • Hero Member
  • *****
  • Posts: 3914
  • Leandro Diaz
Re: [SOLVED] BCButton - AssignStyle from resource
« Reply #3 on: November 04, 2020, 02:35:26 am »
Hi, many thanks for the idea and the sources as well  :) I've added it in dev-bgracontrols, it will be available for the next release.

circular

  • Hero Member
  • *****
  • Posts: 3645
    • Personal webpage
Re: [SOLVED] BCButton - AssignStyle from resource
« Reply #4 on: November 04, 2020, 10:08:38 am »
If I may suggest, as you are using BGRABitmap, you could use BGRAResource. This will retrieve the resource whether it is provided as RES file (LoadFromResourceName) or as LRS file (LoadFromLazarusResource).

That would be almost the same:
Code: Pascal  [Select][+][-]
  1. procedure TCustomBCButton.AssignFromResource(AResourceName: string);
  2. var
  3.   AStream : TStream;
  4.   AButton : TBCButton;
  5. begin
  6.   AButton := TBCButton.Create(nil);
  7.   try
  8.     AStream := BGRAResource.GetResourceStream(AResourceName);
  9.     ReadComponentFromTextStream(AStream, TComponent(AButton), OnFindClass);
  10.     Assign(AButton);
  11.   finally
  12.     AStream.Free;
  13.     AButton.Free;
  14.   end;
  15. end;

And you can call it like that:
Code: Pascal  [Select][+][-]
  1. BCButton1.AssignFromResource('btnstyle');          //for RES Data resource
  2. BCButton1.AssignFromResource('btnstyle.dat');      //for RES Data resource
  3. BCButton1.AssignFromResource('btnstyle.comp');     //for LRS Resource with custom file extension
Conscience is the debugger of the mind

pcurtis

  • Sr. Member
  • ****
  • Posts: 377
Re: [SOLVED] BCButton - AssignStyle from resource
« Reply #5 on: November 04, 2020, 11:31:36 am »
Thanks. I'll keep it in mind.
Windows 10 / Linux Mint 20
Laz 2.10.0
FPC 3.2.0

lainz

  • Hero Member
  • *****
  • Posts: 3914
  • Leandro Diaz
Re: [SOLVED] BCButton - AssignStyle from resource
« Reply #6 on: November 05, 2020, 05:24:17 pm »
Thanks, I've updated the sources in BGRAControls. I will open another bug report, so we change that in all places.

 

TinyPortal © 2005-2018