Force the display of scroll bars in a ListView?

后端 未结 1 547
遇见更好的自我
遇见更好的自我 2020-12-21 06:19
  • The background: Most of us know the SysListView32 common control and the equivalent wrapper ListView class provided by th

相关标签:
1条回答
  • 2020-12-21 07:06
    • Information:

      • Firstly, I have to admit this is an okay answer and not the best/most efficient one. If you have a different answer from mine, please post it.
      • Secondly, this answer owes some credit to Plutonix's answer, experimenting with which I learned that by default ListView does not have WS_HSCROLL | WS_VSCROLL flags set in its styles.
        • This is why my previous workaround had problem with themes.
        • These Classic scroll bars are ones Windows provides to Controls that do not have these flags set.
        • Changing the CreateParams does not work either. You have to set it manually in the OnHandleCreated method using SetWindowLong.
        • The solution I am posting does not use the above technique. Apparently, calling ShowScrollBar for each window message forces these flags to be set.
    • The Solution:

      • Define your WndProc like the following:

        protected override void WndPoc(ref Message m)
        {
        //custom code before calling base.WndProc
        base.WndProc(ref m);
        //custom after base.WndProc returns
        WmScroll(); //VERY INEFFICIENT, called for each message :(
        }
        
      • Define WmScroll() as follows:

        protected virtual void WmScroll()
        {
        NativeMethods.ShowScrollBar(Handle, SB_BOTH, true);
        	
        //si.fMask = SIF_PAGE | SIF_RANGE <- initialized in .ctor
        	
        NativeMethods.GetScrollInfo(Handle, SB_HORZ, ref si);
        if(si.nMax < si.nPage)
        	NativeMethods.EnableScrollBar(Handle, SB_HORZ, ESB_DISABLE_BOTH);
        else
        	NativeMethods.EnableScrollBar(Handle, SB_HORZ, ESB_ENABLE_BOTH);
        NativeMethods.GetScrollInfo(Handle, SB_VERT, ref si);
        if(si.nMax < si.nPage)
        	NativeMethods.EnableScrollBar(Handle, SB_VERT, ESB_DISABLE_BOTH);
        else
        	NativeMethods.EnableScrollBar(Handle, SB_VERT, ESB_ENABLE_BOTH);
        }
        
      • Output:

    It now, looks like:

    These are with another item added featuring the horizontal scroll and working ListViewGroup collapse button:

    • Imperfection, yes there is:
      • A call to AutoResizeColumns is required if group collapse changes effective text width, otherwise the vertical scroll bar hides the collapse buttons.
    0 讨论(0)
提交回复
热议问题