A button within a button

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

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

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read