我想在程序运行中修改控件的风格,编写了如下的程序:
void CControlDlg::OnButton1() 
{
CEdit *pEdit = (CEdit*)GetDlgItem(IDC_DATA);
if ((pEdit->GetStyle() & ES_READONLY) == 0x0)
{

pEdit->ModifyStyle(0x0, ES_READONLY);
          }
}
但是运行后,根本没有变化,而且可以在编辑框里面输入字符,并不是预料中的只读属性。
question1:我修改了静态控件的风格,是成功的,为什么?
question2:请问怎样才能正确修改编辑控件的风格,以及如何修改其他种类控件的风格?

解决方案 »

  1.   

    文本框有特别的地方,要改为只读的不能用ModifyStyle或SetWindowLong,而是向文本框发送EM_SETREADONLY消息
      

  2.   

    Edit Control Styles
    To create an edit control using the CreateWindow or CreateWindowEx function, specify the EDIT class, appropriate window style constants, and a combination of the following edit control styles. After the control has been created, these styles cannot be modified, except as noted. ES_READONLY Prevents the user from typing or editing text in the edit control. 
    To change this style after the control has been created, use the EM_SETREADONLY message.
      

  3.   

    CEdit类有一个成员函数封装了这条消息,所以上述代码可改为:
    if ((pEdit->GetStyle() & ES_READONLY) == 0x0)
    {
    pEdit->SetReadOnly(true);    }