Winforms – Ajuster la largeur de la barre de défilement verticale sur CheckedListBox

J’ai un CheckListBox sur mon formulaire mais je veux élargir la barre de défilement car les utilisateurs utilisent des écrans tactiles et non une souris.

Comment puis-je changer la largeur de la barre de défilement?

EDIT: je parle de la largeur de la barre de défilement verticale

Pour modifier la taille physique de la barre de défilement, reportez-vous à la section suivante .

Cela provient de la page suivante: Barre de défilement horizontale dans ListBox . Je l’ai modifié pour Winforms et cela a fonctionné pour moi:

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); } } } 

Le code suivant utilise SPI_SETNONCLIENTMETRICS pour modifier le paramètre à l’échelle du système pour la largeur de la barre de défilement. Notez que cela changera toutes les applications du système, pas une seule. Vous devriez probablement en faire un élément de configuration afin de pouvoir redéfinir la largeur par défaut si vous en avez besoin.

  [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 lfSsortingkeOut; public byte lfCharSet; public byte lfOutPrecision; public byte lfClipPrecision; public byte lfQuality; public byte lfPitchAndFamily; ///  ///  means that the ssortingng /// should be marshalled as an array of TCHAR embedded in the /// structure. This implies that the font names can be no larger /// than  including the terminating '\0'. /// That works out to 31 characters. ///  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = LF_FACESIZE)] public ssortingng lfFaceName; // to shut it up about the warnings public LOGFONT(ssortingng lfFaceName) { this.lfFaceName = lfFaceName; lfHeight = lfWidth = lfEscapement = lfOrientation = lfWeight = 0; lfItalic = lfUnderline = lfSsortingkeOut = 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; ///  /// Since  is a struct instead of a class, /// we don't have to do any special marshalling here. Much /// simpler this way. ///  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; 

Vous pouvez ensuite utiliser ce code pour voir la valeur actuelle de la largeur de la barre de défilement.

 NONCLIENTMETRICS mesortingcs = new NONCLIENTMETRICS(); mesortingcs.cbSize = Marshal.SizeOf(mesortingcs); SystemParametersInfo(SPI_GETNONCLIENTMETRICS, 0, ref mesortingcs, 0); MessageBox.Show(mesortingcs.iScrollWidth.ToSsortingng()); 

Vous pouvez ensuite utiliser ce code pour changer la taille de la barre de défilement …

 NONCLIENTMETRICS mesortingcs = new NONCLIENTMETRICS(); mesortingcs.cbSize = Marshal.SizeOf(mesortingcs); mesortingcs.iScrollWidth = 17; SystemParametersInfo(SPI_SETNONCLIENTMETRICS, mesortingcs.cbSize, ref mesortingcs, SPIF_SENDCHANGE); 

Vous ne pouvez le faire qu’en créant un contrôle CLB personnalisé et en remplaçant l’événement OnPaint.

Edit: Ok, toujours très basique, mais cet article sur CodeProject pourrait vous aider: http://www.codeproject.com/KB/miscctrl/cutebutton.aspx

Edit 2: Vous pourriez aimer l’exemple suivant d’une barre de défilement personnalisée: http://www.codeproject.com/KB/miscctrl/MotifScrollBars.aspx