解决CListCtrl中第一列元素的只有左对齐问题的两种方法?

来源:互联网 发布:淘宝怎么判断质量问题 编辑:程序博客网 时间:2024/05/08 22:03

     问题:在CListCtrl中第一列元素只能左对齐,下面是msdn中的说明!

CListCtrl::InsertColumn

Inserts a new column in a list view control.

int InsertColumn(
   int nCol,
   const LVCOLUMN* pColumn
);
int InsertColumn(
   int nCol,
   LPCTSTR lpszColumnHeading,
   int nFormat = LVCFMT_LEFT,
   int nWidth = -1,
   int nSubItem = -1
);
Parameters
nCol
The index of the new column.
pColumn
Address of an LVCOLUMN structure that contains the attributes of the new column.
lpszColumnHeading
Address of a string containing the column's heading.
nFormat
Integer specifying the alignment of the column. It can be one of these values: LVCFMT_LEFT, LVCFMT_RIGHT, or LVCFMT_CENTER.
nWidth
Width of the column, in pixels. If this parameter is -1, the column width is not set.
nSubItem
Index of the subitem associated with the column. If this parameter is -1, no subitem is associated with the column.
Return Value
The index of the new column if successful or -1 otherwise.

Remarks
The leftmost column in a list view control must be left-aligned.

The LVCOLUMN structure contains the attributes of a column in report view. It is also used to receive information about a column. This structure is described in the Platform SDK.


解决方法:
   方法1:
       1、在第一列插入一个空列
       2、设置第一列的宽度为0
       3、然后插入位置从1开始


    例子代码
     m_pListCtrl.InsertColumn (0, "",LVCFMT_CENTER);
      m_pListCtrl.InsertColumn (1, "Element",LVCFMT_LEFT);
      m_pListCtrl.InsertColumn (2, "Note",LVCFMT_LEFT);   

      m_pListCtrl.SetColumnWidth (0, 0);     
      m_pListCtrl.SetColumnWidth (1, ClientRect.Width() / 5);   
      m_pListCtrl.SetColumnWidth (2, LVSCW_AUTOSIZE_USEHEADER); 

   方法2:
       1、首先多添加一列
       2、当所有列添加完成以后,删除第一列即可
   例子代码:
       m_List.InsertColumn (0, "dummy", LVCFMT_CENTER); //多添加一列
       m_List.InsertColumn (1, "Element", LVCFMT_CENTER);
       m_List.InsertColumn (2, "Note", LVCFMT_LEFT);

       m_List.DeleteColumn(0);
 
       m_List.SetColumnWidth (0, ClientRect.Width() / 5);     
       m_List.SetColumnWidth (1, LVSCW_AUTOSIZE_USEHEADER);

原创粉丝点击