ListView-将垂直网格线与标题分隔线对齐-使最后一列填充空间 [英] ListView - Align vertical grid lines with Headers dividers - Make last Column fill the space
问题描述
我正在尝试用C#编写Windows Forms MusicPlayer应用程序.
I am trying to write a Windows Forms MusicPlayer application in C#.
应用程序应显示一个列表,并具有一些播放/停止按钮.
The application should show a list and have some play / stop buttons.
我刚刚开始半个小时,但是我的设计几乎完成了.现在,我要修复3件事.一个错误和两个漂亮的东西:
I just started half an hour ago, but my design is almost finished. Now I got 3 things to fix. A bug and 2 good looking things:
-
在图片上,您可以看到我发现的错误.您可能会说这没什么,但是引人注目.我该如何解决?
on the picture you can see the bug I've found. You might say that's nothing, but its a eye catcher. How can I fix this?
如何在不使内容居中的情况下将列标题居中对齐?
how can I align center the headline of a column, without centering the content?
如何使最后一列填充listView的其余部分?
how can I make the last column filling out the rest of the listView?
推荐答案
-
您可以设置除第一列标题以外的所有标题的
TextAlign
;它始终保持对齐.要进行更改,您需要所有者绘制它.You can set the
TextAlign
of all but the 1st Column's Header; it is always left aligned. To change that you need to owner draw it.没有自动填充选项,因此您需要编写一个
setColumnwidth
函数,该函数遍历除最后一列以外的所有列,并对它们的Widths
求和.然后从ListView的Clientsize.Width
中减去总和,并设置最后一列的宽度.There is no automatic filling option so you need to write a
setColumnwidth
function, that loops over all but the last columns and sums theirWidths
; then it subtract the sum from the ListView'sClientsize.Width
and set the last column's Width.网格线中的显示错误对我来说是新的;到目前为止,我还不知道如何解决它;也许所有者图纸也可以帮上忙..?
The display bug in the gridlines is new to me; so far I don't know how to fix it; maybe owner-drawing will help there as well..?
更新:
这是一些代码:
void setLastColumnTofill(ListView lv) { int sum = 0; int count = lv.Columns.Count; for (int i = 0; i < count - 1; i++) sum += lv.Columns[i].Width; lv.Columns[count - 1].Width = lv.ClientSize.Width - sum; }
设置
OwnerDraw = true
后,您可以编写三个(全部都是必需的)Draw事件的代码:After setting
OwnerDraw = true
you could code the three (all are needed!) Draw event :private void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e) { e.Graphics.FillRectangle(SystemBrushes.Menu, e.Bounds); e.Graphics.DrawRectangle(SystemPens.GradientInactiveCaption, new Rectangle(e.Bounds.X , 0, e.Bounds.Width , e.Bounds.Height) ); string text = listView1.Columns[e.ColumnIndex].Text; TextFormatFlags cFlag = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter; TextRenderer.DrawText(e.Graphics, text, listView1.Font, e.Bounds, Color.Black, cFlag); } private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e) { e.DrawDefault = true; } private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e) { e.DrawDefault = true; }
您可能想稍微玩一些颜色或宽度.
You may want to play a little with the colors or the widths..
如果您的ImageList包含用于显示排序顺序(或其他内容)的图像,则也可以添加它以绘制它们:
If you have an ImageList containing images for displaying the sort order (or other things) you can add this to draw them as well:
ColumnHeader colH = listView1.Columns[e.ColumnIndex]; int ii = colH.ImageIndex; if (ii >= 0 && ii < imageList1.Images.Count) e.Graphics.DrawImage(imageList1.Images[ii], e.Bounds.Width + e.Bounds.X - imageList1.ImageSize.Width, 0);
这篇关于ListView-将垂直网格线与标题分隔线对齐-使最后一列填充空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!