drawing a grid over an image component

Hi guys,

I want to draw a grid (the size of it can be variable) over a timage. With a '+' button I must make the grid with more lines and with a '-' button i must make it with less lines... Finally, with another button I want to show/hide the grid...

Any ideas?

best regards,
Manuel Lopez (lopem)
LVL 3
lopemAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
zebadaConnect With a Mentor Commented:
Try this code:

unit main;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, StdCtrls,
  ExtCtrls, Math;

type
  TGrid = class(TGraphicControl)
  private
    image: TImage;
    FRowSize: integer;
    FColSize: integer;
    FVisible: boolean;
    procedure SetColSize(const Value: integer);
    procedure SetRowSize(const Value: integer);
    procedure PaintGrid;
    procedure SetVisible(const Value: boolean);
  public
    constructor Create(AOwner: TComponent; AImage: TImage); reintroduce;
    procedure Paint; override;
    property RowSize: integer read FRowSize write SetRowSize;
    property ColSize: integer read FColSize write SetColSize;
    property Visible: boolean read FVisible write SetVisible;
  end;

  TImageGrid = class(TImage)
  private
    FGrid: TGrid;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    property Grid: TGrid read FGrid;
    property AutoSize;
  end;

  TfGrid = class(TForm)
    btnPlus: TButton;
    btnMinus: TButton;
    btnNone: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure btnPlusClick(Sender: TObject);
    procedure btnMinusClick(Sender: TObject);
    procedure btnNoneClick(Sender: TObject);
  private
    Image: TImageGrid;
  public
  end;

var
  fGrid: TfGrid;

implementation

{$R *.DFM}

// _________________________________________________________________________
procedure TfGrid.FormCreate(Sender: TObject);
begin
  Image := TImageGrid.Create(self);
end;

// _________________________________________________________________________
procedure TfGrid.FormDestroy(Sender: TObject);
begin
  if ( Assigned(Image) ) then
    Image.Free;
end;

// _________________________________________________________________________
procedure TfGrid.btnPlusClick(Sender: TObject);
begin
  Image.Grid.RowSize := Image.Grid.RowSize+1;
  Image.Grid.ColSize := Image.Grid.ColSize+1;
end;

// _________________________________________________________________________
procedure TfGrid.btnMinusClick(Sender: TObject);
begin
  Image.Grid.RowSize := Image.Grid.RowSize-1;
  Image.Grid.ColSize := Image.Grid.ColSize-1;
end;

// _________________________________________________________________________
procedure TfGrid.btnNoneClick(Sender: TObject);
begin
  Image.Grid.Visible := not Image.Grid.Visible;
end;

// _________________________________________________________________________
// TGrid
// _________________________________________________________________________
constructor TGrid.Create(AOwner: TComponent; AImage: TImage);
begin
  inherited Create(AOwner);
  Parent := (AOwner as TWinControl);
  image := AImage;

  Left := image.Left;
  Top := image.Top;
  Width := image.Width;
  Height := image.Height;
  FRowSize := 10;
  FColSize := 10;
  FVisible := true;
end;

// _________________________________________________________________________
procedure TGrid.SetColSize(const Value: integer);
begin
  FColSize := Max(2,Value);
  Invalidate;
end;

// _________________________________________________________________________
procedure TGrid.SetRowSize(const Value: integer);
begin
  FRowSize := Max(2,Value);
  Invalidate;
end;

// _________________________________________________________________________
procedure TGrid.SetVisible(const Value: boolean);
begin
  FVisible := Value;
  Invalidate;
end;

// _________________________________________________________________________
procedure TGrid.Paint;
begin
  inherited;
  PaintGrid;
end;

// _________________________________________________________________________
procedure TGrid.PaintGrid;
var
  r,c: integer;
begin
  if ( (FRowSize<=0) or (FColSize<=0) ) then
    exit;

  if ( not FVisible ) then
    exit;

  Canvas.Pen.Mode := pmCopy;
  Canvas.Pen.Color := clWhite;
  Canvas.Pen.Width := 1;
  Canvas.Pen.Style := psSolid;

  r := 0;
  c := 0;
  while ( r<Width ) do
  begin
    Canvas.MoveTo(r,0);
    Canvas.LineTo(r,Height);
    Inc(r,FRowSize);
  end;

  while ( c<Height ) do
  begin
    Canvas.MoveTo(0,c);
    Canvas.LineTo(Width,c);
    Inc(c,FColSize);
  end;
end;

// _________________________________________________________________________
// TImageGrid
// _________________________________________________________________________
constructor TImageGrid.Create(AOwner: TComponent);
begin
  inherited;
  Parent := (AOwner as TWinControl);
  Left := 50;
  Top := 10;
  try
    Picture.LoadFromFile('background.bmp');
    Stretch := true;
  except
    on e:Exception do
    begin
      Width := 100;
      Height := 100;
    end;
  end;
  FGrid := TGrid.Create(AOwner,self);
end;

// _________________________________________________________________________
destructor TImageGrid.Destroy;
begin
  if ( Assigned(FGrid) ) then
    FGrid.Free;
  Inherited;
end;

end.
0
 
ShagrouniCommented:
0
 
delphi3Commented:
lopem,

I know that this is just a curiosity. I have made a few changes to what
has been given by zebada: Nice work.

This makes the original background image to be controlling size.

constructor TImageGrid.Create(AOwner: TComponent);
begin
  inherited;
  Parent := (AOwner as TWinControl);
  Left := 10;  // moved it to the left
  Top := 10;
  try
    Picture.LoadFromFile('Background.bmp');
    Stretch := true;
  //except
   // on e: Exception do
    begin
      Width := Picture.Width;
      Height := Picture.Height;
      //Width := 100;
      //Height := 100;
    end;
 // end;
    FGrid := TGrid.Create(AOwner, self);
  finally
  end;
end;

and this allows a change in the picture after the original.

procedure TfGrid.FormDblClick(Sender: TObject);
begin
 // clicking any place on the open space on the form but not the picture
 // select a .bmp file
 // it will assume the size of the original background
  if OpenDialog1.Execute then
     Image.Picture.LoadFromFile(OpenDialog1.FileName);
end;
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
lopemAuthor Commented:
Thank you for your  efforts... I will look at them this weekend.

Best regards,
Manuel Lopez (lopem)
0
 
MoondancerCommented:
ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101, Netminder or Mindphaser will return to finalize these if they are still open in 7 days.  Experts, please post closing recommendations before that time.

Below are your open questions as of today.  Questions which have been inactive for 21 days or longer are considered to be abandoned and for those, your options are:
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> YOU CANNOT DELETE A QUESTION with comments; special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Click you Member Profile to view your question history and please keep them updated. If you are a KnowledgePro user, use the Power Search option to find them.  

Questions which are LOCKED with a Proposed Answer but do not help you, should be rejected with comments added.  When you grade the question less than an A, please comment as to why.  This helps all involved, as well as others who may access this item in the future.  PLEASE DO NOT AWARD POINTS TO ME.

To view your open questions, please click the following link(s) and keep them all current with updates.
http://www.experts-exchange.com/questions/Q.20103843.html
http://www.experts-exchange.com/questions/Q.20106401.html
http://www.experts-exchange.com/questions/Q.11362697.html
http://www.experts-exchange.com/questions/Q.20143988.html
http://www.experts-exchange.com/questions/Q.20171229.html
http://www.experts-exchange.com/questions/Q.20190216.html
http://www.experts-exchange.com/questions/Q.20190226.html
http://www.experts-exchange.com/questions/Q.20193039.html
http://www.experts-exchange.com/questions/Q.20195740.html
http://www.experts-exchange.com/questions/Q.20230594.html
http://www.experts-exchange.com/questions/Q.20241586.html
http://www.experts-exchange.com/questions/Q.20273100.html
http://www.experts-exchange.com/questions/Q.20281799.html
http://www.experts-exchange.com/questions/Q.20283305.html
http://www.experts-exchange.com/questions/Q.20297478.html
http://www.experts-exchange.com/questions/Q.20299749.html



*****  E X P E R T S    P L E A S E  ******  Leave your closing recommendations.
If you are interested in the cleanup effort, please click this link
http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643 
POINTS FOR EXPERTS awaiting comments are listed in the link below
http://www.experts-exchange.com/commspt/Q.20277028.html
 
Moderators will finalize this question if in @7 days Asker has not responded.  This will be moved to the PAQ (Previously Asked Questions) at zero points, deleted or awarded.
 
Thanks everyone.
Moondancer
Moderator @ Experts Exchange
0
 
MoondancerCommented:
August 24, 2001 you said you'd look at this, still open today, despite my request.  Please award and close this.
Moondancer - EE Moderator

0
 
lopemAuthor Commented:
Moondancer,

How I can split points between Zebada and Delphi3 users? I want to give an 'A'   grade...

best regards,
Manuel Lopez (lopem)

0
 
Computer101Commented:
Points reduced for split.  Comment from expert accepted as answer.  Delphi3, look for your question in this topic area

Computer101
E-E Admin
0
All Courses

From novice to tech pro — start learning today.