Dot-Net

如何為多行文本框實現精美的捲動條?

  • July 3, 2013

我想為多行文本框實現一個精美的捲動條,如下圖所示:

標準捲動條和花式捲動條

對那個花哨的捲動條有什麼想法嗎?

P/S:我想要 vb.net 解決方案。

如果花哨的捲動條可以實現為另一個控制項(您的圖像實際上看起來像那樣),這裡是支持自定義滾動的 ListBox 的程式碼:

Imports System.Runtime.InteropServices

Public Class CustomScrollListBox
   Inherits ListBox

   Public Sub Scroll(ByVal percentage As Single)
       If (percentage < 0.0!) Then
           Throw New ArgumentException(Nothing, "percentage")
       End If

       ' Sends the scroll / set position Windows message
       Const WM_VSCROLL As Integer = &H115
       Const SB_THUMBPOSITION As Integer = 4
       Dim wp As Integer = CInt(((percentage * MyBase.Items.Count) * 65536.0!)) + SB_THUMBPOSITION
       CustomScrollListBox.SendMessage(MyBase.Handle, WM_VSCROLL, New IntPtr(wp), IntPtr.Zero)
   End Sub

   <DllImport("user32.dll")> _
   Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
   End Function

   Protected Overrides ReadOnly Property CreateParams() As CreateParams
       Get
           ' Removes the vertical scroll window style
           Dim p As CreateParams = MyBase.CreateParams
           Const WS_VSCROLL As Integer = &H200000
           p.Style = (p.Style And -WS_VSCROLL)
           Return p
       End Get
   End Property

End Class

這是一個使用它的範例表單。例如,我已將自定義捲動條實現為標準 Trackbar (trackBar1):

Public Class Form1

   Private trackBar As TrackBar
   Private listBox As CustomScrollListBox

   Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
       ' initialize some listbox props
       listBox = New CustomScrollListBox
       listBox.Location = New Point(&H19, 210)
       listBox.Name = "listBox2"
       listBox.Size = New Size((&H17D - Me.TrackBar1.Width), &HAD)
       listBox.TabIndex = 1
       MyBase.Controls.Add(listBox)

      ' add some items
       Dim i As Integer
       For i = 0 To 100 - 1
           listBox.Items.Add(("item" & i))
       Next i

   End Sub


   Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
       ' compute trackbar's position as a percentage
       listBox.Scroll(CSng(TrackBar1.Maximum - TrackBar1.Value) / TrackBar1.Maximum - TrackBar1.Minimum)
   End Sub

End Class

這段程式碼非常笨重且令人沮喪,但它正在工作

這是一個完全繪製的 GDI+ 自定義捲動條控制項類。它的所有繪圖方法都是可覆蓋的,允許開發人員按照他們的選擇進行繪製。

http://www.codeproject.com/KB/miscctrl/corescrollbar.aspx

引用自:https://stackoverflow.com/questions/6163227