📄 xlistctrl.cpp
字号:
int index = 0;
if (str.IsEmpty())
{
// str is empty, try to get from first listbox string
if (m_pListBox->GetCount() > 0)
m_pListBox->GetText(0, str);
SetItemText(nItem, nSubItem, str);
}
else
{
// set listbox selection from subitem text
index = m_pListBox->FindStringExact(-1, str);
if (index == LB_ERR)
index = 0;
}
m_pListBox->SetCurSel(index);
m_pListBox->GetText(index, m_strInitialComboString);
m_pListBox->SetActive(11);
}
}
}
}
pDC->SelectObject(pOldPen);
}
#endif
///////////////////////////////////////////////////////////////////////////////
// DrawCheckbox
void CXListCtrl::DrawCheckbox(int nItem,
int nSubItem,
CDC *pDC,
COLORREF crText,
COLORREF crBkgnd,
CRect& rect,
XLISTCTRLDATA *pXLCD)
{
ASSERT(pDC);
ASSERT(pXLCD);
GetDrawColors(nItem, nSubItem, crText, crBkgnd);
pDC->FillSolidRect(&rect, crBkgnd);
CRect chkboxrect;
chkboxrect = rect;
chkboxrect.bottom -= 1;
chkboxrect.left += 9; // line up checkbox with header checkbox
chkboxrect.right = chkboxrect.left + chkboxrect.Height(); // width = height
CString str;
str = GetItemText(nItem, nSubItem);
if (str.IsEmpty())
{
// center the checkbox
chkboxrect.left = rect.left + rect.Width()/2 - chkboxrect.Height()/2 - 1;
chkboxrect.right = chkboxrect.left + chkboxrect.Height();
}
// fill rect around checkbox with white
pDC->FillSolidRect(&chkboxrect, m_crWindow);
chkboxrect.left += 1;
// draw border
pDC->DrawEdge(&chkboxrect, EDGE_SUNKEN, BF_RECT);
if (pXLCD[nSubItem].nCheckedState == 1)
{
CPen *pOldPen = NULL;
CPen graypen(PS_SOLID, 1, m_crGrayText);
CPen blackpen(PS_SOLID, 1, RGB(0,0,0));
if (pXLCD[0].bEnabled)
pOldPen = pDC->SelectObject(&blackpen);
else
pOldPen = pDC->SelectObject(&graypen);
// draw the checkmark
int x = chkboxrect.left + 9;
ASSERT(x < chkboxrect.right);
int y = chkboxrect.top + 3;
int i;
for (i = 0; i < 4; i++)
{
pDC->MoveTo(x, y);
pDC->LineTo(x, y+3);
x--;
y++;
}
for (i = 0; i < 3; i++)
{
pDC->MoveTo(x, y);
pDC->LineTo(x, y+3);
x--;
y--;
}
if (pOldPen)
pDC->SelectObject(pOldPen);
}
if (!str.IsEmpty())
{
pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(crText);
pDC->SetBkColor(crBkgnd);
CRect textrect;
textrect = rect;
textrect.left = chkboxrect.right + 4;
pDC->DrawText(str, &textrect, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
}
}
///////////////////////////////////////////////////////////////////////////////
// GetDrawColors
void CXListCtrl::GetDrawColors(int nItem,
int nSubItem,
COLORREF& colorText,
COLORREF& colorBkgnd)
{
DWORD dwStyle = GetStyle();
DWORD dwExStyle = GetExtendedStyle();
COLORREF crText = colorText;
COLORREF crBkgnd = colorBkgnd;
if (GetItemState(nItem, LVIS_SELECTED))
{
if (dwExStyle & LVS_EX_FULLROWSELECT)
{
// selected? if so, draw highlight background
crText = m_crHighLightText;
crBkgnd = m_crHighLight;
// has focus? if not, draw gray background
if (m_hWnd != ::GetFocus())
{
if (dwStyle & LVS_SHOWSELALWAYS)
{
crText = m_crWindowText;
crBkgnd = m_crBtnFace;
}
else
{
crText = colorText;
crBkgnd = colorBkgnd;
}
}
}
else // not full row select
{
if (nSubItem == 0)
{
// selected? if so, draw highlight background
crText = m_crHighLightText;
crBkgnd = m_crHighLight;
// has focus? if not, draw gray background
if (m_hWnd != ::GetFocus())
{
if (dwStyle & LVS_SHOWSELALWAYS)
{
crText = m_crWindowText;
crBkgnd = m_crBtnFace;
}
else
{
crText = colorText;
crBkgnd = colorBkgnd;
}
}
}
}
}
colorText = crText;
colorBkgnd = crBkgnd;
}
///////////////////////////////////////////////////////////////////////////////
// DrawImage
int CXListCtrl::DrawImage(int nItem,
int nSubItem,
CDC* pDC,
COLORREF crText,
COLORREF crBkgnd,
CRect rect,
XLISTCTRLDATA *pXLCD)
{
GetDrawColors(nItem, nSubItem, crText, crBkgnd);
pDC->FillSolidRect(&rect, crBkgnd);
int nWidth = 0;
rect.left += m_HeaderCtrl.GetSpacing();
CImageList* pImageList = GetImageList(LVSIL_SMALL);
if (pImageList)
{
SIZE sizeImage;
sizeImage.cx = sizeImage.cy = 0;
IMAGEINFO info;
int nImage = -1;
if (pXLCD)
nImage = pXLCD[nSubItem].nImage;
if (nImage == -1)
return 0;
if (pImageList->GetImageInfo(nImage, &info))
{
sizeImage.cx = info.rcImage.right - info.rcImage.left;
sizeImage.cy = info.rcImage.bottom - info.rcImage.top;
}
if (nImage >= 0)
{
if (rect.Width() > 0)
{
POINT point;
point.y = rect.CenterPoint().y - (sizeImage.cy >> 1);
point.x = rect.left;
SIZE size;
size.cx = rect.Width() < sizeImage.cx ? rect.Width() : sizeImage.cx;
size.cy = rect.Height() < sizeImage.cy ? rect.Height() : sizeImage.cy;
// save image list background color
COLORREF rgb = pImageList->GetBkColor();
// set image list background color
pImageList->SetBkColor(crBkgnd);
pImageList->DrawIndirect(pDC, nImage, point, size, CPoint(0, 0));
pImageList->SetBkColor(rgb);
nWidth = sizeImage.cx + m_HeaderCtrl.GetSpacing();
}
}
}
return nWidth;
}
///////////////////////////////////////////////////////////////////////////////
// DrawText
void CXListCtrl::DrawText(int nItem,
int nSubItem,
CDC *pDC,
COLORREF crText,
COLORREF crBkgnd,
CRect& rect,
XLISTCTRLDATA *pXLCD)
{
ASSERT(pDC);
ASSERT(pXLCD);
GetDrawColors(nItem, nSubItem, crText, crBkgnd);
pDC->FillSolidRect(&rect, crBkgnd);
CString str;
str = GetItemText(nItem, nSubItem);
if (!str.IsEmpty())
{
// get text justification
HDITEM hditem;
hditem.mask = HDI_FORMAT;
m_HeaderCtrl.GetItem(nSubItem, &hditem);
int nFmt = hditem.fmt & HDF_JUSTIFYMASK;
UINT nFormat = DT_VCENTER | DT_SINGLELINE;
if (nFmt == HDF_CENTER)
nFormat |= DT_CENTER;
else if (nFmt == HDF_LEFT)
nFormat |= DT_LEFT;
else
nFormat |= DT_RIGHT;
CFont *pOldFont = NULL;
CFont boldfont;
// check if bold specified for subitem
if (pXLCD && pXLCD[nSubItem].bBold)
{
CFont *font = pDC->GetCurrentFont();
if (font)
{
LOGFONT lf;
font->GetLogFont(&lf);
lf.lfWeight = FW_BOLD;
boldfont.CreateFontIndirect(&lf);
pOldFont = pDC->SelectObject(&boldfont);
}
}
pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(crText);
pDC->SetBkColor(crBkgnd);
pDC->DrawText(str, &rect, nFormat);
if (pOldFont)
pDC->SelectObject(pOldFont);
}
}
///////////////////////////////////////////////////////////////////////////////
// GetSubItemRect
BOOL CXListCtrl::GetSubItemRect(int nItem,
int nSubItem,
int nArea,
CRect& rect)
{
ASSERT(nItem >= 0);
ASSERT(nItem < GetItemCount());
if ((nItem < 0) || nItem >= GetItemCount())
return FALSE;
ASSERT(nSubItem >= 0);
ASSERT(nSubItem < GetColumns());
if ((nSubItem < 0) || nSubItem >= GetColumns())
return FALSE;
BOOL bRC = CListCtrl::GetSubItemRect(nItem, nSubItem, nArea, rect);
// if nSubItem == 0, the rect returned by CListCtrl::GetSubItemRect
// is the entire row, so use left edge of second subitem
if (nSubItem == 0)
{
if (GetColumns() > 1)
{
CRect rect1;
bRC = GetSubItemRect(nItem, 1, LVIR_BOUNDS, rect1);
rect.right = rect1.left;
}
}
return bRC;
}
///////////////////////////////////////////////////////////////////////////////
// OnLButtonDown
void CXListCtrl::OnLButtonDown(UINT nFlags, CPoint point)
{
TRACE(_T("in CXListCtrl::OnLButtonDown\n"));
int nItem = -1;
CRect rect;
int i;
for (i = 0; i < GetItemCount(); i++)
{
if (CListCtrl::GetItemRect(i, &rect, LVIR_BOUNDS))
{
if (rect.PtInRect(point))
{
nItem = i;
break;
}
}
}
if (nItem == -1)
{
#ifndef DO_NOT_INCLUDE_XCOMBOLIST
if (m_pListBox)
OnComboEscape(0, 0);
#endif
}
else
{
XLISTCTRLDATA *pXLCD = (XLISTCTRLDATA *) CListCtrl::GetItemData(nItem);
if (!pXLCD)
{
return;
}
if (!pXLCD[0].bEnabled)
return;
CRect rect;
int nSubItem = -1;
// check if a subitem checkbox was clicked
for (i = 0; i < GetColumns(); i++)
{
GetSubItemRect(nItem, i, LVIR_BOUNDS, rect);
if (rect.PtInRect(point))
{
nSubItem = i;
break;
}
}
if (nSubItem == -1)
{
// -1 = no checkbox for this subitem
#ifndef DO_NOT_INCLUDE_XCOMBOLIST
if (m_pListBox)
{
OnComboEscape(0, 0);
}
#endif
}
else
{
if (pXLCD[nSubItem].nCheckedState >= 0)
{
int nChecked = pXLCD[nSubItem].nCheckedState;
nChecked = (nChecked == 0) ? 1 : 0;
pXLCD[nSubItem].nCheckedState = nChecked;
UpdateSubItem(nItem, nSubItem);
CWnd *pWnd = GetParent();
if (!pWnd)
pWnd = GetOwner();
if (pWnd && ::IsWindow(pWnd->m_hWnd))
pWnd->SendMessage(WM_XLISTCTRL_CHECKBOX_CLICKED,
nItem, nSubItem);
// now update checkbox in header
// -1 = no checkbox in column header
if (GetHeaderCheckedState(nSubItem) != XHEADERCTRL_NO_IMAGE)
{
int nCheckedCount = CountCheckedItems(nSubItem);
if (nCheckedCount == GetItemCount())
SetHeaderCheckedState(nSubItem, XHEADERCTRL_CHECKED_IMAGE);
else
SetHeaderCheckedState(nSubItem, XHEADERCTRL_UNCHECKED_IMAGE);
}
}
#ifndef DO_NOT_INCLUDE_XCOMBOLIST
else if (pXLCD[nSubItem].bCombo)
{
if (m_pListBox)
{
m_pListBox->DestroyWindow();
delete m_pListBox;
m_pListBox = NULL;
}
rect.left = rect.right - rect.Height();
if (point.x >= rect.left && point.y <= rect.right)
{
pXLCD[nSubItem].bComboIsClicked = TRUE;
m_bComboIsClicked = TRUE;
m_nComboItem = nItem;
m_nComboSubItem = nSubItem;
UpdateSubItem(nItem, nSubItem);
SetTimer(1, 100, NULL);
}
}
else if (m_pListBox)
{
OnComboEscape(0, 0);
}
#endif
}
}
CListCtrl::OnLButtonDown(nFlags, point);
}
///////////////////////////////////////////////////////////////////////////////
// OnPaint
void CXListCtrl::OnPaint()
{
Default();
if (GetItemCount() <= 0)
{
CDC* pDC = GetDC();
int nSavedDC = pDC->SaveDC();
CRect rc;
GetWindowRect(&rc);
ScreenToClient(&rc);
CHeaderCtrl* pHC = GetHeaderCtrl();
if (pHC != NULL)
{
CRect rcH;
pHC->GetItemRect(0, &rcH);
rc.top += rcH.bottom;
}
rc.top += 10;
CString strText;
strText = _T("There are no items to show in this view.");
COLORREF crText = m_crWindowText;
COLORREF crBkgnd = m_crWindow;
CBrush brush(crBkgnd);
pDC->FillRect(rc, &brush);
pDC->SetTextColor(crText);
pDC->SetBkColor(crBkgnd);
pDC->SelectStockObject(ANSI_VAR_FONT);
pDC->DrawText(strText, -1, rc, DT_CENTER | DT_WORDBREAK | DT_NOPREFIX | DT_NOCLIP);
pDC->RestoreDC(nSavedDC);
ReleaseDC(pDC);
}
}
///////////////////////////////////////////////////////////////////////////////
// InsertItem
int CXListCtrl::InsertItem(const LVITEM* pItem)
{
ASSERT(pItem->iItem >= 0);
if (pItem->iItem < 0)
return -1;
int index = CListCtrl::InsertItem(pItem);
if (index < 0)
return index;
XLISTCTRLDATA *pXLCD = new XLISTCTRLDATA [GetColumns()];
ASSERT(pXLCD);
if (!pXLCD)
return -1;
pXLCD[0].crText = m_crWindowText;
pXLCD[0].crBackground = m_crWindow;
pXLCD[0].nImage = pItem->iImage;
CListCtrl::SetItemData(index, (DWORD) pXLCD);
return index;
}
///////////////////////////////////////////////////////////////////////////////
// InsertItem
int CXListCtrl::InsertItem(int nItem, LPCTSTR lpszItem)
{
ASSERT(nItem >= 0);
if (nItem < 0)
return -1;
return InsertItem(nItem,
lpszItem,
m_crWindowText,
m_crWindow);
}
///////////////////////////////////////////////////////////////////////////////
// InsertItem
int CXListCtrl::InsertItem(int nItem,
LPCTSTR lpszItem,
COLORREF crText,
COLORREF crBackground)
{
ASSERT(nItem >= 0);
if (nItem < 0)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -