⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 readonlycombobox.cpp

📁 readonly combobox
💻 CPP
字号:
// ReadOnlyComboBox.cpp : implementation file
//

#include "stdafx.h"
#include "ReadOnlyComboBox.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CReadOnlyComboBox

CReadOnlyComboBox::CReadOnlyComboBox()
{
}

CReadOnlyComboBox::~CReadOnlyComboBox()
{
}

BEGIN_MESSAGE_MAP(CReadOnlyComboBox, CComboBox)
	//{{AFX_MSG_MAP(CReadOnlyComboBox)
	ON_CONTROL_REFLECT(CBN_SELCHANGE, OnSelchange)
	ON_CONTROL_REFLECT(CBN_EDITUPDATE, OnEditupdate)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CReadOnlyComboBox message handlers

void CReadOnlyComboBox::OnSelchange() 
{
	// If any item except item 0 (the editable item) is selected, then
	// we should make the edit control of the combo box readonly. If item 0
	// is selected we should make the edit control of the combo box editable.

	CEdit*	pEdit = (CEdit*)GetWindow(GW_CHILD);
	pEdit->SetReadOnly(GetCurSel() != 0);
}


void CReadOnlyComboBox::OnEditupdate() 
{
	// The user have changed the text in the edit field. 
	// We need to update the editable item number 0.
	// This is done by deleting the first item and adding a new 
	// item with the new text in its place

	CEdit* pEdit = (CEdit*)GetWindow(GW_CHILD);
	CString strChanged;
	pEdit->GetWindowText(strChanged);
	
	DeleteString(0);
	
	// DeleteString() will clear out the edit field in certain cases so we need to update it so.
	// These cases is when using the keyboard arrow buttons to navigate the combo box. With the
	// arrow buttons select a read only item and then the first item. Enter a character in the
	// edit field. This item will not be displayed, but next character will. To solve this we check
	// for this case and handle it below.

	CString strTest;
	pEdit->GetWindowText(strTest); 

	if (strTest.IsEmpty() && strTest != strChanged)
	{
		pEdit->SetWindowText(strChanged);
		pEdit->SetSel(strChanged.GetLength(), strChanged.GetLength());		
	}
	
	// Update the first item with the new changed text.

	InsertString(0, strChanged);	
}

void CReadOnlyComboBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
{
	//  Attach the given output device context to a CDC object we can use.

	CDC dc;
	dc.Attach(lpDrawItemStruct->hDC);

	// Set backgroud color.
	
	if (lpDrawItemStruct->itemState & ODS_SELECTED)
	{
		// The user have selected an item, we need to highlight it.

		dc.SetBkColor(GetSysColor(COLOR_HIGHLIGHT));
	}
	else if (lpDrawItemStruct->itemID == 0)
	{
		// The editable item, should have default color.

		dc.SetBkColor(GetSysColor(COLOR_WINDOW));
	}
	else
	{
		// A non editable, readonly item. We use same color as for a message box.
		// For some reason it looks darker when used in a combo box.

		dc.SetBkColor(GetSysColor(CTLCOLOR_MSGBOX));
	}	
	
	// Get the text from combo box item. For this to work, the combo box resource must have the property called "Has strings" enabled.

	CString strText;
	GetLBText(lpDrawItemStruct->itemID, strText);

	// Set text in item, we need to do this manually since we handling all 

	dc.ExtTextOut(lpDrawItemStruct->rcItem.left, lpDrawItemStruct->rcItem.top, ETO_OPAQUE, &lpDrawItemStruct->rcItem, strText, strText.GetLength (), NULL);

	// Restore background color.

	dc.SetBkColor(GetSysColor(COLOR_WINDOW));

	// Detach the given output device context from the CDC object.

	dc.Detach();
}

int CReadOnlyComboBox::AddString(const CString& strItem)
{
	// Add the string, for this to work, the combo box resource must have the property called "Has strings" enabled.

	int nIndex = CComboBox::AddString(strItem);

	// For the first added item we update the edit field of the combo box and set selected item as the first one.

	if (nIndex == 0)
	{
		CEdit* pEdit = (CEdit*)GetWindow(GW_CHILD);
		pEdit->SetWindowText(strItem);
		SetCurSel(0);
	}

	return nIndex;
}

void CReadOnlyComboBox::SetNumbersOnly()
{
	// Only allow digits to be entered into the edit control of the combo box.

	CEdit* pEdit = (CEdit*)GetWindow(GW_CHILD);
	pEdit->ModifyStyle(0, ES_NUMBER);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -