A horizontal scrollbar appears after minimizing the list, which has an auto-resize width
I have a ListView that sets the width of columns based on size:
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;
}
}
I added a listview to the form and set the anchor property to "All" ("Up | Down | Left | Right"). When I resize the form everything works correctly. But when I maximize the form (via the maximize box) and minimize after the columns are the correct size, but a horizontal scrollbar appears that shouldn't be.
If I click on it (without resizing the columns or list), it disappears.
What if this scrollbar is not displayed?
+3
source to share
1 answer
The method I found to solve my problem is described below.
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;
}
+1
source to share