Calculate the required size of text to fit in column
-
: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
-
: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
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
-
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
:-O Tanke you very mush Mr .Michael Dunn for your help. And I'll try to do your advice. Dr Abudawood
-
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
:) 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
-
:) 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
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
orLVSCW_AUTOSIZE_USEHEADER
value withLVM_SETCOLUMNWIDTH
. --Mike-- Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber Latest art~!@#2rDFA#@(#*%$Rfa39f3fqwf--= NO CARRIER