理论上探讨:可以设置只读模式的ComboBox SetReadOnly()不可实用

注意:此方法在于调整编辑框的父子关系,经测试,大部分情况下布局会出问题,因此只在理论上比较有趣,实践上不可实用

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;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值