按钮之中包含按钮
本程序介绍如何实现在按钮中包含按钮,参见下图。
使用方法如下。在CDialog派生类中添加一个CHiButton成员变量m_wndHiButton,在OnInitDialog中调用 m_wndHiButton.SubclassDlgItem(IDC_BUTTON1, this)即可。这样,只要用户点击按钮,程序就会按下面逻辑进行判断:
void CTestDlg::OnHibutton()
{
if (m_hiButton.m_bInsideButton)
AfxMessageBox(_T("TestInside"));
else
AfxMessageBox(_T("Test"));
}
用户可以附加位图、图标、文本或者光标到该控件上。
下面是头文件:
/************************************
REVISION LOG ENTRY
Revision By: Mihai Filimon
Revised on 7/3/98 12:20:46 PM
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_)
下面是源文件:
/************************************
REVISION LOG ENTRY
Revision By: Mihai Filimon
Revised on 7/3/98 11:10:00 AM
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);
}