解决ListCtrl控件第一列文字不能居中显示的问题/修改网格线
把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控件第一列文字不能居中显示的问题/修改网格线的更多相关文章
- delphi ,1)控件根据窗口大小,一直居中显示 2)显示最大化最小化按钮控件
一.控件根据窗口大小,一直居中显示 1)onResize:当窗体尺寸改变时发生 例子:如何使控件随窗口的放大和缩小动态改变自己的大小,使控件“保存.返回”在窗口变大变小中随着变. 在Panel调用 p ...
- VC/MFC ListCtrl 控件功能使用汇总(转)
以下未经说明,listctrl默认view 风格为report 相关类及处理函数 MFC:CListCtrl类 SDK:以 “ListView_”开头的一些宏.如 ListView_InsertCol ...
- LISTCTRL控件方法
以下未经说明,listctrl默认view风格为report --------------------------------------------------------------------- ...
- CSharpGL(26)在opengl中实现控件布局/渲染文字
CSharpGL(26)在opengl中实现控件布局/渲染文字 效果图 如图所示,可以将文字.坐标轴固定在窗口的一角. 下载 CSharpGL已在GitHub开源,欢迎对OpenGL有兴趣的同学加入( ...
- ListCtrl控件的使用
list contrl控件的使用 .建立基于对话框的应用程序,布置界面,设置属性. 注意添加的是listctrl控件,不是listbox控件,在控件工具箱的倒数第五行list control控件. 属 ...
- easyUI的datagrid控件日期列不能正确显示Json格式数据的解决方案
EasyUI是一套比较轻巧易用的Jquery控件,在使用过程中遇到一个问题,它的列表控件——datagrid, 在显示日期列的时候,由于后台返回给页面的数据是Json格式的,其中的日期字段,在后台是正 ...
- ListCtrl控件
一 CListCtrl类型 LVS_EDITLABELS LVS_OWNERDRAWFIXED LVS_REPORT LVS_SHOWSELALWAYS LVS_SINGLESEL LVS_SMALL ...
- GridView控件隐藏列
GridView隐藏列visible="false" 后你就无法取得这列的值了 下面是迄今为止最简洁的解决方法了. protected void GVList_RowDataBou ...
- WinForm给控件加入hint文字
本文代码主要是参考别人的,仅为个人记录,方面后续使用~ 效果图: 主要代码在一个Win32Utility类中,代码如下: public static class Win32Utility { [Dll ...
随机推荐
- Protocol Buffers的基础说明和使用
我们開始须要使用protobuf的原因是,在处理OJ的contest模块的时候,碰到一个问题就是生成contestRank的时候.须要存储非常多信息. 假设我们採用model存储的话,那么一方面兴许假 ...
- SCOPE_IDENTITY()和 SELECT @@IDENTITY 的用法
这俩个,是在插入数据的时候使用,返回刚刚插入数据行的ID 大家慎用@@IDENTITY,而尽量采用 SCOPE_IDENTITY() 函数替换之. SCOPE_IDENTITY() 也是得到最后一条 ...
- noip 2018 day1 T2 货币系统 完全背包
Code: #include<cstdio> #include<string> #include<cstring> #include<algorithm> ...
- Json与JsonPath
JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,因为它良好的可读性与易于机器进行解析和生成等特性,在当前的数据整理和收集中得到了广泛的应用. JSON和XM ...
- window下搭建Vue.Js开发环境
一.安装node.js.https://nodejs.org/en/download/ 最新包会自动安装npm 二.安装完node之后,npm包含的很多依赖包是部署在国外的,在天朝,大家都知道下载速度 ...
- 【Linux下权限控制之chmod与chown命令】
chmod 用于配置文件/目录权限 命名格式:chmod [选项] 文件/目录名 . 权限类别: r 读取 可用数字4表示 w 写入 可用数字2表示 x 执行 可用数字1表示 . 归属类别: u 属主 ...
- PHP定时执行任务
ignore_user_abort();//关掉浏览器,PHP脚本也可以继续执行. set_time_limit(0);// 通过set_time_limit(0)可以让程序无限制的执行下去 $int ...
- Dcloud+mui 压缩上传图片到服务器
chooseImgFromAlbums选择图片 chooseImgFromPictures 拍照 changeToLocalUrl 转换成可用的路径 uploadpic.compressImg 压缩图 ...
- 学习——HTML5
HTML5多用于手机页面制作,因为PC版浏览器大多不兼容,可以通过下面网站查看HTML5浏览器兼容情况: http://www.caniuse.com/#index 一.语义化标签 1.<hea ...
- crm操作发票实体
using System; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Query; using Microsoft.Cr ...