把CListCtrl设置为Report风格,但是插入第一列的时候(InsertColumn)的时候会发现文字不能居中。即使使用了LVCFMT_CENTER,其他列都可以正常居中,但第一列仍然靠左显示。

插入第一列后,改变它的参数:

  1. LVCOLUMN lvc;
  2. lvc.mask = LVCF_FMT;
  3. GetColumn(, &lvc);
  4. lvc.fmt &=~ LVCFMT_JUSTIFYMASK;
  5. lvc.fmt |= LVCFMT_CENTER;
  6. SetColumn(, &lvc);

第一列式不能设置格式的,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的列。

下面是实例代码:

  1. m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
  2. CString str[] ={_T(""), _T("AAA"), _T("BBB"), _T("CCC"), _T("DDDD"), _T("EEE")};
  3. for(int i=; i<sizeof(str)/sizeof(str[]); i++)
  4. {
  5. m_list.InsertColumn(i, str[i], LVCFMT_CENTER, );
  6. m_list.InsertItem(i, _T(""));
  7. m_list.SetItemText(i, , _T("AAA"));
  8. }
  9. m_list.DeleteColumn();

修改网格线颜色:

  1. const MSG *msg = GetCurrentMessage();
  2. DefWindowProc(msg->message, msg->wParam, msg->lParam); //这两句不能省,否则程序会因消息循环出现异常
  3.  
  4. // Draw the lines only for LVS_REPORT mode
  5. if ((GetStyle() & LVS_TYPEMASK) == LVS_REPORT)
  6. {
  7. // Get the number of columns
  8. CClientDC dc(this);
  9. CHeaderCtrl* pHeader = (CHeaderCtrl*)GetDlgItem();
  10. int nColumnCount = pHeader->GetItemCount();
  11.  
  12. // The bottom of the header corresponds to the top of the line
  13. RECT rect;
  14. pHeader->GetClientRect(&rect);
  15. int top = rect.bottom;
  16.  
  17. // Now get the client rect so we know the line length and
  18. // when to stop
  19. GetClientRect(&rect);
  20.  
  21. // The border of the column is offset by the horz scroll
  22. int borderx = - GetScrollPos(SB_HORZ);
  23.  
  24. CPen listSepPen(PS_SOLID, , RGB(, , )); //定制你的分割线的颜色
  25. CPen *pOldPen = dc.SelectObject(&listSepPen);
  26.  
  27. for (int i = ; i < nColumnCount; i++)
  28. {
  29. // Get the next border
  30. borderx += GetColumnWidth(i);
  31.  
  32. // if next border is outside client area, break out
  33. if (borderx >= rect.right) break;
  34.  
  35. // Draw the line.
  36. dc.MoveTo(borderx, top);
  37. dc.LineTo(borderx, rect.bottom);
  38. }
  39.  
  40. // Draw the horizontal grid lines
  41.  
  42. // First get the height
  43. if (!GetItemRect(, &rect, LVIR_BOUNDS))
  44. return;
  45.  
  46. int height = rect.bottom - rect.top;
  47.  
  48. GetClientRect(&rect);
  49. int width = rect.right;
  50.  
  51. for (int i = ; i <= GetCountPerPage(); i++)
  52. {
  53. dc.MoveTo(, top + height*i);
  54. dc.LineTo(width, top + height*i);
  55. }
  56.  
  57. dc.SelectObject(pOldPen);

解决ListCtrl控件第一列文字不能居中显示的问题/修改网格线的更多相关文章

  1. delphi ,1)控件根据窗口大小,一直居中显示 2)显示最大化最小化按钮控件

    一.控件根据窗口大小,一直居中显示 1)onResize:当窗体尺寸改变时发生 例子:如何使控件随窗口的放大和缩小动态改变自己的大小,使控件“保存.返回”在窗口变大变小中随着变. 在Panel调用 p ...

  2. VC/MFC ListCtrl 控件功能使用汇总(转)

    以下未经说明,listctrl默认view 风格为report 相关类及处理函数 MFC:CListCtrl类 SDK:以 “ListView_”开头的一些宏.如 ListView_InsertCol ...

  3. LISTCTRL控件方法

    以下未经说明,listctrl默认view风格为report --------------------------------------------------------------------- ...

  4. CSharpGL(26)在opengl中实现控件布局/渲染文字

    CSharpGL(26)在opengl中实现控件布局/渲染文字 效果图 如图所示,可以将文字.坐标轴固定在窗口的一角. 下载 CSharpGL已在GitHub开源,欢迎对OpenGL有兴趣的同学加入( ...

  5. ListCtrl控件的使用

    list contrl控件的使用 .建立基于对话框的应用程序,布置界面,设置属性. 注意添加的是listctrl控件,不是listbox控件,在控件工具箱的倒数第五行list control控件. 属 ...

  6. easyUI的datagrid控件日期列不能正确显示Json格式数据的解决方案

    EasyUI是一套比较轻巧易用的Jquery控件,在使用过程中遇到一个问题,它的列表控件——datagrid, 在显示日期列的时候,由于后台返回给页面的数据是Json格式的,其中的日期字段,在后台是正 ...

  7. ListCtrl控件

    一 CListCtrl类型 LVS_EDITLABELS LVS_OWNERDRAWFIXED LVS_REPORT LVS_SHOWSELALWAYS LVS_SINGLESEL LVS_SMALL ...

  8. GridView控件隐藏列

    GridView隐藏列visible="false" 后你就无法取得这列的值了 下面是迄今为止最简洁的解决方法了. protected void GVList_RowDataBou ...

  9. WinForm给控件加入hint文字

    本文代码主要是参考别人的,仅为个人记录,方面后续使用~ 效果图: 主要代码在一个Win32Utility类中,代码如下: public static class Win32Utility { [Dll ...

随机推荐

  1. Protocol Buffers的基础说明和使用

    我们開始须要使用protobuf的原因是,在处理OJ的contest模块的时候,碰到一个问题就是生成contestRank的时候.须要存储非常多信息. 假设我们採用model存储的话,那么一方面兴许假 ...

  2. SCOPE_IDENTITY()和 SELECT @@IDENTITY 的用法

    这俩个,是在插入数据的时候使用,返回刚刚插入数据行的ID 大家慎用@@IDENTITY,而尽量采用 SCOPE_IDENTITY() 函数替换之. SCOPE_IDENTITY()  也是得到最后一条 ...

  3. noip 2018 day1 T2 货币系统 完全背包

    Code: #include<cstdio> #include<string> #include<cstring> #include<algorithm> ...

  4. Json与JsonPath

    JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,因为它良好的可读性与易于机器进行解析和生成等特性,在当前的数据整理和收集中得到了广泛的应用. JSON和XM ...

  5. window下搭建Vue.Js开发环境

    一.安装node.js.https://nodejs.org/en/download/ 最新包会自动安装npm 二.安装完node之后,npm包含的很多依赖包是部署在国外的,在天朝,大家都知道下载速度 ...

  6. 【Linux下权限控制之chmod与chown命令】

    chmod 用于配置文件/目录权限 命名格式:chmod [选项] 文件/目录名 . 权限类别: r 读取 可用数字4表示 w 写入 可用数字2表示 x 执行 可用数字1表示 . 归属类别: u 属主 ...

  7. PHP定时执行任务

    ignore_user_abort();//关掉浏览器,PHP脚本也可以继续执行. set_time_limit(0);// 通过set_time_limit(0)可以让程序无限制的执行下去 $int ...

  8. Dcloud+mui 压缩上传图片到服务器

    chooseImgFromAlbums选择图片 chooseImgFromPictures 拍照 changeToLocalUrl 转换成可用的路径 uploadpic.compressImg 压缩图 ...

  9. 学习——HTML5

    HTML5多用于手机页面制作,因为PC版浏览器大多不兼容,可以通过下面网站查看HTML5浏览器兼容情况: http://www.caniuse.com/#index 一.语义化标签 1.<hea ...

  10. crm操作发票实体

    using System;     using Microsoft.Xrm.Sdk;     using Microsoft.Xrm.Sdk.Query;     using Microsoft.Cr ...