Description
Generating specialized responses to messages sent by Windows to our application.
OverrideWndProc1.cpp
OverrideWndProc1.h
Copyright © 1997-2002 Rodolfo A. Frino. All rights reserved.
This article shows how to derive a TMyButton class from the TButton
class and how to make the button to respond to Right Mouse Clicks
by overriding the Parent class' method (TButton::WndProc)
with the derived class' method (TMyButton::WndProc). Thus, providing a
specialized response to messages.
The structure TMessage is the argument to the WndProc() procedure.
This structure is passed by reference.
#include <vcl.h>
#pragma hdrstop
#include "OverrideWndProc1.h"
//-----------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//-----------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
Color = clBlue;
//Create a TMyButton:
TMyButton* pMyButton = new TMyButton( this );
pMyButton->Parent = this;
pMyButton->Height = 100;
pMyButton->Top = ( ClientHeight - pMyButton->Height )/2;
pMyButton->Width = 200;
pMyButton->Left = ( Width - pMyButton->Width )/2;
pMyButton->Caption = "Big Button";
}
//----------------------------------------------------------------------
void __fastcall TMyButton:: Resize( int width1, int height1,
int width2, int height2 )
{
enum
{
stateSMALL = 0,
stateLARGE = 1
};
static int ButtonState = stateLARGE;
if ( ButtonState == stateLARGE )
{
Form1->Caption = "Small Button";
Caption = "Small Button";
Width = width1;
Height = height1;
Top = ( Form1->ClientHeight - Height )/2;
Left = ( Form1->Width - Width )/2;
ButtonState = stateSMALL;
}
else
{
Form1->Caption = "Big Button";
Caption = "Big Button";
Width = width2;
Height = height2;
Top = ( Form1->ClientHeight - Height )/2;
Left = ( Form1->Width - Width )/2;
ButtonState = stateLARGE;
}
}
//-----------------------------------------------------------------------
void __fastcall TMyButton::WndProc( Messages::TMessage &Message )
{
//Intercept the two messages: WM_RBUTTONDOWN
//and WM_RBUTTONDBLCLK and respond to them
//by calling the Rezize() function.
if ( ( Message.Msg == WM_RBUTTONDOWN ) ||
( Message.Msg == WM_RBUTTONDBLCLK ) )
{
//Respond to the messages
Resize( 100, 50, 200, 100 );
}
else
{
//Call the parent class’s method TButton::WndProc()
//to process any other unprocessed messages
TButton::WndProc( Message );
}
}
#ifndef OverrideWndProc1H
#define OverrideWndProc1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
//New TMyButton class derived from TButton (Parent class)
class TMyButton : public TButton
{
private: // User declarations
public: // User declarations
__fastcall TMyButton( TComponent* Owner ):
TButton( Owner )
{
}
void __fastcall WndProc( Messages::TMessage &Message );
void __fastcall Resize( int width1, int height1,
int width2, int height2 );
};
class TForm1 : public TForm
{
__published: // IDE-managed Components
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//-----------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif