BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Need Label component.

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Usage)
View previous topic :: View next topic  
Author Message
metal.c
Guest





PostPosted: Sun May 29, 2005 8:51 pm    Post subject: Need Label component. Reply with quote




Hello.
I´m looking for a 'Label' component that has OnMouseOver, and OnMouseOut events. Can someone tell me where I may find a 'free' one?



Back to top
Vladimir Stefanovic
Guest





PostPosted: Sun May 29, 2005 9:02 pm    Post subject: Re: Need Label component. Reply with quote



Look at BCB6, it has one "free" called TLabel, unfortunately
they named them OnMouseEnter and OnMouseLeave ;)


--
Best regards,
Vladimir Stefanovic
"metal.c" <no (AT) spam (DOT) com> wrote

Quote:

Hello.
I´m looking for a 'Label' component that has OnMouseOver, and OnMouseOut
events. Can someone tell me where I may find a 'free' one?






Back to top
metal.c
Guest





PostPosted: Sun May 29, 2005 10:13 pm    Post subject: Re: Need Label component. Reply with quote




I don't have BCB6. I use BCB5...
Back to top
Vladimir Stefanovic
Guest





PostPosted: Mon May 30, 2005 4:08 am    Post subject: Re: Need Label component. Reply with quote

BCB 1, 3, 4 & 5 also has TLabel.


--
Best regards,
Vladimir Stefanovic
"metal.c" <no (AT) spam (DOT) com> wrote

Quote:

I don't have BCB6. I use BCB5...



Back to top
Vladimir Stefanovic
Guest





PostPosted: Mon May 30, 2005 4:13 am    Post subject: Re: Need Label component. Reply with quote

It's easy to make OnMouseEnter & OnMouseLeave events
for a component even if they do not exist:

http://tinyurl.com/8km5y



--
Best regards,
Vladimir Stefanovic
"metal.c" <no (AT) spam (DOT) com> wrote

Quote:

I don't have BCB6. I use BCB5...



Back to top
JD
Guest





PostPosted: Mon May 30, 2005 4:41 am    Post subject: Re: Need Label component. Reply with quote


"metal.c" <no (AT) spam (DOT) com> wrote:
Quote:


Please wrap your lines when you post. Your editor may visually
wrap them for you but you need to enter a hard return at the
end of each line.

Quote:
I´m looking for a 'Label' component that has OnMouseOver, and OnMouseOut events. Can someone tell me where I may find a 'free' one?

Please see Vladimire's post and check if your version supports
the OnMouseEnter/Leave events. If it does not, you can
subclass the TLabel's WndProc method to account for these
events:

//--- in the header -------------------------------------------
private: // User declarations
TWndMethod OldLabelWinProc;
void __fastcall NewLabelWinProc( TMessage& );
public: // User declarations
__fastcall ~TForm1(); // add a definition for the destructor


//--- in the unit ---------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
OldLabelWinProc = Label1->WindowProc;
Label1->WindowProc = NewLabelWndProc;
}
//-------------------------------------------------------------
__fastcall TForm1::~TForm1()
{
Label1->WindowProc = OldLabelWndProc;
}
//-------------------------------------------------------------
void __fastcall TForm1::NewLabelWndProc( TMessage &Message )
{
if( Message.Msg == CM_MOUSEENTER )
{
//
}
else if( Message.Msg == CM_MOUSELEAVE )
{
//
}
OldLabelWndProc( Message );
}
//-------------------------------------------------------------

If you have multiple labels that you want to do this for, then
you need a seperate TWndMethod variable for each TLabel and
you must assign and reassign them seperately. In this case,
it would be better to subclass the TLabel and override the WinProc method instead. Just add a new class to your project.

Click: File | New | Other

Then make the new class look like this:

//--- in the header -------------------------------------------
#ifndef Unit2H // what ever you names it as
#define Unit2H
//-------------------------------------------------------------
#include <Classes.hpp>
#include <StdCtrls.hpp>
//-------------------------------------------------------------
class TMyLabel : public TLabel
{
protected:
virtual void __fastcall WndProc( TMessage &Message );
private:
typedef TLabel inherited;
void __fastcall DoMouseEnter();
void __fastcall DoMouseLeave();
public:
__fastcall TMyLable( TComponent *Owner );
};
//-------------------------------------------------------------
#endif


//--- in the unit ---------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Unit2.h" // what ever you named it as

//-------------------------------------------------------------
__fastcall TMyLabel::TMyLabel( TComponent *Owner ) : TLabel( Owner )
{
}
//-------------------------------------------------------------
void __fastcall TMyLabel::WndProc( TMessage &Message )
{
switch( Message.Msg )
{
case: CM_MOUSEENTER: DoMouseEnter(); break;
case: CM_MOUSELEAVE: DoMouseLeave(); break;
}
inherited::WndProc( Message ); // dispatch the message to the base class handler
}
//-------------------------------------------------------------
void __fastcall TMyLabel::DoMouseEnter()
{
//
}
//-------------------------------------------------------------
void __fastcall TMyLabel::DoMouseLeave()
{
//
}
//-------------------------------------------------------------


Then to use your new class, all you have to do is dynamically
allocate it at runtime. For any form that uses the new label:

//--- in the header -------------------------------------------
#include "Unit2.h" // what ever you named it as
class TForm1 : public TForm
{
....
private:
TMyLabel *pLabel;
public:
__fastcall ~TForm1();


//--- in the unit ---------------------------------------------
__fastcall TForm1::TForm1( TComponent *Owner ) : TForm( Owner )
{
pLabel = new TMyLabel( this );
// assign other properties as needed
}
//-------------------------------------------------------------
__fastcall TForm1::~TForm1()
{
delete pLabel;
}
//-------------------------------------------------------------

Note that the delete statement is not needed. In this case,
you want the label to live as long as the form (from start to
finish) so it's a good isea to let the form delete the object.
This is accomplished by assigning the Owner property when you
allocate it using:

pLabel = new TMyLabel( this );

The 'this' is a pointer to the class which ever it is used in.
In the above, it's used within the context if the class Form1
so 'this' evaluates to that particular instance of Form1. The
'this' is passed in as the Owner of the object, and as Owner,
it is responsible for it's memory management. IOW, because
Form1 is the Owner of the new TMyLable, it will destroy the
object when it is destroyed which means that as long as you
assign an Owner, you don't have to delete it to prevent memory
leaks.

If you look again at allocating the TMyLabel, you should notice
that you still need to handle each label seperately. This means
that you also have to set each label's properties seperately:

pLabel->Parent = Panel1;
pLabel->Left = 10;
pLabel->Top = 10;
pLabel->Caption = "Some Caption";

you don't gain much by subclassing the TLabel ... unless you
modify your TMyLabel class just a bit to accept the other
properties as parameters. At a minimum, the Label needs an
Owner (not always - it's complicated), a Parent, the Left and
Top, and the Caption set (the rest of the properties have
defaults set when allocated and should be changed as needed).

Of the 5 parameters, you can drop the Parent (or the Owner)
because you can use the same value as the Owner (or the
Parent). The Parent is where you want the object to be
displayed on-screen and Left and Top are relative to the
Parent.

While it's common to assign the Owner as the class that
allocated the new object, there is no reason why the Parent
can not be the same as the Owner. In fact, I prefere them to
be the same because when an object is destroyed, in addition
to destroying it's Owned objects, it also destroys it's
Parented objects and each of these objects subsequently
destroy any objects that they are Parented to that they Own
and so on.

So ... just pass the desired Parent as the Owner and then
include the other parameters:

//--- in the header -------------------------------------------
public:
__fastcall TMyLabel::TMyLabel( TComponent* Owner, int ALeft, int ATop, AnsiString ACaption );

//--- in the unit ---------------------------------------------
__fastcall TMyLabel::TMyLabel( TComponent* Owner, int ALeft, int ATop, AnsiString ACaption ) : TLabel( Owner )
{
Parent = Owner;
Left = ALeft;
Top = ATop;
Caption = ACaption;
}
//-------------------------------------------------------------

Now when you allocate a TMyLabel, you'd do it like:

pLabel = new TMyLabel( DesiredParent, SomeLeft, SomeTop, "SomeCaption" );

This method doesn't even require a TMyLabel pointer (unless
you need to access addition label properties or you need to
reference the label later). You could just use:

new TMyLabel( DesiredParent, SomeLeft, SomeTop, "SomeCaption" );
new TMyLabel( DesiredParent, SomeOtherLeft, SomeOtherTop, "SomeOtherCaption" );

If you need access to the new label only at the point of
allocation, you can reuse the same pointer:

pLabel = new TMyLabel( DesiredParent, SomeLeft, SomeTop, "SomeCaption" );
pLabel->SomePropert = SomeValue;
pLabel = new TMyLabel( DesiredParent, SomeOtherLeft, SomeOtherTop, "SomeOtherCaption" );
pLabel->SomePropert = SomeOtherValue;

If you need to acces the label at runtime, you'll need to save
the TMyLabel pointer returned by new(ing) it. In that case,
your best bet is using an array and I would suggest using
DynamicArray v/s allocating and deleting an array of TMyLabel
pointers or using a TList because you don't have to worry
about deleting a DynamicArray.

~ JD


Back to top
metal.c
Guest





PostPosted: Mon May 30, 2005 6:53 pm    Post subject: Re: Need Label component. Reply with quote


Now that's what I call helpful information! Thanks
Back to top
metal.c
Guest





PostPosted: Mon May 30, 2005 8:10 pm    Post subject: Re: Need Label component. Reply with quote


Thanks JD. I'm a newbie, but I got it to work.
I had to change "Wnd" to "Win". Was that a trick? Smile
Back to top
JD
Guest





PostPosted: Tue May 31, 2005 2:28 am    Post subject: Re: Need Label component. Reply with quote


"metal.c" <no (AT) spam (DOT) com> wrote:
Quote:

[...] I'm a newbie,

That's why I gave you an extensive response.

Quote:
[...] I had to change "Wnd" to "Win". Was that a trick? Smile

Don't know with out knowing exactly what you're refering to.
My best guess is that it was a type-o on my part. Let that be
a lesson as well ... you can get the right information here
but a type-o changes everything ;-)

~ JD


Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Usage) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.