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

Draw Grid Question 2

Status
Not open for further replies.

Dimitrov

Programmer
Apr 19, 2004
5
BG
Hi, I want when a user sellects a cell of a draw grid an icon to be loaded into that cell. Can you help me how to do that?
 
I assume that when you say: an icon to be loaded, you mean an icon to be displayed ? In which case something like this will do the trick:

Code:
procedure TForm1.DrawGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
	Rect: TRect; State: TGridDrawState);
begin
	if gdSelected in State then
		ImageList1.Draw(DrawGrid1.Canvas,rect.Left,Rect.Top,0);
end;

hth

- fruNNik
 
Well, I really mean to be displayed. This code is helpful, but when I click another cell the image in the previous cell disappears. And I don't want it, I need the image to be there. Can I avoid it somehow?
 
How about this:

Code:
procedure TForm1.DrawGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
    Rect: TRect; State: TGridDrawState);
begin
      ImageList1.DrawDrawGrid1.Canvas,rect.Left,Rect.Top,0);
end;

;)


Cheers

- fruNNik


 
Well I've got to explain what is all about. I want to represent a twodimensional array of char via Drawgrid. For each symbol I have an icon that should be displayed instead of the symbol. So I need an empty draw grid in the beginning and when a cell is selected, the proper icon to be showed, but if I click another one, the previous to stay, so it is like if you enter the array not in the usual way bit with the mouse. So is it possible and how?
 
Hmm i dont get it quite.
What i showed you is how to display a pict in drawgrid.

You need some structure or rules to determine when to display the image and which image to display.

In the DrawGridCell YOU (as the programmer) have to decide IF you show a picture (or text for that matter) and what picture you show ...

So what you should know is:
- you want to show always the same picture, or diferent onces (depending on the cell ??)
- you want them only to show after a cell is clicked once ?

Sorry for the misunderstanding,
maybe you could draw a picture of what you mean ?

Cheers

- fruNNik

 
Well, to be more descriptive, let's say I want to make something like MapEditor. Look at this code with StringDrid:
Code:
procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol,
  ARow: Integer; var CanSelect: Boolean);
begin
  StringGrid1.Cells[acol,arow]:='a';
end;
I want to have the same effect, but with icons and drawgrid, not with chars. The problem with the first code you proposed is that if i click another cell, the previous icon disappears, while i want it to stay there, like the char stays in the stringgrid, or the object in the MapEditor does. It doesn't matter if the icon is the same or not, that's easy to change, but I can't make it to have icons in the cells, only selected by the user, that are visible at the same time.If you can't understand me again, see this:
cell1 cell2 cell3
cell4 cell5 cell6.
If a user sellects first cell1, then cell5, i want to have on the screen:
icon1 cell2 cell3
cell4 icon2 cell6.
What would you suggest?
 
Like Frunnik said,in your ondraw event you must have code that knows which cells must be drawn.

so it must be a combination of 2 things :

* in the OnSelectCell handler, store the cell coordinates in a dynamic array, if the coordinates are already there you can choose to delete them from the array (so this will remove the icon)

* in the Ondraw event check the coordinates with those from the array, if you have match, draw the icon.

a little example (which is far from complete btw)

Code:
var CellList : array of TPoint
...
procedure TForm1.DrawGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;  var CanSelect: Boolean);

var I : integer;
    TP : TPoint;
    Found : boolean;

begin
 TP.X:=ACol;
 TP.Y:=ARow;
 Found:=False;
 if High(CellList) > -1 then
  begin
   for I:=Low(CellList) to High(CellList) do
    begin
     Found:=(TP.X = CellList[I].X) and (TP.Y = CellList[I].Y);
     if Found then Break;
    end;
  end
 else
  Found:=False;
 if not Found then
  begin
   // create space in array
   SetLength(CellList, High(CellList)+2);
   // add coords to array
   CellList[High(CellList)]:=TP;
  end;
end;

procedure TForm1.DrawGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);

var I : integer;
    TP : TPoint;
    Found : boolean;

begin
 TP.X:=ACol;
 TP.Y:=ARow;
 Found:=False;
 if High(CellList) > -1 then
  begin
   for I:=Low(CellList) to High(CellList) do
    begin
     Found:=(TP.X = CellList[I].X) and (TP.Y = CellList[I].Y);
     if Found then Break;
    end;
  end
 else
  Found:=False;
 if Found then
  begin
   // draw the cell
  end;
end;

Cheers

--------------------------------------
What You See Is What You Get
 
I would take a TStringGrid (to put the index of the picture to draw in), and make it owner drawn like this:

Code:
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
	Rect: TRect; State: TGridDrawState);
var
	n: Integer;
begin
	n := StrToIntDef(StringGrid1.Cells[ACol,ARow],-1);
	if n > -0 then
		ImageList1.Draw(StringGrid1.Canvas,Rect.Left,Rect.Top,n);
end;

procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol,
	ARow: Integer; var CanSelect: Boolean);
begin
	if StringGrid1.Cells[ACol,ARow] = '' then
		StringGrid1.Cells[ACol,ARow] := '1'  // image number
	else
		StringGrid1.Cells[ACol,ARow] := '';
	StringGrid1.Invalidate;
end;

This will toggle the picture on and off.
It always draws picture 1 from the imagelist1 now, but you can change that easily by changing the number (string) assigned in TForm1.StringGrid1SelectCell

hth

- fruNNik
 
Oops (typo):

Code:
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
    Rect: TRect; State: TGridDrawState);
var
    n: Integer;
begin
    n := StrToIntDef(StringGrid1.Cells[ACol,ARow],-1);
    if n >= 0 then
        ImageList1.Draw(StringGrid1.Canvas,Rect.Left,Rect.Top,n);
end;
 
Hey guys, thank you all, I got what I need. I want to apologize about the stupid questions, but I've worked with Pascal most, I have only the basics of Delphi.
 
Nice one Frunnik,

should have thought about stringgrid :))

Cheers

--------------------------------------
What You See Is What You Get
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top