65.9K
CodeProject 正在变化。 阅读更多。
Home

只读 ComboBox

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.53/5 (10投票s)

2000 年 8 月 7 日

viewsIcon

124401

downloadIcon

1576

显示一个禁用的下拉式组合框,使其看起来像一个只读的编辑框。

  • 下载源代码文件 - 1 Kb
  • Sample Image - readonlycombo.jpg

    引言

    这是一个快速修复,用于将禁用的组合框显示为只读。 如果您的表单中有许多只读编辑框,那么普通的禁用组合框看起来有点奇怪。 只读编辑框具有黑色文本和灰色背景。 而禁用的组合框具有深灰色文本和灰色背景。 很难向用户解释。

    此代码仅适用于下拉式组合框。 这是因为当父组合框被禁用时,我将子编辑框设置为启用和只读。

    当组合框收到 WM_ENABLE 消息时,检查 bEnable 标志,并为编辑框调用 EnableWindowSetReadOnly。 通过获取组合框的第一个子窗口 GetWindow(GW_CHILD) 来检索指向编辑框的指针。

    BEGIN_MESSAGE_MAP(CReadOnlyComboBox, CComboBox)
    	...
    	ON_WM_ENABLE()
    	...
    END_MESSAGE_MAP() 
    
    void  CReadOnlyComboBox::OnEnable(BOOL bEnable)
    {
    	CComboBox::OnEnable(bEnable); 
    
    	// Get edit control which happens to be the first child window
    	CEdit* pEdit = (CEdit*)GetWindow(GW_CHILD);
    	
    	// Always have the edit box enabled
    	pEdit->EnableWindow(TRUE);
    	
    	// Set read only is combo box is disabled
    	pEdit->SetReadOnly(!bEnable);
    } 
    

    这种方法的唯一缺点是文本无法像只读编辑框一样用鼠标选择。

    © . All rights reserved.