?? colorlistctrl.cpp
字號:
{
RedrawItems( Index, Index );
return TRUE;
}
return FALSE;
}
//***************************************************************
BOOL CColorListCtrl::SetItemTextColor(COLORREF color, int Index, int iSub)
{
CMyLParam *p=GetMyLParam(Index);
if (!p) return FALSE;
if ( p->SetTextColor(color, iSub) )
{
RedrawItems( Index, Index );
return TRUE;
}
return FALSE;
}
//***************************************************************
COLORREF CColorListCtrl::GetItemBackgndColor(int Index, int iSub)
{
CMyLParam *p=GetMyLParam(Index);
if (!p) return 0;
return p->GetBackColor(iSub);
}
//***************************************************************
COLORREF CColorListCtrl::GetItemTextColor(int Index, int iSub)
{
CMyLParam *p=GetMyLParam(Index);
if (!p) return 0;
return p->GetTextColor(iSub);
}
//***************************************************************
void CColorListCtrl::OnPaint()
{
// First let the control do its default drawing.
const MSG *msg = GetCurrentMessage();
DefWindowProc( msg->message, msg->wParam, msg->lParam );
if (!m_fullColumnLines) return;
// Draw the lines only for LVS_REPORT mode
if( (GetStyle() & LVS_TYPEMASK) == LVS_REPORT )
{
// Get the number of columns
CClientDC dc(this );
CHeaderCtrl* pHeader = (CHeaderCtrl*)GetDlgItem(0);
int nColumnCount = pHeader->GetItemCount();
// The bottom of the header corresponds to the top of the line
RECT rect;
pHeader->GetClientRect( &rect );
int top = rect.bottom;
// Now get the client rect so we know the line length and
// when to stop
GetClientRect( &rect );
// The border of the column is offset by the horz scroll
int borderx = 0 - GetScrollPos( SB_HORZ );
CPen *pOldPen;
CPen pen;
CGdiObject *pOldBrush;
pen.CreatePen( PS_DOT, 0, ::GetSysColor(COLOR_HIGHLIGHTTEXT) );
pOldPen =dc.SelectObject(&pen);
pOldBrush=dc.SelectStockObject(NULL_BRUSH);
for( int i = 0; i < nColumnCount; i++ )
{
// Get the next border
borderx += GetColumnWidth( i );
// if next border is outside client area, break out
if( borderx >= rect.right ) break;
// Draw the line.
dc.MoveTo( borderx-1, top);
dc.LineTo( borderx-1, rect.bottom );
}
dc.SelectObject(pOldPen);
dc.SelectObject(pOldBrush);
}
// Do not call CListCtrl::OnPaint() for painting messages
}
//***************************************************************
void CColorListCtrl::RepaintSelectedItems()
{
// Invalidate focused item so it can repaint
int nItem = GetNextItem(-1, LVNI_FOCUSED);
if(nItem != -1)
{
InvalidateFullItem(nItem, FALSE);
}
// Invalidate selected items depending on LVS_SHOWSELALWAYS
if((GetStyle() & LVS_SHOWSELALWAYS))
{
for(nItem = GetNextItem(-1, LVNI_SELECTED);
nItem != -1; nItem = GetNextItem(nItem, LVNI_SELECTED))
{
InvalidateFullItem(nItem, FALSE);
}
}
UpdateWindow();
}
//***************************************************************
BOOL CColorListCtrl::SetSel(int pos, BOOL bSelect)
{
// LVS_ES_FULLROWSELECT only works with newer version of Comctl32
// This style and some of the other new extended styles
// only work with version 4.72 (or higer) of COMCTL32.DLL
UINT nState=0;
if (bSelect) nState=LVIS_SELECTED|LVS_EX_FULLROWSELECT; else nState=0;
if (SetItemState(pos, nState, LVIS_SELECTED|LVS_EX_FULLROWSELECT))
{
if((GetStyle() & LVS_SHOWSELALWAYS))
{
InvalidateFullItem(pos, TRUE);
}
return TRUE;
}
return FALSE;
}
//***************************************************************
void CColorListCtrl::OnKillFocus(CWnd* pNewWnd)
{
SendMessage(WM_PAINT);
CListCtrl::OnKillFocus(pNewWnd);
// check if we are losing focus
if(pNewWnd != NULL && pNewWnd->GetParent() == this)
return;
// repaint items that should change appearance
if((GetStyle() & LVS_TYPEMASK) == LVS_REPORT)
RepaintSelectedItems();
}
//***************************************************************
void CColorListCtrl::OnSetFocus(CWnd* pOldWnd)
{
CListCtrl::OnSetFocus(pOldWnd);
// check if we are getting focus
if(pOldWnd!=NULL && pOldWnd->GetParent()==this)
return;
// repaint items that should change appearance
if((GetStyle() & LVS_TYPEMASK)==LVS_REPORT)
RepaintSelectedItems();
}
//***************************************************************
void CColorListCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
if (!lpDrawItemStruct) return;
IsCreated=TRUE;
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
CMyLParam* pMyLParam = GetMyLParam(lpDrawItemStruct->itemID);
CRect rcItem;
if (!pMyLParam) return;
int x=0;
typedef enum {R_NOTHINGS=0, R_FOCUS, R_SELECT, R_NORMAL, R_SEL_NOFOCUS} redrawmode;
redrawmode redraw=R_NORMAL;
// Get item image and state info
LV_ITEM lvi;
lvi.mask = LVIF_IMAGE | LVIF_STATE;
lvi.iItem = lpDrawItemStruct->itemID;
lvi.iSubItem = 0;
lvi.stateMask = 0xFFFF; // get all state flags
GetItem(&lvi);
BOOL bHighlight = ( (lvi.state & LVIS_DROPHILITED)
||
(
(lvi.state & LVIS_SELECTED)
&&
( ( GetFocus() == this ) || ( GetStyle() & LVS_SHOWSELALWAYS ) )
)
);
BOOL bLostFocus=(GetFocus()!=this);
if ( (lpDrawItemStruct->itemAction&ODA_FOCUS)==ODA_FOCUS)
{ //the control gains or loses input focus
if (bLostFocus) redraw=R_SEL_NOFOCUS;
else
if ((lvi.state&LVIS_FOCUSED)==LVIS_FOCUSED) redraw=R_FOCUS;
else
{
if (bHighlight) redraw=R_SELECT;
else redraw=R_NORMAL;
}
}
else
if ((lpDrawItemStruct->itemAction&ODA_SELECT)==ODA_SELECT)
{ //the selection status has changed
if (bHighlight)
{
if (bLostFocus) redraw=R_SEL_NOFOCUS;
else redraw=R_SELECT;
}
else redraw=R_NORMAL;
}
else
{ //redraw the item
if (bLostFocus)
{
if (bHighlight) redraw=R_SEL_NOFOCUS;
else
redraw=R_NORMAL;
}
else
{
if ((lvi.state&LVIS_FOCUSED)==LVIS_FOCUSED) redraw=R_FOCUS;
else
{
if (bHighlight) redraw=R_SELECT;
else redraw=R_NORMAL;
}
}
}
CPen *pOldPen;
CPen pen;
CGdiObject *pOldBrush;
switch(redraw)
{
case R_FOCUS:
rcItem=GetFullCellRect(lpDrawItemStruct->itemID, TRUE);
pDC->FillSolidRect(rcItem, ::GetSysColor(COLOR_ACTIVECAPTION));
pDC->SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
if (m_BigFocusRect)
{
pen.CreatePen( PS_DOT, 0, ::GetSysColor(COLOR_HIGHLIGHTTEXT) );
pOldPen =pDC->SelectObject(&pen);
pOldBrush=pDC->SelectStockObject(NULL_BRUSH);
pDC->Rectangle(rcItem);
pDC->SelectObject(pOldPen);
pDC->SelectObject(pOldBrush);
}
else pDC->DrawFocusRect(rcItem);
for (x=0; x<m_columnSize; x++)
{
rcItem = GetCellRect(lpDrawItemStruct->itemID, x, FALSE);
pDC->DrawText(CListCtrl::GetItemText(lpDrawItemStruct->itemID,x), rcItem, DT_SINGLELINE | DT_LEFT | DT_VCENTER );
}
break;
case R_SELECT:
rcItem=GetFullCellRect(lpDrawItemStruct->itemID, TRUE);
pDC->FillSolidRect(rcItem, ::GetSysColor(COLOR_ACTIVECAPTION));
pDC->SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
for (x=0; x<m_columnSize; x++)
{
rcItem = GetCellRect(lpDrawItemStruct->itemID, x, FALSE);
pDC->DrawText(CListCtrl::GetItemText(lpDrawItemStruct->itemID,x), rcItem, DT_SINGLELINE | DT_LEFT | DT_VCENTER);
}
break;
case R_SEL_NOFOCUS:
rcItem=GetFullCellRect(lpDrawItemStruct->itemID, TRUE);
pDC->FillSolidRect(rcItem, ::GetSysColor(COLOR_INACTIVECAPTION));
pDC->SetTextColor(pMyLParam->GetTextColor(5));
pen.CreatePen( PS_DOT, 0, ::GetSysColor(COLOR_INACTIVECAPTION) );
pOldPen =pDC->SelectObject(&pen);
pOldBrush=pDC->SelectStockObject(NULL_BRUSH);
if (!m_withSingleRect)
pDC->Rectangle(rcItem);
for (x=0; x<m_columnSize; x++)
{
rcItem = GetCellRect(lpDrawItemStruct->itemID, x, FALSE);
pDC->DrawText(CListCtrl::GetItemText(lpDrawItemStruct->itemID,x), rcItem, DT_SINGLELINE | DT_LEFT | DT_VCENTER);
}
pDC->SelectObject(pOldPen);
pDC->SelectObject(pOldBrush);
break;
case R_NORMAL:
for (x=0; x<m_columnSize; x++)
{
rcItem = GetCellRect(lpDrawItemStruct->itemID, x, TRUE);
pDC->FillSolidRect(rcItem, pMyLParam->GetBackColor(x));
pDC->SetTextColor(pMyLParam->GetTextColor(x));
if (m_withSingleRect)
{
pOldBrush=pDC->SelectStockObject(NULL_BRUSH);
pDC->Rectangle(rcItem);
pDC->SelectObject(pOldBrush);
}
rcItem = GetCellRect(lpDrawItemStruct->itemID, x, FALSE);
pDC->DrawText(CListCtrl::GetItemText(lpDrawItemStruct->itemID,x), rcItem, DT_SINGLELINE | DT_LEFT | DT_VCENTER);
}
break;
}
return;
}
void CColorListCtrl::OnSize(UINT nType, int cx, int cy)
{
CListCtrl::OnSize(nType, cx, cy);
if (IsCreated)
{
RedrawWindow();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -