A button within a button


This control contains two buttons. It's very easy to use. Place a button control in your CDialog, add a CHiButton member variable (eg m_wndHiButton), and then in OnInitDialog subclass the control with m_wndHiButton.SubclassDlgItem(IDC_BUTTON1, this). To handle the message, it sufficient to add, using the class wizard, a handler for "on button click". User can disinguish between which button was pressed using something like:

void CTestDlg::OnHibutton() 
{ 
 if (m_hiButton.m_bInsideButton) 
	AfxMessageBox(_T("TestInside")); 
 else 
	AfxMessageBox(_T("Test")); 
} 
The user can attach icon, bitmap, text, or cursor to this control.

Here the header file:

/************************************ 
 REVISION LOG ENTRY 
 Revision By: Mihai Filimon 
 Revised on 7/3/98 12:20:46 PM 
 Comments: HiButton.h : header file 
 ************************************/ 

#if 
!defined(AFX_HIBUTTON_H__44AF4523_1247_11D2_863B_0040055C08D9__INCLUDED_) 

#define AFX_HIBUTTON_H__44AF4523_1247_11D2_863B_0040055C08D9__INCLUDED_ 

#if _MSC_VER >= 1000 
#pragma once 
#endif // _MSC_VER >= 1000 

///////////////////////////////////////////////////////////////////////////// 

// CHiButton window 

class CHiButton : public CButton 
{ 
// Construction 
public: 
 CHiButton(); 
 BOOL m_bInsideButton; 
 HBITMAP SetBitmap( HBITMAP hBitmap ); 
 HICON SetIcon( HICON hIcon ); 
 HCURSOR SetCursor( HCURSOR hCursor ); 
// Attributes 
public: 

// Operations 
public: 

// Overrides 
 // ClassWizard generated virtual function overrides 
 //{{AFX_VIRTUAL(CHiButton) 
 protected: 
 virtual void PreSubclassWindow(); 
 virtual BOOL OnCommand(WPARAM wParam, LPARAM lParam); 
 //}}AFX_VIRTUAL 

// Implementation 
public: 
 virtual ~CHiButton(); 

 // Generated message map functions 
protected: 
 //{{AFX_MSG(CHiButton) 
 // NOTE - the ClassWizard will add and remove member functions here. 
 //}}AFX_MSG 

 DECLARE_MESSAGE_MAP() 
private: 
 CButton m_wndButtonInside; 
}; 

///////////////////////////////////////////////////////////////////////////// 

//{{AFX_INSERT_LOCATION}} 
// Microsoft Developer Studio will insert additional declarations 
immediately before the previous line. 

#endif // 
!defined(AFX_HIBUTTON_H__44AF4523_1247_11D2_863B_0040055C08D9__INCLUDED_) 

Here the source file:
/************************************ 
 REVISION LOG ENTRY 
 Revision By: Mihai Filimon 
 Revised on 7/3/98 11:10:00 AM 
 Comments: HiButton.cpp : implementation file 
 ************************************/ 

#include "stdafx.h" 
#include "tesat.h" 
#include "HiButton.h" 

#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 

///////////////////////////////////////////////////////////////////////////// 

// CHiButton 

// Function name : CHiButton::CHiButton 
// Description : Contructor 
// Return type : 
CHiButton::CHiButton() 
{ 
 m_bInsideButton = FALSE; 
} 

// Function name : CHiButton::~CHiButton 
// Description : Destructor 
// Return type : 
CHiButton::~CHiButton() 
{ 
} 


BEGIN_MESSAGE_MAP(CHiButton, CButton) 
 //{{AFX_MSG_MAP(CHiButton) 
 // NOTE - the ClassWizard will add and remove mapping macros here. 
 //}}AFX_MSG_MAP 
END_MESSAGE_MAP() 

///////////////////////////////////////////////////////////////////////////// 

// CHiButton message handlers 

// Function name : CHiButton::PreSubclassWindow 
// Description : Subclass control 
// Return type : void 
void CHiButton::PreSubclassWindow() 
{ 
 CString caption; 
 GetWindowText(caption); 
 SetWindowText(_T("")); 
 CRect client; GetClientRect(client); 
 int dx = (int)((double)client.Width() * 0.8), dy = 
(int)((double)client.Height() * 0.7); 
 m_wndButtonInside.Create(caption, WS_CHILD | WS_VISIBLE | GetStyle(), 
CRect(CPoint((client.Width() - dx) / 2, (client.Height() - dy) / 
2),CSize( dx, dy)), this, 0); 
 m_wndButtonInside.SetWindowPos(&CWnd::wndNoTopMost,0,0,0,0, SWP_NOMOVE 
| SWP_NOSIZE); 
 ModifyStyle(0,WS_CLIPSIBLINGS | WS_CLIPCHILDREN); 
 m_wndButtonInside.SetFont(GetFont()); 
 CButton::PreSubclassWindow(); 
} 


// Function name : CHiButton::OnCommand 
// Description : Send WM_COMMAND to parent of button. 
// Return type : BOOL 
// Argument : WPARAM wParam 
// Argument : LPARAM lParam 
BOOL CHiButton::OnCommand(WPARAM wParam, LPARAM lParam) 
{ 
 m_bInsideButton = TRUE; 
 GetParent()->SendMessage(WM_COMMAND, 
MAKEWPARAM(GetDlgCtrlID(),HIWORD(wParam)), lParam); 
 m_bInsideButton = FALSE; 

 return CButton::OnCommand(wParam, lParam); 
} 

// Function name : CHiButton::SetBitmap 
// Description : Call to set the bitmap image of inside button 
// Return type : HBITMAP 
// Argument : HBITMAP hBitmap 
HBITMAP CHiButton::SetBitmap( HBITMAP hBitmap ) 
{ 
 ASSERT(m_wndButtonInside.GetSafeHwnd()); 
 return m_wndButtonInside.SetBitmap(hBitmap); 
} 

// Function name : CHiButton::SetIcon 
// Description : Call to set the icon image of inside button 
// Return type : HICON 
// Argument : HICON hIcon 
HICON CHiButton::SetIcon( HICON hIcon ) 
{ 
 ASSERT(m_wndButtonInside.GetSafeHwnd()); 
 return m_wndButtonInside.SetIcon(hIcon); 
} 

// Function name : CHiButton::SetCursor 
// Description : 
// Return type : HICON 
// Argument : HCURSOR hCursor 
HCURSOR CHiButton::SetCursor( HCURSOR hCursor ) 
{ 
 ASSERT(m_wndButtonInside.GetSafeHwnd()); 
 return m_wndButtonInside.SetCursor(hCursor); 
} 

Last updated: 12 July 1998



Comments

  • 我是朱大牛呀我是朱大牛

    Posted by Legacy on 10/16/2003 12:00am

    Originally posted by: 朱大牛

    我是朱大牛呀我是朱大牛

    Reply
  • where to use

    Posted by Legacy on 08/18/2003 12:00am

    Originally posted by: kmnaveed

    tell me where we can use button with in a button . Two or three good uses plz.
    

    Reply
  • button controls

    Posted by Legacy on 07/09/2003 12:00am

    Originally posted by: balamurugan.k

    I want study materials in VC++ and also ActiveX contros.

    Reply
  • help needed visual c++.net

    Posted by Legacy on 06/01/2003 12:00am

    Originally posted by: maria

    I am a novice 
    
    Could u plz help me in coding

    Reply
  • But why?

    Posted by Legacy on 06/26/2002 12:00am

    Originally posted by: RealCodeGuy

    Give me one example of why you would want a button within a button?

    Reply
  • good :}

    Posted by Legacy on 06/06/2002 12:00am

    Originally posted by: okky

    but what a pity there is no a compile file

    Reply
  • hoi thac mac 1 chut???

    Posted by Legacy on 04/07/2002 12:00am

    Originally posted by: sandy

    Xin chi cho toi biet , toi dang lam de tai hien thuc trang web tro choi "thornq" trong linux thong qua source code c++ dung Activex control. Vay thi toi phai bat dau lam tu dau??Toi chua biet gi ve cach tao Activex Control???Co the chi muc nao noi chi tiet ve dieu nay khong?
    Xin chan thanh cam on/
    sandy

    Reply
  • ^^*

    Posted by Legacy on 01/10/2002 12:00am

    Originally posted by: hihi

    hi~

    very good.^^;

    and byebye~

    Reply
  • giup do ?!

    Posted by Legacy on 10/25/2001 12:00am

    Originally posted by: Vo Van Tai

    Xin cho Toi hoi ve viec Download bai HDHNC ve nha lam nhung khong lam duoc,Hay giup toi download bai nay,xin chi diup tung buoc,boi vi toi co load ve duoc nhung khi mo no len thi bi bao boi mot thong diep:
    
    "Could not complette the operation due to error 800c0011"
    Toi xin cam on.
    tp.HCM 26.10.2001
    sv Vo Van Tai

    Reply
  • Visual Basic Type Button Please...

    Posted by Legacy on 07/01/2001 12:00am

    Originally posted by: Pandu

    Please give code for creating similar buttons in Visual Basic also..

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Hybrid cloud platforms need to think in terms of sweet spots when it comes to application platform interface (API) integration. Cloud Velocity has taken a unique approach to tight integration with the API sweet spot; enough to support the agility of physical and virtual apps, including multi-tier environments and databases, while reducing capital and operating costs. Read this case study to learn how a global-level Fortune 1000 company was able to deploy an entire 6+ TB Oracle eCommerce stack in Amazon Web …

  • Protecting business operations means shifting the priorities around availability from disaster recovery to business continuity. Enterprises are shifting their focus from recovery from a disaster to preventing the disaster in the first place. With this change in mindset, disaster recovery is no longer the first line of defense; the organizations with a smarter business continuity practice are less impacted when disasters strike. This SmartSelect will provide insight to help guide your enterprise toward better …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds