📄 advcombobox.cpp
字号:
hr = g_xpStyle.GetThemeColor( hTheme, WP_DIALOG, 0, TMT_FILLCOLOR, &clrDisabledFill );
if( FAILED( hr ) )
{
clrDisabledFill = RGB(255,0,0);
}
CPen penDisBorder( PS_SOLID, 0, clrDisabledLightBorder );
CBrush brFill( clrDisabledBkg );//clrDisabledFill );
CRect rcl = rect;
rcl.DeflateRect(1,1);
rcl.right = m_rcDropButton.left;
CBrush *oldBr = dc.SelectObject( &brFill );
dc.SelectObject( &penDisBorder );
dc.Rectangle( &rcl );
dc.SelectObject( oldBr );
}
dc.SelectObject( &oldBorderPen );
// Button
hr = g_xpStyle.DrawThemeBackground( hTheme, dc.m_hDC, CP_DROPDOWNBUTTON, nDropBtnThemeStyle, &m_rcDropButton, NULL);
}
else
{
COLORREF clrTopLeft = ::GetSysColor(COLOR_3DSHADOW);
COLORREF clrBottomRight = ::GetSysColor(COLOR_3DHILIGHT);
dc.Draw3dRect( &rect, clrTopLeft, clrBottomRight );
clrTopLeft = ::GetSysColor(COLOR_3DDKSHADOW);
clrBottomRight = ::GetSysColor(COLOR_3DLIGHT);
rect.DeflateRect(1,1);
dc.Draw3dRect( &rect, clrTopLeft, clrBottomRight );
m_rcDropButton.DeflateRect(0,2,0,2);
m_rcDropButton.left -= 2;
m_rcDropButton.right -= 2;
// Button
dc.DrawFrameControl(m_rcDropButton, DFC_SCROLL, dwBtnStyle );
}
//
// Adjust rects
rcText.DeflateRect(4,3,2,3);
}
if( bThemeActive )
hr = g_xpStyle.CloseThemeData( hTheme );
if( (GetStyle() & CBS_DROPDOWN) && (GetStyle() & CBS_SIMPLE) ) // == CBS_DROPDOWNLIST
{
//
// Draw Text as selected
COLORREF clrBackground;
COLORREF clrOldBkColor;
COLORREF clrOldTextColor;
clrBackground = ::GetSysColor(COLOR_HIGHLIGHT);
clrOldBkColor = dc.SetBkColor( clrBackground );
// clrOldTextColor = dc.SetTextColor( ::GetSysColor(COLOR_HIGHLIGHTTEXT) );
int nOldBkMode = dc.SetBkMode( TRANSPARENT );
CFont* pOldFont = dc.SelectObject( m_pFont );
rcText.top -= 2;
rcText.bottom += 2;
rcText.left -= 2;
rcText.right += 1;
if( m_bHasFocus && !m_bDropListVisible )
{
dc.FillSolidRect( rcText, bWndEnabled ? clrBackground : clrDisabledBkg );
clrOldTextColor = dc.SetTextColor(
bWndEnabled ? ::GetSysColor(COLOR_HIGHLIGHTTEXT) : clrDisabledText );
dc.DrawText( m_strEdit.c_str(), &rcText, DT_SINGLELINE|DT_VCENTER);
}
else
{
//+++dc.FillSolidRect( rcText,
//+++ bWndEnabled ? ::GetSysColor(COLOR_HIGHLIGHTTEXT) : clrDisabledBkg );
dc.FillSolidRect( rcText,
bWndEnabled ? ::GetSysColor(COLOR_WINDOW) : clrDisabledBkg );
clrOldTextColor = dc.SetTextColor(
bWndEnabled ? ::GetSysColor(COLOR_BTNTEXT) : clrDisabledText );
dc.DrawText( m_strEdit.c_str(), &rcText, DT_SINGLELINE|DT_VCENTER);
}
dc.SelectObject( pOldFont );
dc.SetBkMode( nOldBkMode );
}
else
{
if( m_pEdit )
{
m_pEdit->SetFont( m_pFont );
}
}
// Do not call CWnd::OnPaint() for painting messages
}
BOOL CAdvComboBox::OnEraseBkgnd(CDC* pDC)
{
return CWnd::OnEraseBkgnd(pDC);
// return TRUE;
}
int CAdvComboBox::SetItemHeight(int nIndex, int nHeight)
{
if( nIndex == -1 )
{
if( nHeight < 10 || nHeight > 50 )
{
return CB_ERR;
}
else
{
//
// Button rect
GetClientRect(m_rcDropButton);
m_rcDropButton.left = m_rcDropButton.right - ::GetSystemMetrics(SM_CXHSCROLL);
return 0;
}
}
return CB_ERR;
}
void CAdvComboBox::OnLButtonDown(UINT nFlags, CPoint point)
{
if( GetFocus() != this )
{
SetFocus();
}
//
// Is mouse over drop button?
if( (GetStyle() & CBS_DROPDOWN) && !(GetStyle() & CBS_SIMPLE) ) // == CBS_DROPDOWN
{
if( m_rcDropButton.PtInRect( point ) )
{
SendMessage( WM_ON_DROPDOWN_BUTTON );
InvalidateRect( m_rcDropButton );
Invalidate();
}
}
else
if( (GetStyle() & CBS_DROPDOWN) && (GetStyle() & CBS_SIMPLE) ) // == CBS_DROPDOWNLIST
{
CRect rc = m_rcCombo;
GetClientRect( &rc );
if( rc.PtInRect( point ) )
{
SendMessage( WM_ON_DROPDOWN_BUTTON );
Invalidate();
}
}
CWnd::OnLButtonDown(nFlags, point);
}
LONG CAdvComboBox::OnSelectedItem( WPARAM wParam, LPARAM /*lParam*/ )
{
list<LIST_ITEM> itemlist;
list<LIST_ITEM>::iterator itemiter;
int nPos = (int)wParam;
itemlist = m_pDropWnd->GetList();
itemiter = itemlist.begin();
advance( itemiter, nPos );
m_strEdit = itemiter->strText;
m_nCurSel = FindStringExact( 0, m_strEdit.c_str() );
SetWindowText( m_strEdit.c_str() );
if( (GetStyle() & CBS_DROPDOWN) && !(GetStyle() & CBS_SIMPLE) ) // == CBS_DROPDOWN
{
if( m_pEdit )
{
m_pEdit->SetWindowText( m_strEdit.c_str() );
m_pEdit->SetFocus();
m_pEdit->SetSel( 0, -1, TRUE );
}
}
// Send message to parent(dialog)
m_bSelItem = true;
int nId = GetDlgCtrlID();
m_pParent->SendMessage( WM_COMMAND, MAKEWPARAM(nId,CBN_SELENDOK), (LPARAM)m_hWnd );
Invalidate();
OnDestroyDropdownList(0,0);
//
// See to it that the drop button is redrawn
InvalidateRect( m_rcDropButton );
m_pParent->SendMessage( WM_COMMAND, MAKEWPARAM(nId,CBN_SELCHANGE), (LPARAM)m_hWnd );
return TRUE;
}
LONG CAdvComboBox::OnDropdownButton( WPARAM /*wParam*/, LPARAM /*lParam*/ )
{
// XLISTCTRL_TRACE(_T("in CAdvComboBox::OnDropdownButton\n"));
//
//
if( !m_bDropListVisible )
{
//
// Button is pressed
//
// Create list
if( !m_pDropWnd )
{
CreateDropList( m_list );
}
m_pDropWnd->ShowWindow( SW_SHOW );
m_bDropListVisible = TRUE;
}
else
{
OnDestroyDropdownList(0,0);
}
// Return TRUE if OK to go back, else return FALSE.
return TRUE;
}
LONG CAdvComboBox::OnDestroyDropdownList( WPARAM /*wParam*/, LPARAM /*lParam*/ )
{
// XLISTCTRL_TRACE(_T("in CAdvComboBox::OnDestroyDropdownList\n"));
//
//
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;
InvalidateRect( &m_rcDropButton );
int nId = GetDlgCtrlID();
if( !m_bSelItem )
{
m_pParent->SendMessage( WM_COMMAND, MAKEWPARAM(nId,CBN_SELENDCANCEL),
(LPARAM)m_hWnd );
}
else
{
m_bSelItem = false;
}
m_pParent->SendMessage( WM_COMMAND, MAKEWPARAM(nId,CBN_CLOSEUP), (LPARAM)m_hWnd );
}
else
{
OnComboComplete();
}
return TRUE;
}
void CAdvComboBox::CreateDropList( list<LIST_ITEM> &droplist)
{
// XLISTCTRL_TRACE(_T("in CAdvComboBox::CreateDropList\n"));
CRect rc;
if( m_pDropWnd )
ASSERT(0);
m_pDropWnd = new CDropWnd( this, droplist, m_dwACBStyle );
GetWindowRect( &rc );
rc.top = rc.bottom ;
//
// 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( 0, 0, nStyle , rc, 1 ? GetDesktopWindow() : this, 6 );
//+++
if (m_nCurSel > ((int)droplist.size() - 1))
m_nCurSel = 0;
m_pDropWnd->GetListBoxPtr()->SetCurSel( m_nCurSel );
m_pDropWnd->SetFont( m_pFont );
// Send message to parent(dialog)
int nId = GetDlgCtrlID();
m_pParent->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;
}
_tcscpy( lpszText, m_iter->strText.c_str() );
return 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 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 );
// this takes too long -
// sorting is handled in CXListCtrl::SetComboBox()
//if( GetStyle() & CBS_SORT )
//{
// m_list.sort();
// Find new item
// return FindString( -1, item.strText.c_str() );
//}
//else
return m_list.size()-1;
}
int CAdvComboBox::GetText(LPTSTR lpszText)
{
if( m_pEdit )
{
CString str;
m_pEdit->GetWindowText( str );
_tcscpy( lpszText, (LPCTSTR)str );
return str.GetLength();
}
else
{
_tcscpy( lpszText, m_strEdit.c_str() );
return 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);
//
// Move Dropdown?
}
void CAdvComboBox::OnSetFocus(CWnd* pOldWnd)
{
CWnd::OnSetFocus(pOldWnd);
// TODO: Add your message handler code here
m_bHasFocus = true;
Invalidate();
//
// 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();
m_pParent->SendMessage( WM_COMMAND, MAKEWPARAM(nId,CBN_SETFOCUS), (LPARAM)m_hWnd );
}
}
void CAdvComboBox::OnSetfocusEdit()
{
m_bHasFocus = false;
//CWnd* pWnd = GetFocus();
//+++
if (m_pEdit)
{
m_pEdit->PostMessage(EM_SETSEL, 0, -1);
}
if( !m_bHasSentFocus )
{
// Send message to parent(dialog)
int nId = GetDlgCtrlID();
m_pParent->SendMessage( WM_COMMAND, MAKEWPARAM(nId,CBN_SETFOCUS), (LPARAM)m_hWnd );
m_bHasSentFocus = true;
}
}
void CAdvComboBox::OnKillFocus(CWnd* pNewWnd)
{
// XLISTCTRL_TRACE(_T("in CAdvComboBox::OnKillFocus\n"));
CWnd::OnKillFocus(pNewWnd);
OnEscapeKey();
//+++
#if 0 // -----------------------------------------------------------
// 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();
m_pParent->SendMessage( WM_COMMAND, MAKEWPARAM(nId,CBN_SELENDCANCEL), (LPARAM)m_hWnd );
m_pParent->SendMessage( WM_COMMAND, MAKEWPARAM(nId,CBN_KILLFOCUS), (LPARAM)m_hWnd );
}
}
else
{
OnEscapeKey();
}
#endif // -----------------------------------------------------------
}
void CAdvComboBox::OnKillfocusEdit()
{
m_bHasFocus = false;
Invalidate();
CWnd* pWnd = GetFocus();
if( !m_pDropWnd && pWnd != this )
{
// Send message to parent(dialog)
int nId = GetDlgCtrlID();
m_pParent->SendMessage( WM_COMMAND, MAKEWPARAM(nId,CBN_SELENDCANCEL), (LPARAM)m_hWnd );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -