Wrong drawing of CListCtrl elements
I have CListCtrlEx obtained from CListCtrl. This list has the style LVS_REPORT, LVS_OWNERDRAWFIXED and LVS_EX_GRIDLINES. I added the ability to change the font for this list. This works great, but there is one bad thing - if I change the font and I was not a scroll list before then all the list items are redrawn correctly, but if I scrolled before the font change then the list items redraw slightly above or below the horizontal lines list grids, i.e. That is, the text of the items is overlaid with grid lines.
This is how I change the font of the list:
LRESULT CListCtrlEx::OnSetFont(WPARAM wParam, LPARAM)
{
LRESULT res = Default();
CRect rc;
GetWindowRect(&rc);
WINDOWPOS wp;
wp.hwnd = m_hWnd;
wp.cx = rc.Width();
wp.cy = rc.Height();
wp.flags = SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOZORDER;
SendMessage(WM_WINDOWPOSCHANGED, 0, (LPARAM)&wp);
return res;
}
void CListCtrlEx::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
HDC hDC = ::GetDC(NULL);
CFont* pFont = GetFont();
HFONT hFontOld = (HFONT)SelectObject(hDC, pFont->GetSafeHandle());
CRect rect;
DrawText(hDC, _T(" "), 1, rect, DT_SINGLELINE | DT_CALCRECT);
lpMeasureItemStruct->itemHeight = rect.bottom - rect.top;
SelectObject(hDC, hFontOld);
::ReleaseDC(NULL, hDC);
}
UPD: three people pressed the UP button and no one knows what it could be ?:(
UPD 1: Here's the class code http://pastebin.com/UdXYEpF7 .h http://pastebin.com/2HYe5AEd .cpp
source to share
I tried your code, it looks like the ListView is messaging with a scroller, the header is also resized and it is not worth looking into. This is fine, if you just set the position to zero, you can keep the old position and return it.
void CListCtrlEx::SetupFont(int nSize, const CString& strName)
{
int saveIndex = GetTopIndex();
EnsureVisible(0, 0);
if (m_pFont.get()) m_pFont.get()->DeleteObject();
VERIFY(m_pFont.get()->CreatePointFont(nSize, strName));
SetFont(m_pFont.get());
//This scrolls to bottom, it ensures saveIndex will end up on top
//once the next EnsureVisible is called
if (GetItemCount())
EnsureVisible(GetItemCount() - 1, 1);
EnsureVisible(saveIndex, 1);
}
source to share