ListBox设置水平滚动条

为ListBox关联控件变量m_ListBox

  1. for (int i=0; i<=20; ++i)
  2. {
  3.     m_List.AddString(L"123456789123456789123456789123456789123456789");
  4. }


当ListBox控件加入的字符串宽度过长时,新加入的有些字符串就不能显示,此时就需要加入水平滚动条

先将ListBox的Horizontal Scroll勾选上
在对话框CTestDlg类中加入SetHScroll成员函数

  1. void CTestDlg::SetHScroll()
  2. {
  3.     CDC* dc = GetDC();
  4.     SIZE s;
  5.     int index;
  6.     CString str;
  7.     long temp;
  8.     for(index= 0; index< m_List.GetCount(); index++)
  9.     {
  10.         m_List.GetText(index,str);
  11.         s = dc->GetTextExtent(str,str.GetLength()+1);   // 获取字符串的像素大小
  12.         // 如果新的字符串宽度大于先前的水平滚动条宽度,则重新设置滚动条宽度
  13.         // IDC_LISTBOX为m_List的资源ID
  14.         temp = (long)SendDlgItemMessage(IDC_LIST1, LB_GETHORIZONTALEXTENT, 0, 0); //temp得到滚动条的宽度
  15.         if (s.cx > temp)  
  16.         {
  17.             SendDlgItemMessage(IDC_LIST1, LB_SETHORIZONTALEXTENT, (WPARAM)s.cx, 0);
  18.         }
  19.     }
  20.     ReleaseDC(dc);
  21. }



之后只要在ListBox加入条目之后调用SetHScroll

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 在XAML中,可以使用ScrollBarStyle属性来设置ListBox滚动条样式。 2. 可以使用Style属性为ListBox滚动条定义一个样式。例如,以下是一个简单的样式,它将滚动条的背景颜色设置为灰色: ```xaml <ListBox ScrollViewer.VerticalScrollBarStyle="{StaticResource ScrollBarStyle}"> <ListBox.Resources> <Style TargetType="ScrollBar" x:Key="ScrollBarStyle"> <Setter Property="Background" Value="Gray"/> </Style> </ListBox.Resources> </ListBox> ``` 3. 可以使用VisualStateManager类来定义滚动条的不同状态,例如:正常、拖动、禁用等等。以下是一个示例,其中定义了三种状态: ```xaml <ListBox ScrollViewer.VerticalScrollBarStyle="{StaticResource ScrollBarStyle}"> <ListBox.Resources> <Style TargetType="ScrollBar" x:Key="ScrollBarStyle"> <Setter Property="Background" Value="Gray"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ScrollBar"> <Grid> <VisualStateManager.VisualStateGroups> <VisualStateGroup x:Name="CommonStates"> <VisualState x:Name="Normal"/> <VisualState x:Name="Disabled"> <Storyboard> <DoubleAnimation Storyboard.TargetName="PART_Thumb" Storyboard.TargetProperty="Opacity" To="0.5" Duration="0"/> </Storyboard> </VisualState> <VisualState x:Name="MouseOver"> <Storyboard> <DoubleAnimation Storyboard.TargetName="PART_Thumb" Storyboard.TargetProperty="Opacity" To="1" Duration="0"/> </Storyboard> </VisualState> </VisualStateGroup> </VisualStateManager.VisualStateGroups> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <Border Grid.Row="0" Background="{TemplateBinding Background}" CornerRadius="2"/> <RepeatButton Grid.Row="0" Style="{StaticResource ScrollBarButtonStyle}" Command="{TemplateBinding Track.DecreaseCommand}" IsEnabled="{TemplateBinding Track.DecreaseButton.IsEnabled}"/> <Track Grid.Row="0" Name="PART_Track"> <Track.DecreaseRepeatButton> <RepeatButton Style="{StaticResource ScrollBarButtonStyle}" Command="{TemplateBinding Track.DecreaseCommand}" IsEnabled="{TemplateBinding Track.DecreaseButton.IsEnabled}"/> </Track.DecreaseRepeatButton> <Track.Thumb> <Thumb x:Name="PART_Thumb" Style="{StaticResource ScrollBarThumbStyle}"/> </Track.Thumb> <Track.IncreaseRepeatButton> <RepeatButton Style="{StaticResource ScrollBarButtonStyle}" Command="{TemplateBinding Track.IncreaseCommand}" IsEnabled="{TemplateBinding Track.IncreaseButton.IsEnabled}"/> </Track.IncreaseRepeatButton> </Track> <RepeatButton Grid.Row="1" Style="{StaticResource ScrollBarButtonStyle}" Command="{TemplateBinding Track.IncreaseCommand}" IsEnabled="{TemplateBinding Track.IncreaseButton.IsEnabled}"/> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> <Style TargetType="Thumb" x:Key="ScrollBarThumbStyle"> <Setter Property="Background" Value="#FF8E8F8F"/> <Setter Property="BorderThickness" Value="1"/> <Setter Property="BorderBrush" Value="#FFCED2D8"/> <Setter Property="Width" Value="8"/> <Setter Property="Height" Value="20"/> <Setter Property="IsTabStop" Value="False"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Thumb"> <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"/> </ControlTemplate> </Setter.Value> </Setter> </Style> <Style TargetType="RepeatButton" x:Key="ScrollBarButtonStyle"> <Setter Property="Background" Value="Transparent"/> <Setter Property="BorderThickness" Value="1"/> <Setter Property="BorderBrush" Value="#FFCED2D8"/> <Setter Property="Width" Value="10"/> <Setter Property="Height" Value="10"/> <Setter Property="IsTabStop" Value="False"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="RepeatButton"> <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"/> </ControlTemplate> </Setter.Value> </Setter> </Style> </ListBox.Resources> </ListBox> ``` 4. 可以使用ScrollViewer类来设置ListBox的滚动行为。例如,以下是一个示例,它启用了水平滚动条和垂直滚动条: ```xaml <ListBox ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto"> </ListBox> ``` 5. 可以使用ScrollBar类的各种属性和事件来自定义ListBox滚动条。例如,以下是一个示例,它定义了一个自定义滚动条,该滚动条使用Thumb.DragStarted事件和Thumb.DragDelta事件来更新ListBox的滚动位置: ```xaml <ListBox> <ListBox.Resources> <Style TargetType="ScrollBar"> <Setter Property="Background" Value="Gray"/> <Setter Property="Width" Value="10"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ScrollBar"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <Border Grid.Row="0" Background="{TemplateBinding Background}" CornerRadius="2"/> <Thumb x:Name="PART_Thumb" Background="#FF8E8F8F" BorderThickness="1" BorderBrush="#FFCED2D8" Width="8" Height="20" IsTabStop="False" DragStarted="PART_Thumb_DragStarted" DragDelta="PART_Thumb_DragDelta"/> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> </ListBox.Resources> </ListBox> ``` ```csharp private void PART_Thumb_DragStarted(object sender, DragStartedEventArgs e) { var thumb = sender as Thumb; var scrollBar = thumb.TemplatedParent as ScrollBar; if (scrollBar != null) { scrollBar.CaptureMouse(); } } private void PART_Thumb_DragDelta(object sender, DragDeltaEventArgs e) { var thumb = sender as Thumb; var scrollBar = thumb.TemplatedParent as ScrollBar; if (scrollBar != null) { double newValue = scrollBar.Value - e.VerticalChange; if (newValue < scrollBar.Minimum) { newValue = scrollBar.Minimum; } else if (newValue > scrollBar.Maximum) { newValue = scrollBar.Maximum; } scrollBar.Value = newValue; } } ``` 以上是一些设置WPF ListBox滚动条的方法。可以根据需要进行自定义和修改,以满足具体的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值