Solved

Changing rowsheight in CListCtrl

Posted on 2002-07-18
6
488 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Process filename extension 3 184
method notes when mouse over in eclipse 5 86
viewing source code from eclipse 13 104
"Black Box" Testing of Control System Software 2 49
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

785 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