ListView的最后一列自动调整创建滚动条 [英] ListView Final Column Autosize creates scrollbar
问题描述
我在执行从ListView中派生的自定义控制。
I am implementing a custom control which derives from ListView.
我想对于最后一列,以填补剩余空间(一个相当普遍的任务),我已经通过重写onResize受到了方法这个:
I would like for the final column to fill the remaining space (Quite a common task), I have gone about this via overriding the OnResize method:
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
if (Columns.Count == 0)
return;
Columns[Columns.Count - 1].Width = -2; // -2 = Fill remaining space
}
或通过另一种方法:
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
if (!_autoFillLastColumn)
return;
if (Columns.Count == 0)
return;
int TotalWidth = 0;
int i = 0;
for (; i < Columns.Count - 1; i++)
{
TotalWidth += Columns[i].Width;
}
Columns[i].Width = this.DisplayRectangle.Width - TotalWidth;
}
编辑:
直到我停靠在ListView到父容器,并通过该控制调整这工作得很好。控制尺寸的缩小每一秒钟时间(即,拖动边框一个像素),我得到的底部有一个滚动条,可以不是在所有(甚至没有一个像素)移动。
This works fine until I dock the ListView into a parent container and resize via that control. Every second time the control size shrinks (IE, drag the the border one pixel), I get a scroll bar on the bottom which can't move at all (not even one pixel).
其结果是,当我拖累我留下ListView中一个闪烁的滚动条的父级的大小和50%的机会,它将在那里当拖拽停止。
The result of which is when I drag the size of the parent I am left with a flickering scroll bar in the ListView, and a 50% chance it will be there when the dragging stops.
推荐答案
ObjectListView (约.NET的WinForms ListView的一个开放源代码包装)允许这种扩大,最后一列来一补可用空间。它实际上更难获得正确的不是它第一次出现 - 不要相信任何人谁告诉你,否则:)
ObjectListView (an open source wrapper around .NET WinForms ListView) allows this "expand-last-column-to-fill-available-space". It's actually more difficult to get right than it first appears -- do not believe anyone who tells you otherwise :)
如果您使用ObjectListView,你得到的解决这一问题 - 和其他几个人 - 是免费的。但是,如果你想自己做所有的工作,你需要:
If you use ObjectListView, you get the solution to that problem -- and several others -- for free. But if you want to do all the work yourself, you'll need to:
- 使用ClientSize代替DisplayRectangle(如@zxpro已经表示)
- 使用布局事件,而不是Resize事件。
- 监听ColumnResized事件,做同样的工作
- 这样做在设计模式中自动调整大小 - 它变得非常混乱
只有当窗口缩小出现最棘手的问题 - 水平滚动条闪烁烦人,有时甚至萎缩完成后仍然存在。这似乎是到底是什么在您的情况发生。
The trickiest problem only appears when the window shrinks -- the horizontal scroll bar flickers annoyingly and sometimes remains after the shrinking is finished. Which appears to be exactly what is happening in your case.
解决的办法是截获WM_WINDOWPOSCHANGING消息并调整其大小的ListView什么新的大小将是。
The solution is to intercept the WM_WINDOWPOSCHANGING message and resize the ListView to what the new size is going to be.
protected override void WndProc(ref Message m) {
switch (m.Msg) {
case 0x46: // WM_WINDOWPOSCHANGING
this.HandleWindowPosChanging(ref m);
base.WndProc(ref m);
break;
default:
base.WndProc(ref m);
break;
}
}
protected virtual void HandleWindowPosChanging(ref Message m) {
const int SWP_NOSIZE = 1;
NativeMethods.WINDOWPOS pos = (NativeMethods.WINDOWPOS)m.GetLParam(typeof(NativeMethods.WINDOWPOS));
if ((pos.flags & SWP_NOSIZE) == 0) {
if (pos.cx < this.Bounds.Width) // only when shrinking
// pos.cx is the window width, not the client area width, so we have to subtract the border widths
this.ResizeFreeSpaceFillingColumns(pos.cx - (this.Bounds.Width - this.ClientSize.Width));
}
}
这篇关于ListView的最后一列自动调整创建滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!