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

来源:互联网 发布:mac精确模式怎么退出 编辑:程序博客网 时间:2024/05/16 04:59

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

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 windowclass CComboBoxX : public CComboBox{// Constructionpublic:CComboBoxX();// Attributespublic:HWND m_hEdit;// Operationspublic:// Overrides// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CComboBoxX)protected:virtual void PreSubclassWindow();//}}AFX_VIRTUAL// Implementationpublic:virtual ~CComboBoxX();BOOL EnableWindow( BOOL bEnable = TRUE );BOOL SetReadOnly( BOOL bReadOnly = TRUE );// Generated message map functionsprotected://{{AFX_MSG(CComboBoxX)//}}AFX_MSGDECLARE_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_FILEstatic char THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////////////// CComboBoxXCComboBoxX::CComboBoxX(){m_hEdit = NULL;}CComboBoxX::~CComboBoxX(){}BEGIN_MESSAGE_MAP(CComboBoxX, CComboBox)//{{AFX_MSG_MAP(CComboBoxX)//}}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CComboBoxX message handlersvoid CComboBoxX::PreSubclassWindow() {// TODO: Add your specialized code here and/or call the base classCRect rect;// Get EditBox's handlem_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 );}elsereturn 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 );}elsereturn TRUE;}


原创粉丝点击