Solved

drawing a grid over an image component

Posted on 2001-08-20
8
690 Views
Last Modified: 2008-02-01
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)
0
Comment
Question by:lopem
8 Comments
 
LVL 6

Accepted Solution

by:
zebada earned 50 total points
ID: 6408359
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
 

Expert Comment

by:Shagrouni
ID: 6411832
0
 
LVL 4

Expert Comment

by:delphi3
ID: 6420525
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 3

Author Comment

by:lopem
ID: 6422574
Thank you for your  efforts... I will look at them this weekend.

Best regards,
Manuel Lopez (lopem)
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 7014782
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
 
LVL 1

Expert Comment

by:Moondancer
ID: 7094026
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
 
LVL 3

Author Comment

by:lopem
ID: 7094091
Moondancer,

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

best regards,
Manuel Lopez (lopem)

0
 
LVL 1

Expert Comment

by:Computer101
ID: 7328947
Points reduced for split.  Comment from expert accepted as answer.  Delphi3, look for your question in this topic area

Computer101
E-E Admin
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question