15,976
社区成员
发帖
与我相关
我的任务
分享
void CSilverTree::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMTVCUSTOMDRAW pNMTVCD = reinterpret_cast<LPNMTVCUSTOMDRAW>(pNMHDR);
if (!this->IsWindowEnabled())
{
*pResult = CDRF_DODEFAULT;
return;
}
switch (pNMTVCD->nmcd.dwDrawStage)
{
case CDDS_PREPAINT:
{
*pResult = CDRF_NOTIFYITEMDRAW;
return;
}
case CDDS_ITEMPREPAINT:
{
*pResult = CDRF_NOTIFYPOSTPAINT;
return;
}
case CDDS_ITEMPOSTPAINT:
{
HTREEITEM hItem = (HTREEITEM)pNMTVCD->nmcd.dwItemSpec;
CDC *pDC = CDC::FromHandle(pNMTVCD->nmcd.hdc);
CRect rcText;
GetItemRect(hItem, &rcText, TRUE);
CString strText = GetItemText(hItem);
if (pNMTVCD->nmcd.uItemState & (CDIS_SELECTED|CDIS_FOCUS))
{
pDC->FillSolidRect(&rcText, RGB(179,179,179));
pDC->SetTextColor(GetSysColor(COLOR_HIGHLIGHTTEXT));
pDC->TextOut(rcText.left+2, rcText.top+2, strText);
}
else
{
pDC->FillSolidRect(&rcText, GetSysColor(COLOR_WINDOW));
pDC->SetTextColor(GetSysColor(COLOR_WINDOWTEXT));
pDC->TextOut(rcText.left+2, rcText.top+2, strText);
}
*pResult = 0;
return;
}
default:
{
*pResult = CDRF_DODEFAULT;
return;
}
}
}可以仿效树的如此做法吗?
void CMyTree::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMTVCUSTOMDRAW pNMCD = reinterpret_cast<LPNMTVCUSTOMDRAW>(pNMHDR);
switch (pNMCD->nmcd.dwDrawStage)
{
case CDDS_PREPAINT:
*pResult = CDRF_NOTIFYITEMDRAW;
return;
case CDDS_ITEMPREPAINT:
if (this->IsWindowEnabled())
{
if ((pNMCD->nmcd.uItemState&(CDIS_FOCUS))==0 &&
(pNMCD->nmcd.uItemState&(CDIS_SELECTED))==CDIS_SELECTED) //selected
{
pNMCD->clrTextBk = RGB(255, 255, 255);
pNMCD->clrText = RGB(0, 0, 0);
}
*pResult = CDRF_NOTIFYPOSTPAINT;
return;
}
else
{
*pResult = CDRF_DODEFAULT;
return;
}
case CDDS_ITEMPOSTPAINT:
if (this->IsWindowEnabled())
{
HTREEITEM hItem = (HTREEITEM)pNMCD->nmcd.dwItemSpec;
CDC *pDC = CDC::FromHandle(pNMCD->nmcd.hdc);
CRect rcText;
GetItemRect(hItem, &rcText, true);
CString strText = GetItemText(hItem);
if (pNMCD->nmcd.uItemState & (CDIS_SELECTED|CDIS_FOCUS))
{
CPen penGray(PS_SOLID, 1, RGB(179,179,179));
CBrush br;
br.CreateSolidBrush(RGB(179,179,179));
CBrush *pBrush = &br;
CBrush *pOldBrush = pDC->SelectObject(pBrush);
CPen *pOldPen = pDC->SelectObject(&penGray);
pDC->Rectangle(&rcText);
pDC->SelectObject(pOldBrush);
pDC->SelectObject(pOldPen);
pDC->FillSolidRect(&rcText, RGB(179,179,179));
pDC->SetTextColor(GetSysColor(COLOR_HIGHLIGHTTEXT));
pDC->TextOut(rcText.left+2, rcText.top+2, strText);
*pResult = CDRF_SKIPDEFAULT;
}
else
{
pDC->FillSolidRect(&rcText, RGB(255,255,255));
pDC->SetTextColor(GetSysColor(COLOR_WINDOWTEXT));
pDC->TextOut(rcText.left+2, rcText.top+2, strText);
*pResult = CDRF_DODEFAULT;
}
return;
}
else
{
*pResult = CDRF_DODEFAULT;
return;
}
}
}以上实现 树 所有功能了void CMyList::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: Add your code to draw the specified item
TCHAR lpBuffer[256];
LV_COLUMN lvc, lvcprev;
::ZeroMemory(&lvc, sizeof(lvc));
::ZeroMemory(&lvcprev, sizeof(lvcprev));
lvc.mask = LVCF_WIDTH | LVCF_FMT;
lvcprev.mask = LVCF_WIDTH | LVCF_FMT;
for (int nCol=0; GetColumn(nCol,&lvc); nCol++)
{
if (nCol > 0)
{
// Get Previous Column Width in order to move the next display item
GetColumn(nCol-1, &lvcprev);
lpDrawItemStruct->rcItem.left += lvcprev.cx;
lpDrawItemStruct->rcItem.right += lpDrawItemStruct->rcItem.left;
}
// Get the text
LV_ITEM lvi;
::ZeroMemory(&lvi, sizeof(lvi));
lvi.mask = LVIF_TEXT | LVIF_PARAM;
lvi.iItem = lpDrawItemStruct->itemID;
lvi.iSubItem = nCol;
lvi.pszText = lpBuffer;
lvi.cchTextMax = sizeof(lpBuffer);
VERIFY(GetItem(&lvi));
CDC* pDC;
pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
if (lpDrawItemStruct->itemState & ODS_SELECTED)
{
pDC->FillSolidRect(&lpDrawItemStruct->rcItem, RGB(125, 125, 125));
pDC->SetTextColor(GetSysColor(COLOR_HIGHLIGHTTEXT));
}
else
{
pDC->FillSolidRect(&lpDrawItemStruct->rcItem, GetSysColor(COLOR_WINDOW));
pDC->SetTextColor(GetSysColor(COLOR_WINDOWTEXT));
}
pDC->SelectObject(GetStockObject(DEFAULT_GUI_FONT));
UINT uFormat = DT_LEFT;
::DrawText(lpDrawItemStruct->hDC, lpBuffer, strlen(lpBuffer), &lpDrawItemStruct->rcItem, uFormat);
pDC->SelectStockObject(SYSTEM_FONT);
}
}
void CMyList::PreSubclassWindow()
{
// TODO: Add your specialized code here and/or call the base class
ModifyStyle(0, LVS_OWNERDRAWFIXED); //for skin
CListCtrl::PreSubclassWindow();
}也许有参考价值void CMyEdit::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
int begin, end;
GetSel(begin, end);
HDC hdc = dc.GetSafeHdc();
if ((0!=begin) && (0!=end))
{
// 有字被选中
SetBkMode(hdc, TRANSPARENT);
SetTextColor(hdc, RGB(250, 0, 0));
SetBkColor(hdc, RGB(0, 0, 0));
}
else
{
}
// Do not call CEdit::OnPaint() for painting messages
}