 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
gif Guest
|
Posted: Sun Nov 23, 2003 12:06 pm Post subject: TImage object trouble |
|
|
Hi at all.
I've a panel and i want make n TImages inside this panel, so i use this to
do:
for(int loop = 0; loop < n; loop++)
{
TImage *Image;
Image->parent = panel;
Image->width = ......
....
}
now i have a function that draw inside the TImage. But the question is, how
can establish where to draw if in TImage n°1 or TImage n°X?
Thanx for helping.
|
|
| Back to top |
|
 |
JD Guest
|
Posted: Sun Nov 23, 2003 12:44 pm Post subject: Re: TImage object trouble |
|
|
"gif" <jeyweb (AT) yahoo (DOT) it> wrote:
| Quote: | I've a panel and i want make n TImages inside this panel, so
i use this to do:
for(int loop = 0; loop < n; loop++)
{
TImage *Image;
Image->parent = panel;
Image->width = ......
....
}
|
This code leaks memory. When you allocate memory, you must
keep a pointer to that memory OR you must set the Owner so
that when the owner is destroyed, the memory will be freed.
Since you want to later reference each object, you need to
save a pointer to the objects. You can then use the pointers
to delete the objects or you can let the Owner clean them up
IF you set the Owner property when you allocate them.
Assuming that you're most likely displaying the images in
columns and rows, I suggest that you use a 2D array of pointers:
int ColCount = 5, RowCount = 10;
TImage*** ImageArray = NULL;
ImageArray = new TImage**[ ColCount ];
for( int Col = 0; Col < ColCount; ++Col ) ImageArray[ Col ] = NULL;
for( int Col = 0; Col < ColCount; ++Col )
{
ImageArray[ Col ] = new TImage*[ RowCount ];
for( int Row = 0; Row < RowCount; ++Row ) ImageArray[ Col ][ Row ] = NULL;
for( int Row = 0; Row < RowCount; ++Row )
{
ImageArray[ Col ][ Row ] = new TImage( this );
...
ImageArray[ Col ][ Row ]->Parent = Panel1;
}
}
Now, using this method, if you set the Owner, you don't need
to delete the object but you still need to delete the memory
allocated to save the pointers:
for( int Col = 0; Col < ColCount; ++Col )
{
// this is optional if you set the Owner but good practice
for( int Row = 0; Row < RowCount; ++Row )
{
delete ImageArray[ Col ][ Row ];
}
delete [] ImageArray[ Col ];
}
delete [] ImageArray;
| Quote: | [...] how can establish where to draw if in TImage n°1 or
TImage n°X?
|
You could define a global TImage pointer and use the OnClick
Event to make it point to the image that was just clicked.
TImage* CurrentImage;
void __fastcall TForm1::ImageClick(TObject* Sender)
{
CurrentImage = static_cast
}
but for this to work, you'll also need to assign the OnClick
event to the TImage when you allocate it:
ImageArray[ Col ][ Row ]->OnClick = ImageClick;
~ JD
|
|
| Back to top |
|
 |
gif Guest
|
Posted: Sun Nov 23, 2003 1:44 pm Post subject: Re: TImage object trouble |
|
|
Sorry i just correct the code with:
for(int loop = 0; loop < n; loop++)
{
TImage *Image
ImageLevel = new TImage(this);
Image->parent = panel;
Image->width = ......
....
}
|
|
| Back to top |
|
 |
gif Guest
|
Posted: Sun Nov 23, 2003 2:50 pm Post subject: Re: TImage object trouble |
|
|
Is there a method at the place of using array?
i thoungh to set a name at every image during the creation then to scan
every parent of panel and serch for one of that name.Is it possible?
Infact it is possible to reffer also without needs on click event.
"JD" <nospam (AT) nospam (DOT) com> ha scritto nel messaggio
news:3fc0b9cb$1 (AT) newsgroups (DOT) borland.com...
| Quote: |
"gif" <jeyweb (AT) yahoo (DOT) it> wrote:
I've a panel and i want make n TImages inside this panel, so
i use this to do:
for(int loop = 0; loop < n; loop++)
{
TImage *Image;
Image->parent = panel;
Image->width = ......
....
}
This code leaks memory. When you allocate memory, you must
keep a pointer to that memory OR you must set the Owner so
that when the owner is destroyed, the memory will be freed.
Since you want to later reference each object, you need to
save a pointer to the objects. You can then use the pointers
to delete the objects or you can let the Owner clean them up
IF you set the Owner property when you allocate them.
Assuming that you're most likely displaying the images in
columns and rows, I suggest that you use a 2D array of pointers:
int ColCount = 5, RowCount = 10;
TImage*** ImageArray = NULL;
ImageArray = new TImage**[ ColCount ];
for( int Col = 0; Col < ColCount; ++Col ) ImageArray[ Col ] = NULL;
for( int Col = 0; Col < ColCount; ++Col )
{
ImageArray[ Col ] = new TImage*[ RowCount ];
for( int Row = 0; Row < RowCount; ++Row ) ImageArray[ Col ][
Row ] = NULL;
for( int Row = 0; Row < RowCount; ++Row )
{
ImageArray[ Col ][ Row ] = new TImage( this );
...
ImageArray[ Col ][ Row ]->Parent = Panel1;
}
}
Now, using this method, if you set the Owner, you don't need
to delete the object but you still need to delete the memory
allocated to save the pointers:
for( int Col = 0; Col < ColCount; ++Col )
{
// this is optional if you set the Owner but good practice
for( int Row = 0; Row < RowCount; ++Row )
{
delete ImageArray[ Col ][ Row ];
}
delete [] ImageArray[ Col ];
}
delete [] ImageArray;
[...] how can establish where to draw if in TImage n°1 or
TImage n°X?
You could define a global TImage pointer and use the OnClick
Event to make it point to the image that was just clicked.
TImage* CurrentImage;
void __fastcall TForm1::ImageClick(TObject* Sender)
{
CurrentImage = static_cast
}
but for this to work, you'll also need to assign the OnClick
event to the TImage when you allocate it:
ImageArray[ Col ][ Row ]->OnClick = ImageClick;
~ JD
|
|
|
| Back to top |
|
 |
JD Guest
|
Posted: Sun Nov 23, 2003 8:25 pm Post subject: Re: TImage object trouble |
|
|
"gif" <jeyweb (AT) yahoo (DOT) it> wrote:
| Quote: | Is there a method at the place of using array? [...]
|
I'm guessing that you want to associate each Image in the
array with a filename. If that's what you want to do, since
you have to maintain the list of names, use a TStringList for
that.
Then I would suggest that you use a simple array v/s a 2D
array of Images. It's a bit more complicated to postion the
Images on the Panel but everything else is easier to work with.
TStringList* NameList = NULL;
NameList = new TStringList;
// populate NameList
TImage** ImageArray = NULL;
ImageArray = new TImage*[ NameList->Count ];
for( int x = 0; x < NameList->Count; ++x ) ImageArray[ x ] = NULL;
for( int x = 0; x < NameList->Count; ++x )
{
ImageArray[ x ] = new TImage( this );
...
ImageArray[ x ]->Parent = Panel1;
}
Then to associate a name with an Image, use the StringList
IndexOf method:
TImage* CurrentImage = ImageArray[ NameList->IndexOf( "the name" ) ];
Finally, to delete the array:
for( int x = 0; x < NameList->Count; ++x ) delete ImageArray[ x ];
delete [] ImageArray;
delete NameList;
~ JD
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Sun Nov 23, 2003 10:29 pm Post subject: Re: TImage object trouble |
|
|
"gif" <jeyweb (AT) yahoo (DOT) it> wrote
| Quote: | Is there a method at the place of using array?
|
I do not understand what you are asking for.
| Quote: | i thoungh to set a name at every image during the creation
then to scan every parent of panel and serch for one of that
name.Is it possible?
|
TComponent has a FindComponent() method for that purpose.
Gambit
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Sun Nov 23, 2003 10:30 pm Post subject: Re: TImage object trouble |
|
|
"gif" <jeyweb (AT) yahoo (DOT) it> wrote
| Quote: | now i have a function that draw inside the TImage. But the
question is, how can establish where to draw if in TImage n°1
or TImage n°X?
|
It is unclear what you are asking for exactly. Please clearify. What
exactly are you trying to accomplish? Please provide more details.
Gambit
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|