2009-11-30 98 views

回答

3

要更改滚动条的物理大小,请参阅this

来自以下页面:Horizontal Scrollbar in ListBox。我修改它的WinForms和它的工作对我来说:

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

的宽度只能做到这一点通过创建自定义CLB控制和重写OnPaint事件。

编辑:好了,还是很基本的,但这篇文章在CodeProject可以帮助你:http://www.codeproject.com/KB/miscctrl/cutebutton.aspx

编辑2:你可能会喜欢定制滚动条的下面的例子:http://www.codeproject.com/KB/miscctrl/MotifScrollBars.aspx

+0

你能给我指导的例子吗? – Jon 2009-11-30 12:17:42

+0

这很困难,因为我不擅长创建自定义控件。首先,这里有一些基本的代码。我会尝试更深入地找到一些例子。 public class MyCheckedListBox:CheckedListBox { protected override void OnPaint(PaintEventArgs e){ base.OnPaint(e); } } – Webleeuw 2009-11-30 12:23:59

+0

谢谢你看到了我自己,但不幸的是没有滚动条宽度属性等 – Jon 2009-11-30 12:28:37

3

下面的代码使用的SPI_SETNONCLIENTMETRICS来更改滚动条宽度的系统范围设置。请注意,它将改变系统上的所有应用程序,而不仅仅是一个。您可能应该将其设置为配置项目,以便您可以根据需要将宽度更改回默认值。

[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; 

然后,您可以使用此代码来查看当前值的滚动条宽度

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

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

然后,您可以使用此代码来改变滚动条的大小...

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

metrics.iScrollWidth = 17; 

SystemParametersInfo(SPI_SETNONCLIENTMETRICS, metrics.cbSize, ref metrics, SPIF_SENDCHANGE); 
+0

在Windows 10上冻结 – Arci 2018-03-05 09:34:13