Solved

drawing a grid over an image component

Posted on 2001-08-20
8
675 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
Comment Utility
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
Comment Utility
0
 
LVL 4

Expert Comment

by:delphi3
Comment Utility
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
 
LVL 3

Author Comment

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

Best regards,
Manuel Lopez (lopem)
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Expert Comment

by:Moondancer
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now