📄 advcombobox.cpp
字号:
}
LONG CAdvComboBox::OnDestroyDropdownList( WPARAM wParam, LPARAM lParam )
{
//
//
if( m_pDropWnd )
{
m_pDropWnd->GetWindowRect( &m_rcDropWnd );
m_bDropRectStored = true;
m_pDropWnd->ShowWindow( SW_HIDE );
m_bDropListVisible = FALSE;
m_pDropWnd->DestroyWindow();
delete m_pDropWnd;
m_pDropWnd = NULL;
Invalidate();
int nId = GetDlgCtrlID();
if( !m_bSelItem )
{
GetParent()->SendMessage( WM_COMMAND, MAKEWPARAM(nId,CBN_SELENDCANCEL), (LPARAM)m_hWnd );
}
else
{
m_bSelItem = false;
}
GetParent()->SendMessage( WM_COMMAND, MAKEWPARAM(nId,CBN_CLOSEUP), (LPARAM)m_hWnd );
}
return TRUE;
}
void CAdvComboBox::CreateDropList( list<LIST_ITEM> &droplist)
{
CRect rc;
if( m_pDropWnd )
ASSERT(0);
m_pDropWnd = new CDropWnd( this, droplist, m_dwACBStyle );
GetWindowRect( &rc );
rc.top = rc.bottom ;
// Get height by calc items in list times itemheight
int nCount = (int)m_list.size();
//
// Get screen size
CRect rcWorkArea;
SystemParametersInfo( SPI_GETWORKAREA, 0, (LPRECT)rcWorkArea, 0) ;
if( rc.bottom >= rcWorkArea.bottom )
{
rc.bottom = rcWorkArea.bottom;
}
else
{
}
int nStyle = WS_CHILD|WS_BORDER|LBS_DISABLENOSCROLL|LBS_NOTIFY;
m_pDropWnd->Create( nStyle , rc, 1 ? GetDesktopWindow() : this, 6 );
m_pDropWnd->GetListBoxPtr()->SetCurSel( m_nCurSel );
m_pDropWnd->SetFont( m_pFont );
// Send message to parent(dialog)
int nId = GetDlgCtrlID();
GetParent()->SendMessage( WM_COMMAND, MAKEWPARAM(nId,CBN_DROPDOWN), (LPARAM)m_hWnd );
}
int CAdvComboBox::GetLBText(int nIndex, LPTSTR lpszText)
{
m_iter = m_list.begin();
advance( m_iter, nIndex );
if( m_iter == m_list.end() || nIndex > (int)m_list.size() )
{
return CB_ERR;
}
strcpy( lpszText, m_iter->strText.c_str() );
return (int)m_iter->strText.length()+1;
}
void CAdvComboBox::GetLBText(int nIndex, CString &rString)
{
m_iter = m_list.begin();
advance( m_iter, nIndex );
if( m_iter == m_list.end() || nIndex > (int)m_list.size() )
{
rString = "";
return;
}
rString = m_iter->strText.c_str();
}
int CAdvComboBox::GetLBTextLen(int nIndex )
{
m_iter = m_list.begin();
advance( m_iter, nIndex );
if( m_iter == m_list.end() || nIndex > (int)m_list.size() )
{
return CB_ERR;
}
return (int)m_iter->strText.length()+1;
}
int CAdvComboBox::AddString(LPCTSTR lpszString)
{
LIST_ITEM item;
item.strText = lpszString;
item.bChecked = false;
item.bDisabled = false;
item.vpItemData = NULL;
m_list.push_back( item );
if( GetStyle() & CBS_SORT )
{
m_list.sort();
// Find new item
return FindString( -1, item.strText.c_str() );
}
else
return (int)m_list.size()-1;
}
int CAdvComboBox::GetText(LPTSTR lpszText)
{
if( m_pEdit )
{
CString str;
m_pEdit->GetWindowText( str );
strcpy( lpszText, (LPCTSTR)str );
return str.GetLength();
}
else
{
strcpy( lpszText, m_strEdit.c_str() );
return (int)m_strEdit.length();
}
}
void CAdvComboBox::GetText(CString &rString)
{
if( m_pEdit )
{
m_pEdit->GetWindowText( rString );
}
else
{
rString = m_strEdit.c_str();
}
}
void CAdvComboBox::SetText(LPCTSTR lpszText)
{
if( m_pEdit )
{
m_pEdit->SetWindowText( lpszText );
}
m_strEdit = lpszText;
}
BOOL CAdvComboBox::PointInWindow(CPoint ptScreenPoint)
{
CRect rc;
GetWindowRect( &rc );
return rc.PtInRect( ptScreenPoint );
}
BOOL CAdvComboBox::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
// TODO: Add your message handler code here and/or call default
if( !m_bDropListVisible )
{
string str;
//
// Select another string from the map
m_zDelta += zDelta;
if( m_zDelta >= WHEEL_DELTA )
{
//
// Select item upwards
m_zDelta = 0;
SelPrevItem();
}
else
if( m_zDelta <= -WHEEL_DELTA )
{
//
// Select item downwards
m_zDelta = 0;
SelNextItem();
}
}
else
{
//
// Handle mousewheel for the droplist here
//
// Select another string from the map
m_zDelta += zDelta;
if( m_zDelta >= WHEEL_DELTA )
{
//
// Scroll list upwards
m_zDelta = 0;
int nTop = m_pDropWnd->GetListBoxPtr()->GetTopIndex();
nTop -= 3;
nTop = nTop < 0 ? 0 : nTop;
m_pDropWnd->GetListBoxPtr()->SetTopIdx( nTop, TRUE );
}
else
if( m_zDelta <= -WHEEL_DELTA )
{
//
// Scroll list downwards
m_zDelta = 0;
int nTop = m_pDropWnd->GetListBoxPtr()->GetTopIndex();
nTop += 3;
nTop = nTop > m_pDropWnd->GetListBoxPtr()->GetCount() ? m_pDropWnd->GetListBoxPtr()->GetCount() : nTop;
m_pDropWnd->GetListBoxPtr()->SetTopIdx( nTop, TRUE );
}
}
return TRUE;
// return CWnd::OnMouseWheel(nFlags, zDelta, pt);
}
void CAdvComboBox::OnSize(UINT nType, int cx, int cy) {
CWnd::OnSize(nType, cx, cy);
if((GetStyle()&CBS_DROPDOWN)&&!(GetStyle()&CBS_SIMPLE)){
CRect rect;
GetClientRect(rect);
rect.right = rect.right - ::GetSystemMetrics(SM_CXHSCROLL);
m_pEdit->MoveWindow(&rect);
}
}
void CAdvComboBox::OnSetFocus(CWnd* pOldWnd)
{
CWnd::OnSetFocus(pOldWnd);
// TODO: Add your message handler code here
m_bHasFocus = true;
Invalidate();
// OutputDebugString( "AdvComboBox got Focus.\n" );
//
// Set focus to the edit control? (CBS_DROPDOWN)
if( (GetStyle() & CBS_DROPDOWN) && !(GetStyle() & CBS_SIMPLE) ) // == CBS_DROPDOWN
{
if( m_pEdit )
{
m_pEdit->SetFocus();
}
}
BOOL bDropdownList = (GetStyle() & CBS_DROPDOWN) && (GetStyle() & CBS_SIMPLE);
if( bDropdownList )
{
// Send message to parent(dialog)
int nId = GetDlgCtrlID();
GetParent()->SendMessage( WM_COMMAND, MAKEWPARAM(nId,CBN_SETFOCUS), (LPARAM)m_hWnd );
}
}
void CAdvComboBox::OnSetfocusEdit()
{
m_bHasFocus = false;
CWnd* pWnd = GetFocus();
if( !m_bHasSentFocus )
{
// Send message to parent(dialog)
int nId = GetDlgCtrlID();
GetParent()->SendMessage( WM_COMMAND, MAKEWPARAM(nId,CBN_SETFOCUS), (LPARAM)m_hWnd );
m_bHasSentFocus = true;
}
}
void CAdvComboBox::OnKillFocus(CWnd* pNewWnd)
{
CWnd::OnKillFocus(pNewWnd);
// Needed for keydown's like 'Alt-C'("&Cancel" button)
if( m_pDropWnd )
{
OnDestroyDropdownList(0,0);
}
m_bHasFocus = false;
Invalidate();
BOOL bDropdownList = (GetStyle() & CBS_DROPDOWN) && (GetStyle() & CBS_SIMPLE);
if( bDropdownList && !m_pDropWnd )
{
// Send message to parent(dialog)
int nId = GetDlgCtrlID();
GetParent()->SendMessage( WM_COMMAND, MAKEWPARAM(nId,CBN_SELENDCANCEL), (LPARAM)m_hWnd );
GetParent()->SendMessage( WM_COMMAND, MAKEWPARAM(nId,CBN_KILLFOCUS), (LPARAM)m_hWnd );
}
}
void CAdvComboBox::OnKillfocusEdit()
{
m_bHasFocus = false;
Invalidate();
CWnd* pWnd = GetFocus();
if( !m_pDropWnd && pWnd != this )
{
// Send message to parent(dialog)
int nId = GetDlgCtrlID();
GetParent()->SendMessage( WM_COMMAND, MAKEWPARAM(nId,CBN_SELENDCANCEL), (LPARAM)m_hWnd );
GetParent()->SendMessage( WM_COMMAND, MAKEWPARAM(nId,CBN_KILLFOCUS), (LPARAM)m_hWnd );
m_bHasSentFocus = false;
m_pEdit->SetSel(0,0);
}
}
void CAdvComboBox::OnShowWindow(BOOL bShow, UINT nStatus)
{
CWnd::OnShowWindow(bShow, nStatus);
}
void CAdvComboBox::ModifyACBStyle(UINT nRemoveStyle, UINT nAddStyle)
{
if( nAddStyle & ACBS_FLAT )
{
nRemoveStyle |= ACBS_STANDARD;
}
else
if( nAddStyle & ACBS_STANDARD )
{
nRemoveStyle |= ACBS_FLAT;
}
m_dwACBStyle &= ~nRemoveStyle;
m_dwACBStyle |= nAddStyle;
Invalidate();
}
int CAdvComboBox::GetCount()
{
return (int)m_list.size();
}
int CAdvComboBox::GetCurSel()
{
CString str;
GetText( str );
return FindStringExact( -1, str );
}
int CAdvComboBox::SetCurSel(int nSelect)
{
if( nSelect == -1 )
{
m_nCurSel = nSelect;
m_strEdit = "";
Invalidate();
return CB_ERR;
}
else
if( m_list.size() == 0 )
{
m_nCurSel = nSelect;
return CB_ERR;
}
else
if( nSelect < -1 || nSelect > (int)m_list.size()-1 )
{
return CB_ERR;
}
else
{
m_nCurSel = nSelect;
m_iter = m_list.begin();
advance( m_iter, nSelect );
m_strEdit = m_iter->strText;
Invalidate();
return m_nCurSel;
}
}
int CAdvComboBox::FindString(int nStartAfter, LPCTSTR lpszString)
{
int nPos = 0;
m_iter = m_list.begin();
if( nStartAfter != -1 )
{
advance( m_iter, nStartAfter );
nPos = nStartAfter;
}
for( m_iter; m_iter != m_list.end(); ++m_iter )
{
if( strncmp( m_iter->strText.c_str(), lpszString, strlen(lpszString) ) == 0 )
{
return nPos;
}
nPos++;
}
return CB_ERR;
}
int CAdvComboBox::FindStringExact(int nIndexStart, LPCTSTR lpszFind)
{
if( nIndexStart > (int)m_list.size() && nIndexStart != -1 )
return CB_ERR;
int nPos = 0;
m_iter = m_list.begin();
if( nIndexStart != -1 )
{
advance( m_iter, nIndexStart );
nPos = nIndexStart;
}
for( m_iter; m_iter != m_list.end(); ++m_iter )
{
if( strcmp( m_iter->strText.c_str(), lpszFind ) == 0 )
{
return nPos;
}
nPos++;
}
return CB_ERR;
}
int CAdvComboBox::SelectString(int nStartAfter, LPCTSTR lpszString)
{
if( nStartAfter > (int)m_list.size() )
return CB_ERR;
int nPos = 0;
m_iter = m_list.begin();
if( nStartAfter != -1 )
{
advance( m_iter, nStartAfter );
nPos = nStartAfter;
}
for( m_iter; m_iter != m_list.end(); ++m_iter )
{
if( strcmp( m_iter->strText.c_str(), lpszString ) == 0 )
{
m_nCurSel = nPos;
m_strEdit = m_iter->strText;
Invalidate();
return nPos;
}
nPos++;
}
return CB_ERR;
}
int CAdvComboBox::SetItemData(int nIndex, DWORD dwItemData)
{
m_iter = m_list.begin();
advance( m_iter, nIndex );
if( m_iter == m_list.end() || nIndex > (int)m_list.size() )
{
return CB_ERR;
}
m_iter->vpItemData = (void*)dwItemData;
return CB_OKAY;
}
DWORD CAdvComboBox::GetItemData(int nIndex)
{
m_iter = m_list.begin();
advance( m_iter, nIndex );
if( m_iter == m_list.end() || nIndex > (int)m_list.size() )
{
return CB_ERR;
}
return (DWORD)m_iter->vpItemData;
}
int CAdvComboBox::SetItemDataPtr(int nIndex, void *pData)
{
m_iter = m_list.begin();
advance( m_iter, nIndex );
if( m_iter == m_list.end() || nIndex > (int)m_list.size() )
{
return CB_ERR;
}
m_iter->vpItemData = pData;
return CB_OKAY;
}
void* CAdvComboBox::GetItemDataPtr(int nIndex)
{
m_iter = m_list.begin();
advance( m_iter, nIndex );
if( m_iter == m_list.end() || nIndex > (int)m_list.size() )
{
return (void*)CB_ERR;
}
return m_iter->vpItemData;
}
void CAdvComboBox::ResetContent()
{
m_list.clear();
m_strEdit = "";
Invalidate();
}
void AFXAPI DDX_ACBIndex( CDataExchange* pDX, int nIDC, int& index )
{
HWND hWnd = pDX->PrepareCtrl( nIDC );
if( pDX->m_bSaveAndValidate )
{
CAdvComboBox* pACB = (CAdvComboBox*)pDX->m_pDlgWnd->GetDlgItem( nIDC );
index = pACB->GetCurSel();
}
else
{
CAdvComboBox* pACB = (CAdvComboBox*)pDX->m_pDlgWnd->GetDlgItem( nIDC );
pACB->SetCurSel( index );
}
}
void AFXAPI DDX_ACBString( CDataExchange* pDX, int nIDC, CString& value )
{
HWND hWnd = pDX->PrepareCtrl( nIDC );
if( pDX->m_bSaveAndValidate )
{
CAdvComboBox* pACB = (CAdvComboBox*)pDX->m_pDlgWnd->GetDlgItem( nIDC );
pACB->GetText( value );
}
else
{
CAdvComboBox* pACB = (CAdvComboBox*)pDX->m_pDlgWnd->GetDlgItem( nIDC );
pACB->SetText( value );
}
}
BOOL CAdvComboBox::GetItemDisabled( int nIndex )
{
if( nIndex > (int)m_list.size() )
return CB_ERR;
m_iter = m_list.begin();
advance( m_iter, nIndex );
return m_iter->bDisabled;
}
void CAdvComboBox::SetItemDisabled(int nIndex, BOOL bDisabled)
{
if( nIndex > (int)m_list.size() )
return;
m_iter = m_list.begin();
advance( m_iter, nIndex );
m_iter->bDisabled = bDisabled;
}
BOOL CAdvComboBox::GetItemChecked( int nIndex )
{
if( nIndex > (int)m_list.size() )
return CB_ERR;
m_iter = m_list.begin();
advance( m_iter, nIndex );
return m_iter->bChecked;
}
void CAdvComboBox::SetItemChecked(int nIndex, BOOL bChecked)
{
if( nIndex > (int)m_list.size() )
return;
m_iter = m_list.begin();
advance( m_iter, nIndex );
m_iter->bChecked = bChecked;
}
BOOL CAdvComboBox::PreTranslateMessage(MSG* pMsg)
{
if( pMsg->message == WM_KEYDOWN )
{
m_bAutoAppend = TRUE;
// OutputDebugString( "Key was pressed(AdvComboBox)" );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -