Solved

Changing rowsheight in CListCtrl

Posted on 2002-07-18
6
492 Views
Last Modified: 2013-11-20
Hi All,
I tried to change font size of CListCtrl object. Unfortunately the row height of the control was not updated, and the text got clipped. I tried to run my app on win 98 as well as win NT.

Any suggestions?

p.s.
Please, Real answares. Not links to codeguru or codeproject

--EC--
0
Comment
Question by:elcapitan
  • 3
6 Comments
 
LVL 30

Expert Comment

by:SteveGTR
ID: 7162062
Sorry for the "unreal" answer:

http://www.codeproject.com/listctrl/changerowheight.asp

You're going to have to a little reading :)


0
 
LVL 3

Author Comment

by:elcapitan
ID: 7162088
This article is dealing with owner draw controls. I'm not interesting in drawing the control my self. Is there another way to do that? maybe custom draw?
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 7162131
Use the techniques decribed for WM_MEASUREITEM. I briefly tried out the technique using the dialog's message mapping and it worked fine:

BEGIN_MESSAGE_MAP(CGrantBrowse, CAuxiliaryDialog)
  // ...
  ON_WM_MEASUREITEM()
  // ...
END_MESSAGE_MAP()

void CMyDlg::OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
  if (nIDCtl == IDC_MYLISTCTRL)
    lpMeasureItemStruct->itemHeight = 20;
  else
    CDialog::OnMeasureItem(nIDCtl, lpMeasureItemStruct);
}

There appears to be a way to redirect the measure item message using reflection so that the subclassed control is more usable. Rather than relying on the container window having to resize the control's height.

Good Luck,
Steve

0
 
LVL 30

Accepted Solution

by:
SteveGTR earned 300 total points
ID: 7163119
Of course the example I plugged the above into was for a ownerdrawn list control. Here is a draw handler from the class I tested with. It is based on CSortListCtrl from MSJ "Wicked code" column from 12/97 Vol 11, No 12:
 
// To allow for row highlighting you must check the "Owner drawn fixed" attribute
// in the CListCtrl's properties.
void CSortListCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
      static BOOL bClearing=FALSE;

      // Get the display context for the control.
      CDC* pCDC = CDC::FromHandle(lpDrawItemStruct->hDC);

      // Use mask if there is one.
      UINT uiFlags(ILD_TRANSPARENT);

      // Check to see if this item is selected.
      if (lpDrawItemStruct->itemState & ODS_SELECTED)
            {
            pCDC->SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
            pCDC->SetBkColor(::GetSysColor(COLOR_HIGHLIGHT));
            uiFlags |= ILD_BLEND50;
            }
      else
            {
            pCDC->SetTextColor(::GetSysColor(COLOR_WINDOWTEXT));
            pCDC->SetBkColor(::GetSysColor(COLOR_WINDOW));
            }

      // Get the image list and draw.
      CImageList* plistImages = GetImageList(LVSIL_SMALL);

      // In order to get the focus rectangle to disappear on highlighted rows,
      // I had to force an invalidate via this cheesey methold. Not doing so
      // would cause the only the 1st column to be repainted with the border!
      if (lpDrawItemStruct->itemState & ODS_FOCUS)
            {
            if (!bClearing)
                  {
                  bClearing = TRUE;
          CRect rTextClip(lpDrawItemStruct->rcItem);
                  rTextClip.left += (plistImages ? 16 : 0);

                  InvalidateRect(rTextClip);
                  return;
                  }
            else
                  bClearing = FALSE;
            }

      // Get the item image to be displayed.
      LV_ITEM lvi;
      memset(&lvi, 0, sizeof(&lvi));
      lvi.mask = LVIF_IMAGE | LVIF_STATE;
      lvi.iItem = lpDrawItemStruct->itemID;
      GetItem(&lvi);

      // If valid, draw.
      if (plistImages)
            {
            CPoint ptAt(lpDrawItemStruct->rcItem.left, lpDrawItemStruct->rcItem.top);
                  plistImages->Draw(pCDC, lvi.iImage, ptAt, uiFlags);
            }

      // Now walk through columns and draw text.
      char szText[256];     // Increase this if you have longer text.
      LV_COLUMN lvc;
      memset(&lvc, 0, sizeof(lvc));
      lvc.mask = LVCF_WIDTH;

      for (int nColumn = 0; GetColumn(nColumn, &lvc); nColumn++)
            {
            CRect rTextClip;

            // First time.
            if (0 == nColumn)
                  {
                  rTextClip.left   = lpDrawItemStruct->rcItem.left + (plistImages ? 16 : 0);
                  rTextClip.top    = lpDrawItemStruct->rcItem.top;
                  rTextClip.right  = lpDrawItemStruct->rcItem.left + lvc.cx;
                  rTextClip.bottom = lpDrawItemStruct->rcItem.bottom;
                  }
            else
                  {
                  // Just "move" the rect to the right.
                  rTextClip.left  = rTextClip.right;
                  rTextClip.right = rTextClip.left + lvc.cx;
                  }

            // Get the text.
            lvi.iItem = lpDrawItemStruct->itemID;
            lvi.mask = LVIF_TEXT;
            lvi.iSubItem = nColumn;
            lvi.pszText = szText;
            lvi.cchTextMax = sizeof(szText);
            GetItem(&lvi);

            // Add ellipses if necessary.
            int nTextCount(lstrlen(lvi.pszText));

            CSize sizText(pCDC->GetOutputTextExtent(lvi.pszText, nTextCount));

            // Make the clipping rectangle a little smaller for a gap
            // effect between columns.
            rTextClip.right -= 4;

            if (sizText.cx >= rTextClip.Width())
                  {
                  // Make a shorter string, including "..." that fits.
                  CSize sizEllipse = pCDC->GetOutputTextExtent("...", 3);

                  // Now start dropping characters at the tail until width
                  // is correct.
                  while (sizText.cx+sizEllipse.cx > (rTextClip.Width()) && nTextCount > 1)
                        {
                        lvi.pszText[--nTextCount] = 0;
                        sizText = pCDC->GetOutputTextExtent(lvi.pszText, nTextCount);
                        }

                  // Ok, append "...".
                  lvi.pszText[nTextCount] = '.';
                  lvi.pszText[nTextCount + 1] = '.';
                  lvi.pszText[nTextCount + 2] = '.';
                  lvi.pszText[nTextCount + 3] = 0;
                  }

            // Restore full rect.
            rTextClip.right += 4;

            // Print the text.
            pCDC->ExtTextOut(rTextClip.left + 2, rTextClip.top + 1,
                  ETO_CLIPPED | ETO_OPAQUE, &rTextClip, lvi.pszText, lstrlen(lvi.pszText), NULL);
        }

      // If focused draw focus rect (I added the focus check)
      if (lpDrawItemStruct->itemState & ODS_FOCUS)
            {
    CRect rTextClip(lpDrawItemStruct->rcItem);
            rTextClip.left += (plistImages ? 16 : 0);

            CWnd *pFocusWnd = GetFocus();
            
            if (pFocusWnd && pFocusWnd->m_hWnd == m_hWnd)
                  pCDC->DrawFocusRect(rTextClip);
            else
                  {
                  CBrush bdrBrush(::GetSysColor(COLOR_HIGHLIGHT));
                  pCDC->FrameRect(rTextClip, &bdrBrush);
                  }
            }

      // Reset colors if necessary.
      if (lpDrawItemStruct->itemState & ODS_SELECTED)
            {
            pCDC->SetTextColor(::GetSysColor(COLOR_WINDOWTEXT));
            pCDC->SetBkColor(::GetSysColor(COLOR_WINDOW));
            }
}
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Windows Drag & Drop Location 2 112
Generic progress indicator 6 119
sum13 challenge 24 135
wordappend challenge 8 225
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question