Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Calculate the required size of text to fit in column

Calculate the required size of text to fit in column

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    drabudawood
    wrote on last edited by
    #1

    :confused:How can I calculate the required size of text which can be placed in one column of CListCtrl object and fits in. when I tried to use the next code, some text did not appear all, I get three dots at the end of the appeared text on the column. Can you help me please what is wrong with code ? void CMyDialogDlg::AdjustColSize(int ColNo,CString s) { CClientDC dc(this); CSize sz; CFont *f = m_ListControl.GetFont(); dc.SelectObject(f); sz = dc.GetTextExtent(s,_tcslen(s)); sz.cx += GetSystemMetrics(SM_CXBORDER); if( width[ColNo] < sz.cx ) { m_ListControl.SetColumnWidth(ColNo,sz.cx); width[ColNo]=sz.cx; } } Dr Abudawood

    M 1 Reply Last reply
    0
    • D drabudawood

      :confused:How can I calculate the required size of text which can be placed in one column of CListCtrl object and fits in. when I tried to use the next code, some text did not appear all, I get three dots at the end of the appeared text on the column. Can you help me please what is wrong with code ? void CMyDialogDlg::AdjustColSize(int ColNo,CString s) { CClientDC dc(this); CSize sz; CFont *f = m_ListControl.GetFont(); dc.SelectObject(f); sz = dc.GetTextExtent(s,_tcslen(s)); sz.cx += GetSystemMetrics(SM_CXBORDER); if( width[ColNo] < sz.cx ) { m_ListControl.SetColumnWidth(ColNo,sz.cx); width[ColNo]=sz.cx; } } Dr Abudawood

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #2

      Use LVM_GETSTRINGWIDTH to get the width in pixels of a string. The docs say:

      If you use the returned string width as the column width in the LVM_SETCOLUMNWIDTH message, the string will be truncated. To retrieve the column width that can contain the string without truncating it, you must add padding to the returned string width.

      so you have to fudge the return value by a few pixels (I guess the exact number depends on the font, bigger font means more padding). --Mike-- Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber aef392rDFA#@(#*@%@#$Rfa39f3fqwf--= NO CARRIER

      D 2 Replies Last reply
      0
      • M Michael Dunn

        Use LVM_GETSTRINGWIDTH to get the width in pixels of a string. The docs say:

        If you use the returned string width as the column width in the LVM_SETCOLUMNWIDTH message, the string will be truncated. To retrieve the column width that can contain the string without truncating it, you must add padding to the returned string width.

        so you have to fudge the return value by a few pixels (I guess the exact number depends on the font, bigger font means more padding). --Mike-- Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber aef392rDFA#@(#*@%@#$Rfa39f3fqwf--= NO CARRIER

        D Offline
        D Offline
        drabudawood
        wrote on last edited by
        #3

        :-O Tanke you very mush Mr .Michael Dunn for your help. And I'll try to do your advice. Dr Abudawood

        1 Reply Last reply
        0
        • M Michael Dunn

          Use LVM_GETSTRINGWIDTH to get the width in pixels of a string. The docs say:

          If you use the returned string width as the column width in the LVM_SETCOLUMNWIDTH message, the string will be truncated. To retrieve the column width that can contain the string without truncating it, you must add padding to the returned string width.

          so you have to fudge the return value by a few pixels (I guess the exact number depends on the font, bigger font means more padding). --Mike-- Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber aef392rDFA#@(#*@%@#$Rfa39f3fqwf--= NO CARRIER

          D Offline
          D Offline
          drabudawood
          wrote on last edited by
          #4

          :) Now I got the answer. after your help it was very easy as next: void CTestScrolDlg::AdjustColSize(int ColNo,CString s) { //Global variable Array width[ColNo] int Gab = 15; int size=m_ListControl.GetStringWidth(s)+Gab; if( width[ColNo] < size ) { m_ListControl.SetColumnWidth(ColNo,size); width[ColNo]=size; } };) Dr Abudawood

          M 1 Reply Last reply
          0
          • D drabudawood

            :) Now I got the answer. after your help it was very easy as next: void CTestScrolDlg::AdjustColSize(int ColNo,CString s) { //Global variable Array width[ColNo] int Gab = 15; int size=m_ListControl.GetStringWidth(s)+Gab; if( width[ColNo] < size ) { m_ListControl.SetColumnWidth(ColNo,size); width[ColNo]=size; } };) Dr Abudawood

            M Offline
            M Offline
            Michael Dunn
            wrote on last edited by
            #5

            I just realized this, but if you want the column to be wide enough to show all strings in the column, use the LVSCW_AUTOSIZE or LVSCW_AUTOSIZE_USEHEADER value with LVM_SETCOLUMNWIDTH. --Mike-- Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber Latest art~!@#2rDFA#@(#*%$Rfa39f3fqwf--= NO CARRIER

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups