Search code examples
c++buildervclc++builder-2007

Borland Builder 2007 Child VCL controls and the WindowProc method


I have managed to override a controls WindowProc function in order to determine more events than are supported by Builder (i.e. double middle mouse clicks etc..)

The trouble is when I override say a child components WindowProc the parent still gets the event.

In my case I have a panel which has a button parented with it. The button obscures a part of the panel but when clicking the button the parent (panel) gets the event also (haven't tested that the button gets the event also but wanted to to see if anybody had a solution for this firstly).

Is there any way that you can programatically stop the parent receiving the event or a way of determining whether the event is for the child and not the parent.

The problem I will have is that if the user presses the button the panel event and the button event will be triggered.

Please any advice will be gratefully received.

Joe

Code Example: Note that Panel and Button derive from ConfigComponent class which is where the m_kOldComponentWndMethod and ComponentWndProc method live.

Overriding the panel

//---------------------------------------------------------------------------
CConfigComponentPanel::CConfigComponentPanel( TObject* pkParent,
                                              const CConfigComponentDimensions& rkConfigComponentDimensions,
                                              const CConfigComponentPos& rkConfigComponentPos,
                                              const CConfigSelect::SelectCollection& rkSelectCollection,
                                              TColor kBackgroundColour,
                                              TColor kForegroundColour,
                                              const std::string& rstrDisplayText,
                                              const CConfigFontRef& rkConfigFontRef,
                                              const CConfigComponent::ConfigComponentCollection& rkConfigComponentCollection )
:   CConfigComponent( rkConfigComponentDimensions, rkConfigComponentPos, kBackgroundColour, kForegroundColour ),
    m_pkPanel( new TPanel( ( TComponent* )NULL ) ),
    m_kConfigComponentCollection(),
    m_kSelectCollection(),
    m_kConfigFontRef( rkConfigFontRef ),
    m_strDisplayText( rstrDisplayText )
{
    // Set the parent.
    m_pkPanel->Parent = dynamic_cast<TWinControl*>( pkParent );

    if ( rkSelectCollection.size() > 0 )
    {
        // Store the old window proc method before overriding.
        m_kOldComponentWndMethod = m_pkPanel->WindowProc;
        m_pkPanel->WindowProc = ComponentWndProc;
    }

    // Add selects to collection
    AddSelectsToCollection( rkSelectCollection );
    // Add components to collection
    AddConfigComponentsToCollection( rkConfigComponentCollection );
}
//---------------------------------------------------------------------------

Overriding the Button

//---------------------------------------------------------------------------
CConfigComponentButton::CConfigComponentButton( TObject* pkParent,
                                                const CConfigSelect::SelectCollection& rkSelectCollection,
                                                const CConfigComponentDimensions& rkConfigComponentDimensions,
                                                const CConfigComponentPos& rkConfigComponentPos,
                                                TColor kBackgroundColour,
                                                TColor kForegroundColour,
                                                const CConfigButtonBGProperties& rkConfigButtonBGProperties ):
    CConfigComponent( rkConfigComponentDimensions, rkConfigComponentPos, kBackgroundColour, kForegroundColour ),
    m_kConfigButtonBGProperties( rkConfigButtonBGProperties ),
    m_pkButton( new TAdvToolButton( NULL ) ),
    m_kSelectCollection( rkSelectCollection )
{
    m_pkButton->Parent = dynamic_cast<TWinControl*>( pkParent );

    // Store the old window proc method before overriding.
    m_kOldComponentWndMethod = m_pkButton->WindowProc;
    m_pkButton->WindowProc = ComponentWndProc;
}
//---------------------------------------------------------------------------

The Component Wnd Proc Method ( comes in here twice once for panel and once for button )

//---------------------------------------------------------------------------
void __fastcall CConfigComponent::ComponentWndProc( TMessage& rkMessage )const
{
    if ( rkMessage.Msg == WM_MBUTTONDBLCLK )
    {
        (void)Application->MessageBox( "CConfigComponent::ComponentWndProc", "" );
    }
    if ( rkMessage.Msg == WM_LBUTTONDBLCLK )
    {
        (void)Application->MessageBox( "ComponentWndProc::Left Button", "" );
    }

    if ( m_kOldComponentWndMethod )
    {
        m_kOldComponentWndMethod( rkMessage );
    }
}
//---------------------------------------------------------------------------

Thanks, Joe


Solution

  • TAdvToolButton is a TGraphicControl descendant. TGraphicalControl does not have its own window and thus cannot receive user input directly. User input is directed to the Parent window, so the WindowProc of the Parent sees the messages first. If the input occured within the client area of a child TGraphicControl, the Parent then forwards the input to that child. That is why you are seeing user input messages occur in both components. If you change your CConfigComponentButton class to use a windowed button, like TButton or TBitBtn, you will not see the duplicate messages anymore since user input will be directed to the button directly and not to the parent TPanel.