您的位置:首页 > 其它

MFC CButton点击能在OFF/ON之间切换

2013-08-28 15:06 155 查看
CButton控件点击后自动切换文本信息:

1. 继承一个CButton的类,增加函数

protected:
afx_msg BOOL OnClicked1();
2. 函数代码为:

BOOL CButton_Change::OnClicked1()

{

int lenght;

lenght = GetWindowTextLengthW();

if(lenght==3){

this->SetWindowTextW(_T("ON"));

}else{

this->SetWindowTextW(_T("OFF"));

}

return TRUE;

}

3. 添加消息处理:

ON_CONTROL_REFLECT_EX(BN_CLICKED, &CButton_Change::OnClicked1)

最后为:

BEGIN_MESSAGE_MAP(CButton_Change, CButton)
ON_CONTROL_REFLECT_EX(BN_CLICKED, &CButton_Change::OnClicked1)
END_MESSAGE_MAP()

4. 使用:在*Dlg.cpp中

button = new CButton_Change;

button->Create(_T("OFF"), 0, CRect(200, 200, 250, 250), this, IDC_BUTTON2);

button->ShowWindow(TRUE);

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: