簡體   English   中英

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

[英]How to implement fancy scroll bar for multiline textbox?

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

標准滾動條和花式滾動條

對那個花哨的滾動條有什么想法嗎?

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

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

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

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

如果花哨的滾動條可以實現為另一個控件(您的圖像實際上看起來像那樣),這里是支持自定義滾動的 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM