【问题标题】:Appears horizontal scrollbar after minimizing listview that has auto-resize columns width最小化具有自动调整列宽的列表视图后出现水平滚动条
【发布时间】:2014-11-10 03:11:11
【问题描述】:

我有 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;
    }
}

我在表单上添加了列表视图并将锚属性设置为“全部”(上 | 下 | 左 | 右)。 当我更改表单的大小时,一切正常。但是当我最大化一个表单(通过最大化框)并在该列具有正确大小之后最小化,但出现不应该的水平滚动条。

如果我点击它(不改变列或列表视图的大小),它就会消失。

我应该怎么做才能使这个滚动条不出现?

【问题讨论】:

    标签: c# .net listview scrollbar


    【解决方案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;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-08
      • 1970-01-01
      • 2015-03-01
      • 2020-05-28
      • 2013-03-08
      相关资源
      最近更新 更多