注意:此方法在于调整编辑框的父子关系,经测试,大部分情况下布局会出问题,因此只在理论上比较有趣,实践上不可实用
vc6.0工程文件,演示可以设置只读模式的ComboBox,可以设置只读属性,SetReadOnly()。
目前发现的问题是在应用了xp样式后,文字显示时隐时现。
http://download.csdn.net/detail/wwwwws/5841909
#if !defined(AFX_COMBOBOXX_H__BF360CE4_B6DE_4AB9_96D8_82598BC5A43F__INCLUDED_)
#define AFX_COMBOBOXX_H__BF360CE4_B6DE_4AB9_96D8_82598BC5A43F__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// ComboBoxX.h : header file
//
/
// CComboBoxX window
class CComboBoxX : public CComboBox
{
// Construction
public:
CComboBoxX();
// Attributes
public:
HWND m_hEdit;
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CComboBoxX)
protected:
virtual void PreSubclassWindow();
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CComboBoxX();
BOOL EnableWindow( BOOL bEnable = TRUE );
BOOL SetReadOnly( BOOL bReadOnly = TRUE );
// Generated message map functions
protected:
//{{AFX_MSG(CComboBoxX)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_COMBOBOXX_H__BF360CE4_B6DE_4AB9_96D8_82598BC5A43F__INCLUDED_)
// ComboBoxX.cpp : implementation file
//
#include "stdafx.h"
#include "ComboBoxX.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/
// CComboBoxX
CComboBoxX::CComboBoxX()
{
m_hEdit = NULL;
}
CComboBoxX::~CComboBoxX()
{
}
BEGIN_MESSAGE_MAP(CComboBoxX, CComboBox)
//{{AFX_MSG_MAP(CComboBoxX)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/
// CComboBoxX message handlers
void CComboBoxX::PreSubclassWindow()
{
// TODO: Add your specialized code here and/or call the base class
CRect rect;
// Get EditBox's handle
m_hEdit = ::GetWindow( m_hWnd, GW_CHILD );
// Get EditBox's size
::GetWindowRect( m_hEdit, rect );
GetParent()->ScreenToClient( rect );
// Alter EditBox from child window to brother window
::SetParent( m_hEdit, ::GetParent( m_hWnd ) );
// Set EditBox's Z-order and its position in the parent client window
::SetWindowPos( m_hEdit, m_hWnd, rect.left, rect.top, rect.Width(), rect.Height(), NULL );
}
BOOL CComboBoxX::SetReadOnly( BOOL bReadOnly )
{
ASSERT( ::IsWindow( m_hEdit ) );
if ( ::IsWindowEnabled( m_hEdit ) )
{
CWnd::EnableWindow( !bReadOnly );
::EnableWindow( m_hEdit, TRUE );
return (BOOL)::SendMessage( m_hEdit, EM_SETREADONLY, bReadOnly, 0L );
}
else
return FALSE;
}
BOOL CComboBoxX::EnableWindow( BOOL bEnable )
{
ASSERT( ::IsWindow( m_hEdit ) );
if ( bEnable == FALSE )
{
::EnableWindow( m_hEdit, FALSE );
return CWnd::EnableWindow( FALSE );
}
else if ( ::IsWindowEnabled( m_hEdit ) == FALSE )
{
::EnableWindow( m_hEdit, TRUE );
return ( ::GetWindowLong( m_hEdit, GWL_STYLE ) & ES_READONLY ) ? TRUE : CWnd::EnableWindow( TRUE );
}
else
return TRUE;
}