解決ListCtrl控件第一列文字不能居中顯示的問題


原文地址:http://blog.pfan.cn/yuqiexing/50744.html (略作整理和補充)

把CListCtrl設置為Report風格,但是插入第一列的時候(InsertColumn)的時候會發現文字不能居中。即使使用了LVCFMT_CENTER,其他列都可以正常居中,但第一列仍然靠左顯示。

解決方案:

(1)巧妙解決:插入第一列時寬度設置為0,棄之不用。但是這樣有問題,凡是與第一列相關的一些設置將發揮不了作用,例如checkbox和icon。

(2)插入第一列后,改變它的參數:(我使用了這種方法)

LVCOLUMN lvc;
lvc.mask = LVCF_FMT;
GetColumn(0, &lvc);
lvc.fmt &=~ LVCFMT_JUSTIFYMASK; 
lvc.fmt |= LVCFMT_CENTER;
SetColumn(0, &lvc);

(3)插入第一列后,將其刪除,第二列此時會充當第一列。

補充(csdn網友:VisualEleven):

MS自己搞的,第一列式不能設置格式的,MSDN里有說明:
If a column is added to a list-view control with index 0 (the leftmost column) and with LVCFMT_RIGHT or LVCFMT_CENTER specified, the text is not right-aligned or centered. The text in the index 0 column is left-aligned. Therefore if you keep inserting columns with index 0, the text in all columns are left-aligned. If you want the first column to be right-aligned or centered you can make a dummy column, then insert one or more columns with index 1 or higher and specify the alignment you require. Finally delete the dummy column.

大致意思是這樣的:索引為0的列(最左邊的列)如果設置了LVCFMT_RIGHT或LVCFMT_CENTER屬性,上面的文字並不會右對齊或居中對齊。索引為0 的列是左對齊。如果你要想第一列右對齊或者居中對齊,你可以這樣做,先保留索引為0的列,其他的列均指定右對齊或居中對齊屬性,最后刪除索引為0的列。

下面是實例代碼:

m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
CString str[] ={_T(""), _T("AAA"), _T("BBB"), _T("CCC"), _T("DDDD"), _T("EEE")};
for(int i=0; i<sizeof(str)/sizeof(str[0]); i++)
{
    m_list.InsertColumn(i, str[i], LVCFMT_CENTER, 100);
    m_list.InsertItem(i, _T(""));
    m_list.SetItemText(i, 0, _T("AAA"));
}
m_list.DeleteColumn(0);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM