when the (default) scrollbar appears inside the listview the size of the last column is not automatically fixed/decreased ...“默认滚动条”表示垂直滚动。
AutoResize 不是AutoFit。它用于根据标题文本范围或内容长度调整列的大小,而不是管理滚动条并且效果很好。如果它确实自动调整最后一列的大小,当最后一列是一个小的“OK?”时,我们会很生气。类型列在 AutoFitted 时呈现为不可读。
在NativeMethods 类中调用;这些吃滚动条并获取它们是否显示。
Public Const WS_VSCROLL As Integer = &H200000
Public Const WS_HSCROLL As Integer = &H100000
Friend Enum SBOrientation As Integer
SB_HORZ = &H0
SB_VERT = &H1
SB_CTL = &H2
SB_BOTH = &H3
End Enum
<DllImport("user32.dll")> _
Private Shared Function ShowScrollBar(ByVal hWnd As IntPtr,
ByVal wBar As Integer,
<MarshalAs(UnmanagedType.Bool)> ByVal bShow As Boolean
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
<DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function GetWindowLong(ByVal hWnd As IntPtr,
ByVal nIndex As Integer) As Integer
End Function
Friend Shared Function IsHScrollVisible(ByVal ctl As Control) As Boolean
Dim wndStyle As Integer = GetWindowLong(ctl.Handle, GWL_STYLE)
Return ((wndStyle And WS_HSCROLL) <> 0)
End Function
Friend Shared Function IsVScrollVisible(ByVal ctl As Control) As Boolean
Dim wndStyle As Integer = GetWindowLong(ctl.Handle, GWL_STYLE)
Return ((wndStyle And WS_VSCROLL) <> 0)
End Function
Friend Shared Sub ShowHideScrollBar(ByVal ctl As Control,
ByVal sb As SBOrientation, ByVal bShow As Boolean)
ShowScrollBar(ctl.Handle, sb, bShow)
End Sub
从最后一列窃取 xx 个像素;如果 VScroll 消失,则将像素放回去。
基本上发生的情况是ClientSizeChanged 触发两次:1) 当 VScroll 到达时,然后在几个微秒后 HScroll 由于 VScroll 而到达时再次触发。因此,您必须两次处理该事件。这会在第一遍中调整所需列的大小,并在第二遍中吃掉 HScroll。即使在第 1 步之后不再需要 HScroll,如果您不手动删除它,它也会短暂显示。
如果由于您的布局方式或用户调整列的大小而需要 HScroll,它无论如何都会调整最后一列的大小,但不会吃掉 HScroll。缺少检测何时不需要第一步的额外逻辑(通常,您看不到它执行此操作)。
警告:我不知道这将如何或是否适用于这个第 3 方主题。我不认为主题可以像这样修改内部滚动条。
另外,这是为了在子类 LV 中使用,我知道你已经这样做了。对于其他人,通过对引用进行一些修改,这也应该在表单上使用相关事件(例如ClientSizeChanged)“推送”更改,即使它在表单中留下了很多丑陋的代码。
这也没有包含在 IF AutoFit 属性测试之类的东西中。
Private orgClient As Rectangle ' original client size for comparing
Private _VScrollWidth As Integer
Private _resizedCol As Integer = -1
Private _VScroll As Boolean = False ' persistent Scroll flags
Private _HScroll As Boolean = False
' 3rd party???
_VScrollWidth = System.Windows.Forms.SystemInformation.VerticalScrollBarWidth
把它放在像ISupportInitialize.EndInit这样的地方:
orgClient = Me.ClientRectangle
Sub New 不是适合orgClient 的地方 - 控件尚未创建。 OnHandleCreated 可以,但如果没有ISupportInitialize,我会在控件上调用一个新的 Sub 来从 Form_Load 设置它。这实际上很方便,如果您想在手动调整列大小后“重新启动”事物然后再回来。例如:
' method on subclassed LV: from form load: thisLV.ResetClientSize
Public Sub ResetClientSize
orgClient = Me.ClientRectangle
End Sub
' a helper:
Private Function AllColumnsWidth() As Integer
Dim w As Integer = 0
For Each c As ColumnHeader In Columns
w += c.Width
Next
Return w
End Function
' The Meat
Protected Overrides Sub OnClientSizeChanged(ByVal e As System.EventArgs)
Dim VScrollVis As Boolean
Dim HScrollVis As Boolean
' get who is Now Showing...local vars
VScrollVis = NativeMethods.IsVScrollVisible(Me)
HScrollVis = NativeMethods.IsHScrollVisible(Me)
' width change
Dim delta As Integer = (orgClient.Width - ClientRectangle.Width)
Dim TotalWidth As Integer = AllColumnsWidth()
If (TotalWidth < ClientRectangle.Width - _VScrollWidth) And
(_resizedCol = -1) Then
Exit Sub
End If
Me.SuspendLayout()
' If VScroll IS showing, but WASNT showing the last time thru here
' ... then we are here because VScroll just appeared.
' That being the case, trim the desired column
If VScrollVis And _VScroll = False Then
' a smarter version finds the widest column and resizes THAT one
_resizedCol = Columns.Count - 1
' we have to wait for the HScroll to show up
' to remove it
Columns(_resizedCol).Width -= (delta + 1)
End If
' HScroll just appeared
If HScrollVis And (delta = _VScrollWidth) Then
' HScroll popped up, see if it is needed
If AllColumnsWidth() <= orgClient.Width Then
' no, go away foul beast !
NativeMethods.ShowHideScrollBar(Me,
NativeMethods.SBOrientation.SB_HORZ, False)
_HScroll = False ' hopefully
' allows us to set it back if the VScroll disappears
orgClient = ClientRectangle
Else
' ToDo: use this to detect when none of this is needed
_HScroll = HScrollVis
End If
End If
' If VScroll ISNOT showing, but WAS showing the last time thru here
' ...then we are here because VScroll disappeared
If VScrollVis = False And _VScroll = True Then
' put back the pixels
If _resizedCol <> -1 Then
Columns(_resizedCol).Width += (_VScrollWidth + 1)
' reset column tracker
_resizedCol = -1
' reset to new compare size
orgClient = ClientRectangle
End If
End If
_VScroll = VScrollVis
Me.ResumeLayout()
End Sub