Simplest Subclassing - a sample CTipButton class from CButton
Submitted by Date of Submission User Level
Bulent Ozkir February 8, 2001 Beginner
In this article, I tried to make subclassing an MFC control CButton simply and tried to create a CTipButton class, which is an auto multi line ToolTip enabled button. By following this example you can make all MFC controls auto ToolTip enabled. This will surely save your development. I hope this inspires you some. Other than the ToolTip feauture, all the CButton events can be declared, implemented by CTipButton. Thanks to public inheritance.
Since the code reveals itself, I am not going to explain them. Please probe into TipButton.h and TipButton.cpp sourcecodes to grab subclassing. After then refer to MSDN for the description of MFC classes and functions you wonder. ( Beware the CToolTip.RelayEvent , CButton::PreTranslateMessage functions... )
As we to enable subclassing to work, we just need to create a variable of CTipButton in our main dialog. In this example I created 2 buttons m_myTipButton and m_myTipButton2.
Step 0 : Add the TipButton.h and TipButton.cpp by using "Add Files to Folder" of your Files tab at your Workspace.
Step 1 : In the first step in your main dialog's header file just include;
#include "TipButton.h"
Step 2 : So I just add a member variable using ClassWizard of my main dialog; (so subclassing happens automagically :) ) ,
Step 3 : And in the InitDialog (or in any member function ) (OPTIONAL for displaying ToolTipText) ,
m_myTipButton.SetToolTipText(CString(_T("Developed by Bulent Ozkir, bulentozkir@hotmail.com, to change this text just call CTipButton::SetToolTipText(...) any time...")), TRUE);
m_myTipButton2.SetToolTipText(CString(_T("tip button2 Developed by 1 2 3 4 5 Bulent Ozkir, bulentozkir@hotmail.com,")), TRUE);
// you can either call this way... strings from resource table ...
//m_myTipButton.SetToolTipText(IDS_STRING1, TRUE);
//m_myTipButton2.SetToolTipText(IDS_STRING1, TRUE);
--------------------------------------------------------------------------------
Bulent Ozkir is a Turkey based senior software engineer. He has worked as a senior support engineer for Microsoft from 1998 to 2000. In his over 5 years programming career he has been working on various programming technologies including VB, VC, Visual Interdev, JavaScript, ASP, HTML/DHTML, WMI, COM+, TCP/IP, IIS, SS3, MCIS25, ASP and SQL Server. His background includes B.A. Comp. Sc., MCSE + Internet and MCSD. See members area for more details.
--------------------------------------------------------------------------------