成员变量关联:
private:
CMFCButton m_btnColorTest;
CMFCButton* m_pBtnColor;
// CMFCAppColorDlg 对话框
#define DELETE(ptr) do{if (ptr) {delete ptr; ptr = nullptr;}}while(0)
CMFCAppColorDlg::CMFCAppColorDlg(CWnd* pParent /*=nullptr*/)
: CDialogEx(IDD_MFCAPPCOLOR_DIALOG, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_pBtnColor = new CMFCButton; // 堆上的对象放在构造函数中初始化,保证 DDX_Control 时对象已经被创建,
// 放 OnInitDialog() 中不行,滞后于 DDX_Control 了,会有问题。
}
CMFCAppColorDlg::~CMFCAppColorDlg()
{
DELETE(m_pBtnColor);
}
void CMFCAppColorDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_BUTTON1, m_btnColorTest); // 关联变量
DDX_Control(pDX, IDC_BUTTON2, *m_pBtnColor); // 关联变量
}
BEGIN_MESSAGE_MAP(CMFCAppColorDlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, &CMFCAppColorDlg::OnBnClickedButton1)
ON_BN_CLICKED(IDC_BUTTON2, &CMFCAppColorDlg::OnBnClickedButton2)
END_MESSAGE_MAP()
按钮事件:
void CMFCAppColorDlg::OnBnClickedButton1()
{
m_pBtnColor->SetTextColor(RGB(255, 0, 0));
Invalidate(); // MFC刷新界面
}
void CMFCAppColorDlg::OnBnClickedButton2()
{
m_pBtnColor->SetTextColor(RGB(0, 0, 0));
Invalidate(); // // MFC刷新界面
}
标签:MFC,更改,IDC,DDX,CMFCAppColorDlg,按钮,pBtnColor,ptr
From: https://www.cnblogs.com/huvjie/p/18021786