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

     问题:在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);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值