Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: VCL component OnClick event


This question is answered.


Permlink Replies: 5 - Last Post: Feb 26, 2016 10:00 AM Last Post By: Remy Lebeau (Te...
Mario Sassone

Posts: 12
Registered: 10/28/06
VCL component OnClick event  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 25, 2016 12:16 PM
My conceptual problem is:

Im designing a visual component, descendant of TGraphicControl, to be inserted in my form.

When I click on it, first it shoud update himself internally; second, some other operation has to be done in the form.

If I handle the OnClick event inside the component, with:

TMyGraphicControl::OnClick = TMyGraphicControl::MyOnClickHandler

internal updating works; if I handle the component OnClick event in the form, with:

MyGraphicControl->OnClick = TMyForm::MyOnClickHandler

I can make operations at the form level.

How can I obtain both the things? The two above ways seem not compatible to me.

I guessed that a good way could be to read the MyGraphicControl->ControlState property, but I didnt find explanations about the use of Vcl_Controls__21 enums and the following syntax does not work:

if(MyGraphicControl->ControlState == csLButtonDown)

Every help will be appreciated a lot.
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: VCL component OnClick event
Correct
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 25, 2016 1:27 PM   in response to: Mario Sassone in response to: Mario Sassone
Mario wrote:

How can I obtain both the things?

You need to override the virtual Click() method. Have it make whatever internal
updates it needs to make, and then call the inherited Click() method to fire
the user's OnClick event handler. For example:

class TMyControl : public TGraphicControl
{
protected:
    DYNAMIC void __fastcall Click();
 
__published:
    __property OnClick;
};
 
void __fastcall TMyControl::Click()
{
    // make updates...
    
    // fire OnClick event...
    TGraphicControl::Click();
}


--
Remy Lebeau (TeamB)
Mario Sassone

Posts: 12
Registered: 10/28/06
Re: VCL component OnClick event  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 25, 2016 11:13 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Thank you, now it works perfectly.
Mario Sassone

Posts: 12
Registered: 10/28/06
Re: VCL component OnClick event  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 25, 2016 11:14 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Thank you. Now it works perfectly.
Mario Sassone

Posts: 12
Registered: 10/28/06
Re: VCL component OnClick event  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 26, 2016 12:31 AM   in response to: Mario Sassone in response to: Mario Sassone
One last thing about mouse handling:

Overriding the virtual methods works perfectly for

Click()
MouseUp()
MouseDown()
MouseMove()


but there's no virtual method for mouse leaving and entering, only the events:

OnMouseLeave()
OnMouseEnter()


so the only way I know to manage mouse leaving inside the component is through the messages:

class PACKAGE MyGraphicControl : public TGraphicControl
{
private:
	TNotifyEvent FOnMouseLeave;
	MESSAGE void __fastcall CMMouseLeave    (TMessage &Msg);
 
protected:
public:
	__fastcall TO2button(TComponent* Owner);
__published:
	__property TNotifyEvent OnMouseLeave = {read=FOnMouseLeave, write=FOnMouseLeave};
 
	BEGIN_MESSAGE_MAP
		MESSAGE_HANDLER(CM_MOUSELEAVE,     TMessage, CMMouseLeave)
	END_MESSAGE_MAP(TGraphicControl)
};
 
void __fastcall MyComponent::CMMouseLeave(TMessage &Msg)
{
	if (FOnMouseLeave) FOnMouseLeave(this);
	
	// make updates here
}


Can you suggest a simpler alternative, maybe without handling windows messages directly?

Edited by: Mario Sassone on Feb 26, 2016 9:32 AM

Edited by: Mario Sassone on Feb 26, 2016 9:32 AM

Edited by: Mario Sassone on Feb 26, 2016 9:45 AM
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: VCL component OnClick event [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 26, 2016 10:00 AM   in response to: Mario Sassone in response to: Mario Sassone
Mario wrote:

but there's no virtual method for mouse leaving and entering

True. You have to handle the CM_MOUSEENTER and CM_MOUSELEAVE messages directly
instead.

the only way I know to manage mouse leaving inside the component
is through the messages:

Yes. But you do not need to define your own OnMouseLeave event manually,
it already exists in TControl, so just promote it:

class PACKAGE MyGraphicControl : public TGraphicControl
{
private:
    MESSAGE void __fastcall CMMouseLeave(TMessage &Message);
 
public:
    __fastcall MyGraphicControl(TComponent* Owner);
 
    BEGIN_MESSAGE_MAP
        VCL_MESSAGE_HANDLER(CM_MOUSELEAVE, TMessage, CMMouseLeave)
    END_MESSAGE_MAP(TGraphicControl)
 
__published:
    __property OnMouseLeave;
};
 
void __fastcall MyComponent::CMMouseLeave(TMessage &Message)
{
    // make updates here
 
    // fire OnMouseLeave event handler
    TGraphicControl::Dispatch(&Message);
}


Alternatively:

class PACKAGE MyGraphicControl : public TGraphicControl
{
protected:
    virtual void __fastcall WndProc(TMessage &Message);
 
public:
    __fastcall MyGraphicControl(TComponent* Owner);
 
__published:
    __property OnMouseLeave;
};
 
void __fastcall MyComponent::WndProc(TMessage &Message)
{
    if (Message.Msg == CM_MOUSELEAVE)
    {
        // make updates here
    }
 
    TGraphicControl::WndProc(Message);
}


Can you suggest a simpler alternative, maybe without handling
windows messages directly?

Sadly, there isn't one.

--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02