Solved

Changing rowsheight in CListCtrl

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
cat dog challenge 18 122
How to determine file rename from EventLog ? 2 36
sumDigits challenge 9 96
scoreUp challenge 14 48
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now