Winforms - Adjust width of vertical scrollbar on CheckedListBox
Asked Answered
S

3

3

I have a CheckListBox on my form but I want to make the scrollbar wider as users are using touch screens not a mouse.

How can I change the scroll bar width?

EDIT: I am talking about the width of the vertical scrollbar

Spiculum answered 30/11, 2009 at 12:14 Comment(1)
community.devexpress.com/forums/p/62878/261988.aspxPearman
P
3

To change the physical size of the scrollbar, see this.

This came from the following page: Horizontal Scrollbar in ListBox. I modified it for Winforms and it worked for me:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace CheckedListBoxScrollBarsWidth
{
   public partial class Form1 : Form
   {
      const int LB_GETHORIZONTALEXTENT = 0x0193;
      const int LB_SETHORIZONTALEXTENT = 0x0194;

      const long WS_HSCROLL = 0x00100000L;

      const int SWP_FRAMECHANGED = 0x0020;
      const int SWP_NOMOVE = 0x0002;
      const int SWP_NOSIZE = 0x0001;
      const int SWP_NOZORDER = 0x0004;

      const int GWL_STYLE = (-16);    

      public Form1()
      {
         InitializeComponent();
         checkedListBox1.HorizontalScrollbar = true;
         AddStyle(checkedListBox1.Handle, (uint)WS_HSCROLL);
         SendMessage(checkedListBox1.Handle, LB_SETHORIZONTALEXTENT, 1000, 0);
      }

      [DllImport("user32.dll")]
      static extern int SendMessage(IntPtr hwnd, int msg, int wParam, int lParam);

      [DllImport("user32.dll")]
      static extern uint GetWindowLong(IntPtr hwnd, int index);

      [DllImport("user32.dll")]
      static extern void SetWindowLong(IntPtr hwnd, int index, uint value);

      [DllImport("user32.dll")]
      static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X,
            int Y, int cx, int cy, uint uFlags);


      private void AddStyle(IntPtr handle, uint addStyle)
      {
         // Get current window style
         uint windowStyle = GetWindowLong(handle, GWL_STYLE);

         // Modify style
         SetWindowLong(handle, GWL_STYLE, windowStyle | addStyle);

         // Let the window know of the changes
         SetWindowPos(handle, IntPtr.Zero, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOZORDER | SWP_NOSIZE | SWP_FRAMECHANGED);
      }
   }
}
Pearman answered 30/11, 2009 at 13:30 Comment(0)
K
3

The following code makes use of SPI_SETNONCLIENTMETRICS to change the system wide setting for the scrollbar width. NOTE that it will change all applications on the system not just a single one. You should probably make this a configuration item so that you can change the width back to a default if you need to.

 [DllImport("user32", CharSet = CharSet.Auto)]
    private static extern int SystemParametersInfo(int uAction, int uParam, ref NONCLIENTMETRICS lpvParam, int fuWinIni);

    private const int LF_FACESIZE = 32;

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    private struct LOGFONT
    {
        public int lfHeight;
        public int lfWidth;
        public int lfEscapement;
        public int lfOrientation;
        public int lfWeight;
        public byte lfItalic;
        public byte lfUnderline;
        public byte lfStrikeOut;
        public byte lfCharSet;
        public byte lfOutPrecision;
        public byte lfClipPrecision;
        public byte lfQuality;
        public byte lfPitchAndFamily;

        /// <summary>
        /// <see cref="UnmanagedType.ByValTStr"/> means that the string
        /// should be marshalled as an array of TCHAR embedded in the
        /// structure.  This implies that the font names can be no larger
        /// than <see cref="LF_FACESIZE"/> including the terminating '\0'.
        /// That works out to 31 characters.
        /// </summary>
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = LF_FACESIZE)]
        public string lfFaceName;

        // to shut it up about the warnings
        public LOGFONT(string lfFaceName)
        {
            this.lfFaceName = lfFaceName;
            lfHeight = lfWidth = lfEscapement = lfOrientation = lfWeight = 0;
            lfItalic = lfUnderline = lfStrikeOut = lfCharSet = lfOutPrecision
            = lfClipPrecision = lfQuality = lfPitchAndFamily = 0;
        }
    }

    private struct NONCLIENTMETRICS
    {
        public int cbSize;
        public int iBorderWidth;
        public int iScrollWidth;
        public int iScrollHeight;
        public int iCaptionWidth;
        public int iCaptionHeight;
        /// <summary>
        /// Since <see cref="LOGFONT"/> is a struct instead of a class,
        /// we don't have to do any special marshalling here.  Much
        /// simpler this way.
        /// </summary>
        public LOGFONT lfCaptionFont;
        public int iSMCaptionWidth;
        public int iSMCaptionHeight;
        public LOGFONT lfSMCaptionFont;
        public int iMenuWidth;
        public int iMenuHeight;
        public LOGFONT lfMenuFont;
        public LOGFONT lfStatusFont;
        public LOGFONT lfMessageFont;
    }

    private const int SPI_GETNONCLIENTMETRICS = 41;
    private const int SPI_SETNONCLIENTMETRICS = 42;
    private const int SPIF_SENDCHANGE = 2;

You can then use this code to see the current value for the scrollbar width

NONCLIENTMETRICS metrics = new NONCLIENTMETRICS();
metrics.cbSize = Marshal.SizeOf(metrics);
SystemParametersInfo(SPI_GETNONCLIENTMETRICS, 0, ref metrics, 0);

MessageBox.Show(metrics.iScrollWidth.ToString());

You can then use this code to change the size of the scrollbar...

NONCLIENTMETRICS metrics = new NONCLIENTMETRICS();
metrics.cbSize = Marshal.SizeOf(metrics);

metrics.iScrollWidth = 17;

SystemParametersInfo(SPI_SETNONCLIENTMETRICS, metrics.cbSize, ref metrics, SPIF_SENDCHANGE);
Kalvn answered 30/11, 2009 at 12:55 Comment(2)
Freezes on Windows 10Gigue
I MESSED UP MY WINDOWS 10 BY DOING THIS!!! The scroll bars and other non-client windows UI items became enormously small after executing the SPI_SETNONCLIENTMETRICS call above. So the question still remains - how to that in Windows 10 the right way?Anodic
P
3

To change the physical size of the scrollbar, see this.

This came from the following page: Horizontal Scrollbar in ListBox. I modified it for Winforms and it worked for me:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace CheckedListBoxScrollBarsWidth
{
   public partial class Form1 : Form
   {
      const int LB_GETHORIZONTALEXTENT = 0x0193;
      const int LB_SETHORIZONTALEXTENT = 0x0194;

      const long WS_HSCROLL = 0x00100000L;

      const int SWP_FRAMECHANGED = 0x0020;
      const int SWP_NOMOVE = 0x0002;
      const int SWP_NOSIZE = 0x0001;
      const int SWP_NOZORDER = 0x0004;

      const int GWL_STYLE = (-16);    

      public Form1()
      {
         InitializeComponent();
         checkedListBox1.HorizontalScrollbar = true;
         AddStyle(checkedListBox1.Handle, (uint)WS_HSCROLL);
         SendMessage(checkedListBox1.Handle, LB_SETHORIZONTALEXTENT, 1000, 0);
      }

      [DllImport("user32.dll")]
      static extern int SendMessage(IntPtr hwnd, int msg, int wParam, int lParam);

      [DllImport("user32.dll")]
      static extern uint GetWindowLong(IntPtr hwnd, int index);

      [DllImport("user32.dll")]
      static extern void SetWindowLong(IntPtr hwnd, int index, uint value);

      [DllImport("user32.dll")]
      static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X,
            int Y, int cx, int cy, uint uFlags);


      private void AddStyle(IntPtr handle, uint addStyle)
      {
         // Get current window style
         uint windowStyle = GetWindowLong(handle, GWL_STYLE);

         // Modify style
         SetWindowLong(handle, GWL_STYLE, windowStyle | addStyle);

         // Let the window know of the changes
         SetWindowPos(handle, IntPtr.Zero, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOZORDER | SWP_NOSIZE | SWP_FRAMECHANGED);
      }
   }
}
Pearman answered 30/11, 2009 at 13:30 Comment(0)
E
0

You can only do that by creating a custom CLB control and overriding the OnPaint event.

Edit: Ok, still very basic, but this article on CodeProject might help you: http://www.codeproject.com/KB/miscctrl/cutebutton.aspx

Edit 2: You might like the following example of a customized scrollbar: http://www.codeproject.com/KB/miscctrl/MotifScrollBars.aspx

Evante answered 30/11, 2009 at 12:17 Comment(4)
Could you give me a guide example?Spiculum
That's difficult, because I'm no expert in creating custom controls. To start with, here's some basic code. I'll try to find some example with more depth. public class MyCheckedListBox : CheckedListBox { protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); } }Evante
Thanks have seen those myself but unfortunately no scroll bar width properties etcSpiculum
I was afraid of that. Still, I think that being creative yourself with the OnPaint and OnMouseDown event handlers is the only solution.Evante

© 2022 - 2024 — McMap. All rights reserved.