📄 itemcombobox.cpp
字号:
#include "stdafx.h"
#include "ItemComboBox.h"
#include ".\itemcombobox.h"
// CItemComboBox
IMPLEMENT_DYNAMIC(CItemComboBox, CComboBox)
CItemComboBox::CItemComboBox()
{
m_pList = NULL;
m_nItem = -1;
m_nSubItem = -1;
}
CItemComboBox::CItemComboBox(CListCtrl *pList, int nItem, int nSubItem, CMapStringToString *pMapStr/* = NULL*/, int nCurSel/* = -1*/)
{
m_pList = pList;
m_nItem = nItem;
m_nSubItem = nSubItem;
Create();
if ( pMapStr != NULL )
SetStringMap( *pMapStr, nCurSel );
}
void CItemComboBox::SetListCtrl(CListCtrl *pList, int nItem, int nSubItem)
{
m_pList = pList;
m_nItem = nItem;
m_nSubItem = nSubItem;
}
CItemComboBox::~CItemComboBox()
{
}
BEGIN_MESSAGE_MAP(CItemComboBox, CComboBox)
ON_WM_KILLFOCUS()
END_MESSAGE_MAP()
void CItemComboBox::OnKillFocus(CWnd* pNewWnd)
{
CComboBox::OnKillFocus(pNewWnd);
if ( m_pList != NULL )
{
CString strText;
CString strValue;
int nCurSel = GetCurSel();
if (nCurSel != CB_ERR)
{
GetLBText( nCurSel, strText );
if ( m_mapStr.Lookup( strText, strValue ) )
{
m_pList->SetItemText( m_nItem, m_nSubItem, strValue );
}
}
m_pList->Invalidate();
}
delete this;
}
BOOL CItemComboBox::Create()
{
CRect rect;
int nHeight = 12;// default value
if ( m_pList != NULL )
{
m_pList->GetSubItemRect( m_nItem, m_nSubItem, LVIR_BOUNDS, rect );
nHeight = rect.Height() - 2;
rect.bottom += 80;
// if the m_nSubItem == 0, the rect will be the whole row's rect. Code below would avoid this case
rect.right = rect.left + m_pList->GetColumnWidth( m_nSubItem );
}
BOOL bRet = CComboBox::Create( WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST | CBS_AUTOHSCROLL, rect, m_pList, 0x1234 );
TRACE1( _T("Height: %d\r\n"), nHeight );
m_font.CreateFont(
nHeight, // nHeight
0, // nWidth
0, // nEscapement
0, // nOrientation
FW_NORMAL, // nWeight
FALSE, // bItalic
FALSE, // bUnderline
0, // cStrikeOut
ANSI_CHARSET, // nCharSet
OUT_DEFAULT_PRECIS, // nOutPrecision
CLIP_DEFAULT_PRECIS, // nClipPrecision
DEFAULT_QUALITY, // nQuality
DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
"Arial");
SetFont( &m_font );
SetFocus();
return bRet;
}
void CItemComboBox::SetStringMap(CMapStringToString &mapStr, int nCurSel/* = -1*/)
{
POSITION pos = mapStr.GetStartPosition();
CString strKey, strValue;
CString & strText = m_pList->GetItemText( m_nItem, m_nSubItem );
m_mapStr.RemoveAll();
int nMatchSel = 0, nIndex = 0;
while ( pos != NULL )
{
mapStr.GetNextAssoc( pos, strKey, strValue );// copy from mapStr, if reserve the pointer, ItemComboBox would not keep independent
m_mapStr.SetAt( strKey, strValue );
if ( strText == strValue )// find the last match item
nMatchSel = nIndex;
InsertString( nIndex++, strKey );// strKey: show in the combobox, strValue: show in the list ctrl
}
if ( nCurSel == -1 )
{
SetCurSel( nMatchSel );
}
else
{
SetCurSel( nCurSel );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -