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

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

LVCOLUMN lvc;
lvc.mask = LVCF_FMT;
GetColumn(, &lvc);
lvc.fmt &=~ LVCFMT_JUSTIFYMASK;
lvc.fmt |= LVCFMT_CENTER;
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的列。

下面是实例代码:

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=; i<sizeof(str)/sizeof(str[]); i++)
{
m_list.InsertColumn(i, str[i], LVCFMT_CENTER, );
m_list.InsertItem(i, _T(""));
m_list.SetItemText(i, , _T("AAA"));
}
m_list.DeleteColumn();

修改网格线颜色:

 const MSG *msg = GetCurrentMessage();
DefWindowProc(msg->message, msg->wParam, msg->lParam); //这两句不能省,否则程序会因消息循环出现异常 // Draw the lines only for LVS_REPORT mode
if ((GetStyle() & LVS_TYPEMASK) == LVS_REPORT)
{
// Get the number of columns
CClientDC dc(this);
CHeaderCtrl* pHeader = (CHeaderCtrl*)GetDlgItem();
int nColumnCount = pHeader->GetItemCount(); // The bottom of the header corresponds to the top of the line
RECT rect;
pHeader->GetClientRect(&rect);
int top = rect.bottom; // Now get the client rect so we know the line length and
// when to stop
GetClientRect(&rect); // The border of the column is offset by the horz scroll
int borderx = - GetScrollPos(SB_HORZ); CPen listSepPen(PS_SOLID, , RGB(, , )); //定制你的分割线的颜色
CPen *pOldPen = dc.SelectObject(&listSepPen); for (int i = ; i < nColumnCount; i++)
{
// Get the next border
borderx += GetColumnWidth(i); // if next border is outside client area, break out
if (borderx >= rect.right) break; // Draw the line.
dc.MoveTo(borderx, top);
dc.LineTo(borderx, rect.bottom);
} // Draw the horizontal grid lines // First get the height
if (!GetItemRect(, &rect, LVIR_BOUNDS))
return; int height = rect.bottom - rect.top; GetClientRect(&rect);
int width = rect.right; for (int i = ; i <= GetCountPerPage(); i++)
{
dc.MoveTo(, top + height*i);
dc.LineTo(width, top + height*i);
} 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. 《AndroidStudio每日一贴》11. 重构之提炼为常量

    提炼为常量是从暂时变量高速提炼出静态常量,这也是我们经常使用的重构手段. 很多其它有用技巧请查看<AndroidStudio有用指南> 操作步骤: ➤ 菜单条: Refactor -> ...

  2. 罗列几个Android插件化开发框架

    携程插件化框架 ACDD插件化框架 360插件化框架 Android-Plugin-Framework DL APK动态加载框架 部分框架对比 DynamicLoadApk 迁移成本很重:需要使用『t ...

  3. CORS with Spring MVC--转

    原文地址:http://dontpanic.42.nl/2015/04/cors-with-spring-mvc.html CORS with Spring MVC   In this blog po ...

  4. 是时候抛弃web.xml了?

    你是否再为配置文件web.xml容易出错而烦恼?是否为web.xml文件存放位置而不知所措?是否为web.xml为什么要这样配?怎么才能更好的配置web.xml而烦恼?那么一种新的方式出现了: spr ...

  5. leetcode 10 Regular Expression Matching(简单正则表达式匹配)

    最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...

  6. 简单的字符串压缩--C代码

    #include <stdio.h> #include <string.h> bool compress(char *str) { char *p=str,c; ; if(!s ...

  7. noip 2018 day1 T1 铺设道路 贪心

    Code: #include<cstdio> using namespace std; int main() { int last=0,ans=0; int n;scanf("% ...

  8. 翻译《虚幻引擎4艺术大师 - 蓝图 III 》 中文版

    本文章由cartzhang编写,转载请注明出处. 所有权利保留.  文章链接:http://blog.csdn.net/cartzhang/article/details/49800063  作者:c ...

  9. 【Henu ACM Round#17 C】Kitahara Haruki's Gift

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 判断sum/2这个价值能不能得到就可以了. 则就是一个01背包模型了. 判断某个价值能否得到. f[j]表示价值j能否得到. f[0 ...

  10. poj 1087 A Plug for UNIX(字符串编号建图)

    A Plug for UNIX Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14862   Accepted: 5026 ...