views:

59

answers:

2

I created a TSkinPanel derive from TcustomControl

it has a FGraphic: TPicture.

the FGraphic is drawn on the canvas of the TSkinPanel and works fine if you load and image from the TObject Inspector.

but i doesnt won'k work on loading image on runtime "Form1.SkinPanel1.Picture.LoadFromFile('skin.bmp');

+2  A: 

If you get no error when you call Picture.LoadFromFile then chances are it worked just but your control is simply not reacting to the change. The first thing to do is to handle the Picture.OnChange event handler and do something: if you do the painting yourself simply call Invalidate(), if you're using Picture to set up some other control that in turn does the painting, do the appropriate Assign() fom OnChange.

Cosmin Prund
i was able to trap the Event just like you said. but when i do FGraphic.Assign(Picture); i got Stack overflow error
XBasic3000
what i did is doing like " SkinPanel1.Picture.LoadFromFile('skin.bmp'); SkinPanel1.Apply; " it so UGLY
XBasic3000
If you got a Stack Overflow error, you've come to the right place :-)
Svein Bringsli
What's FGraphic? Your Stack Overflow is caused by a function calling itself, directly or indirectly: I don't know what FGraphic is because you didn't post any code, but my guess is it's causing your OnChange handler to be called again, and that's going on in a loop. My other guess is, you should call your ` Apply ` method (whatever that is) from the OnChange handler, or call ` Invalidate; `. If this still doesn't work edit the question and add some code! I'd like to see the class definition and the Paint handler.
Cosmin Prund
+1  A: 

You have to use the TPicture.OnChange event, eg:

type
  TSkinPanel = class(TCustomControl)
  private
    FPicture: TPicture;
    procedure PictureChanged(Sender: TObject);
    procedure SetPicture(Value: TPicture);
  protected
    procedure Paint; override;
  public
    constructor Create(Owner: TComponent); override;
    destructor Destroy; override;
  published
    property Picture: TPicture read FPicture write SetPicture;
  end;

  constructor TSkinPanel.Create(Owner: TComponent);
  begin
    inherited;
    FPicture := TPicture.Create;
    FPicture.OnChange := PictureChanged;
  end;

  destructor TSkinPanel.Destroy;
  begin
    FPicture.Free;
    inherited;
  end;

  procedure TSkinPanel.PictureChanged(Sender: TObject);
  begin
    Invalidate;
  end;

  procedure TSkinPanel.SetPicture(Value: TPicture);
  begin
    FPicture.Assign(Value);
  end;

  procedure TSkinPanel.Paint;
  begin
    if (FPicture.Graphic <> nil) and (not FPicture.Graphic.Empty) then
    begin
      // use FPicture as needed...
    end;
  end;
Remy Lebeau - TeamB