Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong CListCtrl items drawing

I have my CListCtrlEx derived from CListCtrl. This list have style LVS_REPORT, LVS_OWNERDRAWFIXED and LVS_EX_GRIDLINES. I have added possibility to change font for this list. This works fine, but there is one bad thing - if I change font and before that I have not been scrolling list, then all list items redraws right, but if I have done scrolling before font changing, then list items redraws a little bit upper or lower than list grid horizontal lines, i. e. items text becomes overlapped by grid lines.

Here is how I changing list font:

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);
}

And here is how it looks like:

UPD: three people have clicked button UP and nobody knows what it can be? :(

UPD 1: here's the class code http://pastebin.com/UdXYEpF7 .h http://pastebin.com/2HYe5AEd .cpp

like image 889
brightside90 Avatar asked Jan 30 '26 09:01

brightside90


1 Answers

I tried your code, it looks like ListView is exchanging messages with scroller, the header is also being resized, it's not really worth investigating. It's fine if you just set position to zero, you can save the old position and put it back.

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);
}
like image 169
Barmak Shemirani Avatar answered Feb 02 '26 01:02

Barmak Shemirani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!