📄 chklistbox.cpp
字号:
////////////////////////////////////////////////////////////////////////////
// User Functions
/////////////////////////////////////////////////////////////////////////////
LISTITEM* CCheckList::AddString( CString csText, LISTITEM* pParentItem, int nCheckState, DWORD dwID, COLORREF crTextColor, COLORREF crTextHighColor, COLORREF crBgHighlightColor )
{
// Calculate the Items width and height
CDC* pdc = GetWindowDC();
CFont* pOldFont = pdc->SelectObject(m_pTextFont);
CSize cText = pdc->GetTextExtent(csText);
pdc->SelectObject( pOldFont );
ReleaseDC(pdc);
// Make sure its the correct kind
if( nCheckState < UNCHECKED || nCheckState > INDETERMINATE )
return NULL;
// Create a new item
LISTITEM* pListItem = new LISTITEM;
pListItem->csText = csText;
pListItem->dwID = dwID;
pListItem->nLevel = 1;
pListItem->nTextLength = cText.cx;
pListItem->bSelected = false;
pListItem->nCheckedState = nCheckState;
pListItem->crTextColor = crTextColor;
pListItem->crTextHighColor = crTextHighColor;
pListItem->crBgHighlightColor = crBgHighlightColor;
pListItem->pParentItem = NULL;
// Add it to the correct list
if(pParentItem==NULL)
m_RootItemList.AddTail(pListItem);
else
{
pParentItem->m_ItemList.AddTail(pListItem);
pListItem->pParentItem = pParentItem;
}
// Set the level this item is at
LISTITEM* pCurrentItem = pListItem->pParentItem;
while( pCurrentItem )
{
pListItem->nLevel++;
pCurrentItem = pCurrentItem->pParentItem;
}
// Set this items width
pListItem->nTotalWidth = cText.cx + (pListItem->nLevel * m_cBitmapDimen.cx) + PADDING;
// Is this the longest item?
if( pListItem->nTotalWidth > m_nWidestItem )
m_nWidestItem = pListItem->nTotalWidth;
// Update the scroll bar
m_nTotalRowCount++;
m_pCheckControl->UpdateScrollBar();
Invalidate();
return pListItem;
}
int CCheckList::DeleteString(int nItem)
{
// Is this a valid item?
if( nItem < -1 || nItem > m_nTotalRowCount - 1 || m_pCheckControl == NULL)
return LB_ERR;
// Find this item
m_nCurrentRow = 0;
LISTITEM* pFoundItem = GetItem( &m_RootItemList, nItem );
if( pFoundItem == NULL )
return LB_ERR;
if(nItem==GetCurSel())
m_pCheckControl->SetValue();
// Recursivly delmete the children items
DeleteItems(&pFoundItem->m_ItemList);
// Delete this item (must find it in the parent list)
// Set the proper list to loop through
CPtrList* pParentList = &m_RootItemList;
if( pFoundItem->pParentItem != NULL )
pParentList = &pFoundItem->pParentItem->m_ItemList;
LISTITEM* pCurItem;
POSITION Pos = pParentList->GetHeadPosition();
POSITION OldPos;
while(Pos)
{
OldPos = Pos;
pCurItem = (LISTITEM*)pParentList->GetNext(Pos);
if( pCurItem == pFoundItem )
{
pParentList->RemoveAt(OldPos);
delete pCurItem;
break;
}
}
// Recalculate the widest item in the list
m_nWidestItem = 0;
GetWidestItem(&m_RootItemList); // (sets the member variable)
// Redraw from this element down
m_nTotalRowCount--;
m_pCheckControl->UpdateScrollBar();
m_pCheckControl->RedrawWindow();
return 0;
}
int CCheckList::GetCount(LISTITEM* pParentItem)
{
if(pParentItem)
return pParentItem->m_ItemList.GetCount();
return m_nTotalRowCount;
}
int CCheckList::GetTopIndex()
{
if(m_pCheckControl)
return m_pCheckControl->GetTopIndex();
return -1;
}
int CCheckList::SetTopIndex(int nTop)
{
if(m_pCheckControl)
{
// Is this too high
if( nTop > m_nTotalRowCount )
return LB_ERR;
// Are there not enough items to scroll
if( m_pCheckControl->GetViewableRows() > m_nTotalRowCount )
return 0;
// Set the top
if( nTop > m_nTotalRowCount - m_pCheckControl->GetViewableRows() )
nTop = m_nTotalRowCount - m_pCheckControl->GetViewableRows();
// Set the top
m_pCheckControl->SetTopIndex(nTop);
}
return 0;
}
DWORD CCheckList::GetItemData(int nItem)
{
// Find the item
m_nCurrentRow = 0;
LISTITEM* pFoundItem = GetItem( &m_RootItemList, nItem );
if( pFoundItem )
return pFoundItem->dwID;
return LB_ERR;
}
int CCheckList::SetItemData(int nItem, DWORD dwID)
{
// Find the item
m_nCurrentRow = 0;
LISTITEM* pItem = GetItem( &m_RootItemList, nItem );
if( pItem == NULL )
return LB_ERR;
pItem->dwID = dwID;
return 0;
}
LISTITEM* CCheckList::GetItem(int nItem)
{
// Find the item
m_nCurrentRow = 0;
return GetItem( &m_RootItemList, nItem );
}
LISTITEM* CCheckList::GetItem(DWORD dwID)
{
// Find the item
return GetItem( &m_RootItemList, dwID );
}
int CCheckList::GetText(int nItem, CString* pString )
{
// Find the item
m_nCurrentRow = 0;
LISTITEM* pItem = GetItem( &m_RootItemList, nItem );
if( pItem == NULL )
return LB_ERR;
// Copy the string
*pString = pItem->csText;
return 0;
}
int CCheckList::GetTextLen(int nItem )
{
// Find the item
m_nCurrentRow = 0;
LISTITEM* pItem = GetItem( &m_RootItemList, nItem );
if( pItem == NULL )
return LB_ERR;
return pItem->csText.GetLength();
}
int CCheckList::GetCurSel()
{
if( m_pCheckControl == NULL)
return LB_ERR;
return m_pCheckControl->GetCurSel();
//0;
}
int CCheckList::SetCurSel(int nItem)
{
// Is this a valid item?
if( nItem < -1 || nItem > m_nTotalRowCount - 1 || m_pCheckControl == NULL)
return LB_ERR;
return m_pCheckControl->SetCurSel(nItem);
}
void CCheckList::ResetContent()
{
// Delete all elements
DeleteItems(&m_RootItemList);
m_nTotalRowCount = 0;
m_nWidestItem = 0;
m_pCheckControl->UpdateScrollBar();
m_pCheckControl->RedrawWindow();
}
int CCheckList::SetCheck(int nItem, int nCheckState )
{
// Is this a valid item?
if( nItem < -1 || nItem > m_nTotalRowCount - 1 || m_pCheckControl == NULL)
return LB_ERR;
// Make sure its the correct kind
if( nCheckState < UNCHECKED || nCheckState > INDETERMINATE )
return LB_ERR;
return m_pCheckControl->SetCheck(nItem, nCheckState);
}
int CCheckList::GetCheck(int nItem)
{
// Is this a valid item?
if( nItem < -1 || nItem > m_nTotalRowCount - 1 || m_pCheckControl == NULL)
return LB_ERR;
m_nCurrentRow = 0;
LISTITEM* pItem = GetItem( &m_RootItemList, nItem );
if( pItem == NULL )
return LB_ERR;
return pItem->nCheckedState;
}
////////////////////////////////////////////////////////////////////////////
// Painting routines
/////////////////////////////////////////////////////////////////////////////
void CCheckList::OnPaint()
{
CPaintDC dc(this); // device context for painting
//////////////////////////////
// Paint the 3-D Border
CRect Rect;
GetClientRect(Rect);
CPen* pOldPen;
CPen DkGrayPen( PS_SOLID, 1, RGB(128,128,128));
CPen GrayPen( PS_SOLID, 1, RGB(192,192,192));
CPen WhitePen( PS_SOLID, 1, RGB(255,255,255));
CPen BlackPen( PS_SOLID, 1, RGB(0,0,0 ));
// Draw left and top
pOldPen = dc.SelectObject(&DkGrayPen);
dc.MoveTo( Rect.left, Rect.bottom);
dc.LineTo( Rect.left, Rect.top );
dc.LineTo( Rect.right, Rect.top );
// Draw the Black inner shadow
dc.SelectObject(&BlackPen);
dc.MoveTo( Rect.left + 1, Rect.bottom - 1);
dc.LineTo( Rect.left + 1, Rect.top + 1 );
dc.LineTo( Rect.right - 1, Rect.top + 1 );
// Draw White sides
dc.SelectObject(&WhitePen);
dc.MoveTo( Rect.right - 1, Rect.top + 1);
dc.LineTo( Rect.right - 1, Rect.bottom - 1);
dc.LineTo( Rect.left + 1, Rect.bottom - 1);
// Draw gray sides
dc.SelectObject(&GrayPen);
dc.MoveTo( Rect.right - 2, Rect.top + 2);
dc.LineTo( Rect.right - 2, Rect.bottom - 2);
dc.LineTo( Rect.left + 2, Rect.bottom - 2);
// Select the old tools
dc.SelectObject( pOldPen );
}
void CCheckList::OnSize(UINT nType, int cx, int cy)
{
CWnd::OnSize(nType, cx, cy);
// Move the Control
GetClientRect(m_WindowRect);
if( m_pCheckControl )
{
CRect ControlRect(m_WindowRect);
ControlRect.InflateRect(-2, -2);
m_pCheckControl->MoveWindow(ControlRect);
}
}
LISTITEM* CCheckList::GetItem( CPtrList* pItemList, int nItem )
{
// loop for all items
LISTITEM* pListItem = NULL;
POSITION Pos = pItemList->GetHeadPosition();
while(Pos)
{
// Get this item
pListItem = (LISTITEM*)pItemList->GetNext(Pos);
// Is this the item?
if( m_nCurrentRow == nItem )
return pListItem;
// Are there any open items in this?
m_nCurrentRow++;
if( pListItem->m_ItemList.GetCount() != 0 )
{
pListItem = GetItem( &pListItem->m_ItemList, nItem);
if( pListItem )
return pListItem;
}
}
return NULL;
}
LISTITEM* CCheckList::GetItem( CPtrList* pItemList, DWORD dwID )
{
// loop for all items
LISTITEM* pListItem = NULL;
POSITION Pos = pItemList->GetHeadPosition();
while(Pos)
{
// Get this item
pListItem = (LISTITEM*)pItemList->GetNext(Pos);
// Is this the item?
if( pListItem->dwID == dwID )
return pListItem;
// Are there any open items in this?
if( pListItem->m_ItemList.GetCount() != 0 )
{
pListItem = GetItem( &pListItem->m_ItemList, dwID );
if( pListItem )
return pListItem;
}
}
return NULL;
}
void CCheckList::GetWidestItem(CPtrList* pItemList)
{
// loop for all items
LISTITEM* pListItem = NULL;
POSITION Pos = pItemList->GetHeadPosition();
while(Pos)
{
// Get this item
pListItem = (LISTITEM*)pItemList->GetNext(Pos);
// Is this item wider
if( pListItem->nTotalWidth > m_nWidestItem )
m_nWidestItem = pListItem->nTotalWidth;
// Are there any open items in this?
if( pListItem->m_ItemList.GetCount() != 0 )
GetWidestItem( &pListItem->m_ItemList );
}
}
int CCheckList::SetItemText(int nItem, CString strNew)
{
CDC* pdc = GetWindowDC();
CSize cText = pdc->GetTextExtent(strNew);
ReleaseDC(pdc);
m_nCurrentRow = 0;
LISTITEM* pItem = GetItem( &m_RootItemList, nItem );
if( pItem == NULL )
return LB_ERR;
pItem->csText=strNew;
pItem->nTextLength=cText.cx;
Invalidate();
return 0;
}
void CCheckControl::SetValue()
{
m_nPrevSelRow=LB_ERR;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -