C++ Builder

La Isla Bonita



How to Kill the Focus of a Windowed Control
How to Kill the Focus of a Windowed Control


Description

In this example I will show how to kill the focus of a TMyButton control which is a descendant of a TButton.

We will override TButton::WndProc to change the initial Windows message handler for the control. Thus, the TMyButton::WndProc will implement the behaviour we want (the desired behaviour is to kill the focus on the TButton buttons)

To achieve this behaviour, we intercept the WM_SETFOCUS message (see TMyButton::WndProc), and then we respond by sending the WM_KILLFOCUS message through the SendMessage() function:

SendMessage(pMyButton1->Handle, WM_KILLFOCUS, Message.Msg, NULL);
The TMyButton class is publicly derived from TButton:
class TMyButton : public TButton
{
  private:	// User declarations

  public:	// User declarations
         // Constructor
        __fastcall TMyButton(TComponent* Owner): TButton(Owner){}
         // WndProc procedure
         void __fastcall WndProc(Messages::TMessage &Message);
};
Notice that this class has only two methods: the constructor and the WndProc procedure. Both methods are public.

All components on the form are created at runtime. (BitBtn1, BitBtn2, pMyButton1 and pMyButton2)



Code

KillFocus.cpp


#include <vcl.h>
#pragma hdrstop

#include "KillFocus.h"
TForm1 *Form1;
//-----------------------------------------------------------------------
TBitBtn*   BitBtn1;
TBitBtn*   BitBtn2;
TMyButton* pMyButton1;
TMyButton* pMyButton2;


__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
   Color = clBlue;

   BitBtn1 = new TBitBtn(this);
   BitBtn1->Parent  = this;
   BitBtn1->Height  = 50;
   BitBtn1->Top     = 10;
   BitBtn1->Width   = 200;
   BitBtn1->Left    = (Width - BitBtn1->Width )/2;
   BitBtn1->Caption = "BITBTN 1";

   BitBtn2 = new TBitBtn(this);
   BitBtn2->Parent  = this;
   BitBtn2->Height  = 50;
   BitBtn2->Top     = BitBtn1->Top + BitBtn1->Height + 1;
   BitBtn2->Width   = 200;
   BitBtn2->Left    = (Width - BitBtn2->Width )/2;
   BitBtn2->Caption = "BITBTN 2";

   pMyButton1 = new TMyButton(this);
   pMyButton1->Parent  = this;
   pMyButton1->Height  = 100;
   pMyButton1->Top     = (ClientHeight - pMyButton1->Height)/2;
   pMyButton1->Width   = 200;
   pMyButton1->Left    = (Width - pMyButton1->Width)/2;
   pMyButton1->Caption = "Button 1 (Focus Killed)";

   pMyButton2 = new TMyButton(this);
   pMyButton2->Parent  = this;
   pMyButton2->Height  = 100;
   pMyButton2->Top     = (ClientHeight + pMyButton2->Height)/2 + 1;
   pMyButton2->Width   = 200;
   pMyButton2->Left    = (Width - pMyButton2->Width )/2;
   pMyButton2->Caption = "Button 2 (Focus Killed)";
}
//----------------------------------------------------------------------


void __fastcall TMyButton::WndProc( Messages::TMessage &Message )
{  
   // Intercept the message: WM_SETFOCUS 
   if (Message.Msg == WM_SETFOCUS)
   {   
       // Respond to the message by sending the WM_KILLFOCUS message
       SendMessage(pMyButton1->Handle, WM_KILLFOCUS, Message.Msg, NULL);
   }
   else
   {   
       // Call the inherited method WndProc() to process any
       // other message that has not been processed.
       TButton::WndProc( Message );
   }
}
//-----------------------------------------------------------------------

LocalDateTime.h


#ifndef KillFocusH
#define KillFocusH
//-----------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <Buttons.hpp>
//-----------------------------------------------------------------------


// TMyButton class is derived from TButton
class TMyButton : public TButton
{
  private:	// User declarations

  public:	// User declarations
        __fastcall TMyButton(TComponent* Owner):
                   TButton(Owner)
        {
        }
        void __fastcall WndProc(Messages::TMessage &Message);
};


class TForm1 : public TForm
{
__published:	// IDE-managed Components

private:	// User declarations
public:		// User declarations
        __fastcall TForm1(TComponent* Owner);
};
//-----------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//-----------------------------------------------------------------------
#endif

Homepage

Copyright © 1997-2002 Rodolfo A. Frino. All rights reserved.