Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting graphic at design-time

Status
Not open for further replies.

bobbie100

Programmer
Aug 29, 2003
64
GB
I need to select a bitmap image in a descendant of TGraphicControl to be displayed at design-time. TImage does this, but I don't want to use TImage (variety of reasons). I have Published the TBitmap as foolows:

type
MyGraphicControl = class(TGraphicControl)
private
FBaseBitmap: TBitmap;
...
protected
procedure SetBaseBitmap(ABitmap: TBitmap);
...
published
property BaseBitmap: TBitmap read FBaseBitmap write SetBaseBitmap;
...
end;
implementation
...
procedure MyGraphicControl.SetBaseBitmap(ABitmap: TBitmap);
begin
????????
end;

Problem is that whatever I seem to try at ?????? I get an access violation error when I select a .bmp file in the Object Inspector at design-time (using the standard graphic property editor).

Also, how do I get the bitmap displayed at design-time? Is it as simple as using
Canvas.Draw(0,0,FBaseBitmap);
in SetBaseBitmap?

Any suggestions (not too rude) would be much appreciated.
 
Sorry for bothering anyone who looked at this. I suddenly realised that I have the TImage source listing. The following test component did what I wanted, so I'll base my implementation in the application on this. However, any comments would be gratefully received.
[tt]
unit MyGraphicControl;

interface

uses
SysUtils, Classes, Controls, Graphics;

type
TMyGraphicControl = class(TGraphicControl)
private
FBaseBitmap: TBitmap;
protected
procedure SetBaseBitmap(Value: TBitmap);
procedure BaseBitmapChanged(Sender: TObject);
procedure Paint; override;
published
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property BaseBitmap: TBitmap read FBaseBitmap write SetBaseBitmap;
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('My Components', [TMyGraphicControl]);
end;

constructor TMyGraphicControl.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
{ Parent set here to support setting Transparent in BaseBitmapChanged }
Parent := AOwner as TWinControl;
FBaseBitmap := TBitmap.Create;
FBaseBitmap.OnChange := BaseBitmapChanged;
end;

destructor TMyGraphicControl.Destroy;
begin
FBaseBitmap.Free;
inherited Destroy;
end;

procedure TMyGraphicControl.BaseBitmapChanged(Sender: TObject);
begin
if csDesigning in ComponentState then
begin
inherited Canvas.Brush.Style := bsClear;
inherited Canvas.FillRect(Rect(0, 0, Width, Height));
if FBaseBitmap.Width > 0 and FBaseBitmap.Height then
begin
Width := FBaseBitmap.Width;
Height := FBaseBitmap.Height;
FBaseBitmap.Transparent := True; //Fixed setting for test purposes only
inherited Canvas.Draw(0, 0, FBaseBitmap);
end;
end;
end;

procedure TMyGraphicControl.Paint;
begin
BaseBitmapChanged(Self);
end;

procedure TMyGraphicControl.SetBaseBitmap(Value: TBitmap);
begin
FBaseBitmap.Assign(Value);
end;

end.[/tt]
 
one suggestion.....in BaseBitmapChanged there is an if statement that reads:

if FBaseBitmap.Width > 0 and FBaseBitmap.Height then

I would change it to be a little more readable :)

if (FBaseBitmap.Width > 0) and (FBaseBitmap.Height > 0) then
 
Well done BillNZ for spotting the deliberate cock-up. Your suggestion is what I meant to write.....Really!!! Looking at the code I'm amazed it worked. My only excuse is that this is gash code for testing, but many thanks for looking.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top