Solved

drawing a grid over an image component

Posted on 2001-08-20
8
681 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi Form ownership 4 73
creating threads in delphi 1 82
Find and Replace Stream with 0s 8 57
Performance of SQL statement 37 98
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 this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

920 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

14 Experts available now in Live!

Get 1:1 Help Now