📄 sel_row.shtml.htm
字号:
if( m_nHighlight == 0 ) // Highlight only first column
{
pDC->SetTextColor(::GetSysColor(COLOR_WINDOWTEXT));
pDC->SetBkColor(::GetSysColor(COLOR_WINDOW));
}
rcBounds.right = rcHighlight.right > rcBounds.right ? rcHighlight.right :
rcBounds.right;
rgn.CreateRectRgnIndirect(&rcBounds);
pDC->SelectClipRgn(&rgn);
for(int nColumn = 1; GetColumn(nColumn, &lvc); nColumn++)
{
rcCol.left = rcCol.right;
rcCol.right += lvc.cx;
// Draw the background if needed
if( m_nHighlight == HIGHLIGHT_NORMAL )
pDC->FillRect(rcCol, &CBrush(::GetSysColor(COLOR_WINDOW)));
sLabel = GetItemText(nItem, nColumn);
if (sLabel.GetLength() == 0)
continue;
// Get the text justification
UINT nJustify = DT_LEFT;
switch(lvc.fmt & LVCFMT_JUSTIFYMASK)
{
case LVCFMT_RIGHT:
nJustify = DT_RIGHT;
break;
case LVCFMT_CENTER:
nJustify = DT_CENTER;
break;
default:
break;
}
rcLabel = rcCol;
rcLabel.left += offset;
rcLabel.right -= offset;
pDC->DrawText(sLabel, -1, rcLabel, nJustify | DT_SINGLELINE |
DT_NOPREFIX | DT_VCENTER | DT_END_ELLIPSIS);
}
// Draw focus rectangle if item has focus
if (lvi.state & LVIS_FOCUSED && (GetFocus() == this))
pDC->DrawFocusRect(rcHighlight);
// Restore dc
pDC->RestoreDC( nSavedDC );
}
</FONT></TT></PRE>
<h4>Step 4: Define RepaintSelectedItems() helper function</h4>
This helper function is used to add or remove the focus rectangle around an item. It also adds or removes highlighting from a selected item depending on whether the control has the LVS_SHOWSELALWAYS style. Actually this function just invalidates the rectangle and DrawItem() takes care of the rest.
<PRE><TT><FONT COLOR="#990000">void CMyListCtrl::RepaintSelectedItems()
{
CRect rcBounds, rcLabel;
// Invalidate focused item so it can repaint
int nItem = GetNextItem(-1, LVNI_FOCUSED);
if(nItem != -1)
{
GetItemRect(nItem, rcBounds, LVIR_BOUNDS);
GetItemRect(nItem, rcLabel, LVIR_LABEL);
rcBounds.left = rcLabel.left;
InvalidateRect(rcBounds, 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))
{
GetItemRect(nItem, rcBounds, LVIR_BOUNDS);
GetItemRect(nItem, rcLabel, LVIR_LABEL);
rcBounds.left = rcLabel.left;
InvalidateRect(rcBounds, FALSE);
}
}
UpdateWindow();
}
</FONT></TT></PRE>
<h4>Step 5: Add code to OnPaint() to invalidate entire row</h4>
When the list view control repaints an item, it repaints only the area occupied by defined columns. That is if the last column does not extend to the end of the client area, then the space to the right of the last column is not repainted. If we are highlighting the full row then this area also needs to be invalidated, so that the code in DrawItem() can add or remove the highlighting from this area.
<PRE><TT><FONT COLOR="#990000">void CMyListCtrl::OnPaint()
{
// in full row select mode, we need to extend the clipping region
// so we can paint a selection all the way to the right
if (m_nHighlight == HIGHLIGHT_ROW &&
(GetStyle() & LVS_TYPEMASK) == LVS_REPORT )
{
CRect rcBounds;
GetItemRect(0, rcBounds, LVIR_BOUNDS);
CRect rcClient;
GetClientRect(&rcClient);
if(rcBounds.right < rcClient.right)
{
CPaintDC dc(this);
CRect rcClip;
dc.GetClipBox(rcClip);
rcClip.left = min(rcBounds.right-1, rcClip.left);
rcClip.right = rcClient.right;
InvalidateRect(rcClip, FALSE);
}
}
CListCtrl::OnPaint();
}
</FONT></TT></PRE>
<h4>Step 6: Add handlers for WM_KILLFOCUS and WM_SETFOCUS</h4>
This is another step to mimic the default behaviour of the list view control. When the control loses focus, the focus rectangle around the selected (focus) item has to be removed. When the control gets back focus, then the focus rectangle has to be redrawn. Both these handlers call the RepaintSelectedItems() helper function.
<PRE><TT><FONT COLOR="#990000">void CMyListCtrl::OnKillFocus(CWnd* pNewWnd)
{
CListCtrl::OnKillFocus(pNewWnd);
// check if we are losing focus to label edit box
if(pNewWnd != NULL && pNewWnd->GetParent() == this)
return;
// repaint items that should change appearance
if((GetStyle() & LVS_TYPEMASK) == LVS_REPORT)
RepaintSelectedItems();
}
void CMyListCtrl::OnSetFocus(CWnd* pOldWnd)
{
CListCtrl::OnSetFocus(pOldWnd);
// check if we are getting focus from label edit box
if(pOldWnd!=NULL && pOldWnd->GetParent()==this)
return;
// repaint items that should change appearance
if((GetStyle() & LVS_TYPEMASK)==LVS_REPORT)
RepaintSelectedItems();
}
</FONT></TT></PRE>
<h4>Step 7: Add helper function to change selection mode</h4>
In this function we update the member variable and invalidate the control so that the items are redrawn with the proper highlighting. SetHighlightType() returns the previous highlight value.
<PRE><TT><FONT COLOR="#990000">int CMyListCtrl::SetHighlightType(EHighlight hilite)
{
int oldhilite = m_nHighlight;
if( hilite <= HIGHLIGHT_ROW )
{
m_nHighlight = hilite;
Invalidate();
}
return oldhilite;
}
</FONT></TT></PRE>
<BR>
<P>
<HR>
<TABLE BORDER=0 WIDTH="100%" >
<TR>
<TD WIDTH="33%"><FONT SIZE=-1><A HREF="../index.htm" tppabs="http://www.codeguru.com/">Goto HomePage</A></FONT></TD>
<TD WIDTH="33%">
<CENTER><FONT SIZE=-2>© 1997 Zafir Anjum</FONT> </CENTER>
</TD>
<TD WIDTH="34%">
<DIV ALIGN=right><FONT SIZE=-1>Contact me: <A HREF="mailto:zafir@home.com">zafir@home.com</A> </FONT></DIV>
</TD>
</TR>
</TABLE>
<CENTER><IMG SRC="../cgi/Count.cgi-ft=2&dd=E-df=lv_sel_row.cnt" tppabs="http://www.codeguru.com/cgi/Count.cgi?ft=2&dd=E%7cdf=lv_sel_row.cnt" ALIGN="BOTTOM" BORDER="0"></CENTER>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -