我有ListView,它根据大小设置列的宽度:
public class CommonListView : ListView
{
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
int columnWidth = (ClientSize.Width - SystemInformation.VerticalScrollBarWidth - 6) / Columns.Count;
foreach (ColumnHeader column in Columns)
column.Width = columnWidth;
}
}
我在表单上添加了listview,并将anchor属性设置为'All'(Top | Bottom | Left | Right)。 当我更改表单的大小时,所有工作正常。但是当我最大化一个表单(通过最大化框)并最小化后,该列具有正确的大小,但出现水平滚动条不应该。
如果我点击它(不改变列或列表视图的大小),它就会消失。
如何才能显示此滚动条?
答案 0 :(得分:1)
我找到解决下述问题的方法。
private FormWindowState _previouseFormWindowState;
private void MainForm_Resize(object sender, EventArgs e)
{
if (_previouseFormWindowState == FormWindowState.Maximized && WindowState != FormWindowState.Maximized)
{
var si = new SCROLLINFO();
if (GetScrollInfo(listView, ref si, ScrollBarDirection.SB_HORZ))
{
if (si.nMax < si.nPage)
{
ShowScrollBar(listView.Handle, (int)ScrollBarDirection.SB_HORZ, false);
}
}
}
_previouseFormWindowState = WindowState;
}
public enum ScrollInfoMask : uint
{
SIF_RANGE = 0x1,
SIF_PAGE = 0x2,
SIF_POS = 0x4,
SIF_DISABLENOSCROLL = 0x8,
SIF_TRACKPOS = 0x10,
SIF_ALL = (SIF_RANGE | SIF_PAGE | SIF_POS | SIF_TRACKPOS),
}
public enum ScrollBarDirection
{
SB_HORZ = 0,
SB_VERT = 1,
SB_CTL = 2,
SB_BOTH = 3
}
[Serializable, StructLayout(LayoutKind.Sequential)]
public struct SCROLLINFO
{
public uint cbSize;
public uint fMask;
public int nMin;
public int nMax;
public uint nPage;
public int nPos;
public int nTrackPos;
}
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ShowScrollBar(IntPtr hWnd, int wBar, bool bShow);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetScrollInfo(IntPtr hwnd, int fnBar, ref SCROLLINFO lpsi);
public static bool GetScrollInfo(Control ctrl, ref SCROLLINFO si, ScrollBarDirection scrollBarDirection)
{
if (ctrl != null)
{
si.cbSize = (uint)Marshal.SizeOf(si);
si.fMask = (int)ScrollInfoMask.SIF_ALL;
if (GetScrollInfo(ctrl.Handle, (int)scrollBarDirection, ref si))
return true;
}
return false;
}