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 MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TImage and background bitmap 1

Status
Not open for further replies.

sevenmiles

Technical User
Nov 27, 2005
3
AT
I try to write a component based on TImage, and use a background bitmap for drawing. I cannot get the drawing to be the same size as the image control, it remains about 100x100 pixels even if the control is much bigger when I place it on the form.
Here is my code. I have removed everything not relevant to this problem:
Code:
interface

uses
  SysUtils, Classes, Controls, ExtCtrls, Graphics;

type
  TGraph = class(TImage)
  private
    BgBmp : TBitmap;              // Background bitmap
  protected
    procedure Paint; //override;  // Overriding makes the component crash.
    procedure PaintRequest(Sender: TObject);      // Not necessary?
  public
    constructor Create (AOwner: TComponent); override;
    destructor  Destroy; override;
  published
  end;
procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('BB', [TGraph]);
end;

constructor TGraph.Create (AOwner: TComponent);
begin
  inherited Create (AOwner);
  BgBmp := TBitmap.Create;
  OnResize := PaintRequest;                       // Not necessary?
  Paint;
end;

destructor TGraph.Destroy;
begin
  BgBmp.Free;
  inherited Destroy;
end;

procedure TGraph.PaintRequest(Sender: TObject);   // Not necessary?
begin
  invalidate;
end;

procedure TGraph.Paint;
begin
  BgBmp.Height := Height;       // *******  This does not work, ********
  BgBmp.Width := Width;         // *******  the size does not change  **
  with BgBmp, Canvas do
  begin                         // Create grid on background bitmap
    Brush.Color := clBlack;
    Rectangle (0, 0, Width - 1, Height - 1);       // Black area
    Pen.Color := clGreen;
    Rectangle (10, 10, Width - 11, Height - 11);   // Simple grid
  end;
  Picture.Assign(BgBmp);        // Copy background bitmap to screen
end;

end.
 
What's happening is this:

-your constructor is called to create your TGraph component
-the first thing the constructor does is call its inherited constructor (i.e. of TImage)
-the constructor of TImage sets the height and width to 105 pixels
-your constructor then calls your Paint method, which draws a TBitmap of the size of your TGraph, i.e 105 x 105
-after the constructor has executed Delphi will set the properties of your component that you have specified, at this point the height and width of your TGraph wil be set to the values you set at design time
-your Paint method is never called again, because you do not override the Paint method of TImage (note the compiler warning when compiling TGraph!)

if you do override the Paint method (and make 'inherited' the first line in this method) then you will see your bitmap drawn to the correct size.

Steve
 
Incidentally, your component is descended from TImage, which has a Stretch property. You can just set this property to true to make your bitmap assume the size and shape of your TGraph component

Steve
 
Steve,

Thanks a lot. Including the inherited Paint did the trick.
Using Stretch is no solution, because I want the lines to stay 1 pixel wide no matter how big the graph becomes.

Bjorn
 
Being new to Delphi, I am on a steep learning curve, and I want to round off this thread with what I have learnt.
- Do not use a class derived from TImage for this purpose. TImage is meant to display pictures, and has a Paint method that automatically executes when the picture is changed, as for example when you assign a bitmap to it. If you put your drawing routine or assignment inside Paint, you will get an endless recursion.
- Use TPaintbox instead. Then you can put your drawing routine inside the overridden Paint, and the bitmap will be correctly redrawn every time it is invalidated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top