1. 用ClassWizard创建一个基于CButton的新类CWeixinBtn,然后利用ClassWizard增加类CWeixinBtn的成员和方法:
(1)在该类中增加成员: 一个指针CWeixinBtn *m_pBtn,必须是public,否则在类CTestDlg中访问不了.
(2)在该类中增加一个方法:OnMouseMove(),也就是增加一个鼠标移动到上面的消息响应函数
在该消息响应函数的实现代码中,隐藏自己,显示别人(m_Btn所指向的那个)
2. CTestDlg中:在将2个按钮(外观一样的)分别增加关联变量m_btn1,m_btn2,变量类型选择CWeixinBtn
(1)变量m_bt1属于CWeixinBtn类,自动会响应该类中的OnMouseMove
(2)变量m_bt1的任何响应,实际是其关联的按钮IDC_BUTTON1的响应.
(3) 在对话框创建完成后(其上的控件按钮也创建OK),它会发送WM_INITDIALOG消息,因此类CTestDlg中有该消息的响应函数OnInitDialog(),在该函数中交换地址,也就是说必须要等到按钮生成后才能获得其地址,也才能交换
m_btn1.m_pBtn = &m_btn2; //交换按钮的地址
m_btn2.m_pBtn = &m_btn1;
CTestDlg.cpp
(1)在该类中增加成员: 一个指针CWeixinBtn *m_pBtn,必须是public,否则在类CTestDlg中访问不了.
(2)在该类中增加一个方法:OnMouseMove(),也就是增加一个鼠标移动到上面的消息响应函数
在该消息响应函数的实现代码中,隐藏自己,显示别人(m_Btn所指向的那个)
2. CTestDlg中:在将2个按钮(外观一样的)分别增加关联变量m_btn1,m_btn2,变量类型选择CWeixinBtn
(1)变量m_bt1属于CWeixinBtn类,自动会响应该类中的OnMouseMove
(2)变量m_bt1的任何响应,实际是其关联的按钮IDC_BUTTON1的响应.
(3) 在对话框创建完成后(其上的控件按钮也创建OK),它会发送WM_INITDIALOG消息,因此类CTestDlg中有该消息的响应函数OnInitDialog(),在该函数中交换地址,也就是说必须要等到按钮生成后才能获得其地址,也才能交换
m_btn1.m_pBtn = &m_btn2; //交换按钮的地址
m_btn2.m_pBtn = &m_btn1;
类WeixinBtn.cpp 的实现:
// WeixinBtn.cpp : implementation file
//
#include "stdafx.h"
#include "Test.h"
#include "WeixinBtn.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/
// CWeixinBtn
CWeixinBtn::CWeixinBtn()
{
}
CWeixinBtn::~CWeixinBtn()
{
}
BEGIN_MESSAGE_MAP(CWeixinBtn, CButton)
//{{AFX_MSG_MAP(CWeixinBtn)
ON_WM_MOUSEMOVE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/
// CWeixinBtn message handlers
void CWeixinBtn::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
this->ShowWindow(SW_HIDE); //让自己隐藏
this->m_pBtn->ShowWindow(SW_SHOW);//让自己的m_pBtn所指向的那个按钮显示
CButton::OnMouseMove(nFlags, point);
}
CTestDlg.cpp
// TestDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Test.h"
#include "TestDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/
// CTestDlg dialog
CTestDlg::CTestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTestDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTestDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTestDlg)
DDX_Control(pDX, IDC_BUTTON2, m_btn2);
DDX_Control(pDX, IDC_BUTTON1, m_btn1);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
//{{AFX_MSG_MAP(CTestDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/
// CTestDlg message handlers
BOOL CTestDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
m_btn1.m_pBtn = &m_btn2; //交换按钮的地址
m_btn2.m_pBtn = &m_btn1;
return TRUE; // return TRUE unless you set the focus to a control
}
void CTestDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CTestDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CTestDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
//---