Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
806 views
in Technique[技术] by (71.8m points)

c# - Winforms - Adjust width of vertical scrollbar on CheckedListBox

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 ''.
        /// 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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...