VB.NET自定义滚动条 [英] VB.NET Custom Scrollbars
问题描述
我有一个有点自定义界面的项目,但还没有找到一个外观和效果很好的自定义滚动条控件,我发现这个代码在线工作并且跟踪条很好,因为有很多很好的看起来可定制的轨道栏控制在那里。问题是代码以相反的方式工作,所以如果我向上滚动它向下滚动,最大值就像最小值。我认为这是因为轨道条只能垂直翻转,底部最大而不是顶部。那么我下面的代码可以修改工作,还是有一个漂亮的vb.net自定义滚动条?
I have a project with a somewhat custom interface but have yet to find a custom scroll bar control that looks and works well, I have found this code online which does work with and track bar which is good because there are plenty of nice looking customizable track bar controls out there. The problem is the code works in the opposite way so if I scroll up it scrolls down and the maximum is like the minimum. I think this is because the track bar can only be flipped vertical one way with the maximum at the bottom instead of the top. So can the code I have below be modifiyed to work or is there a nice looking custom scroll bar for vb.net?
Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs) Handles Panel1.Paint
Dim pt As New Point(Me.Panel1.AutoScrollPosition.X, Me.Panel1.AutoScrollPosition.Y)
Me.CustomScrollBar1.Minimum = 0
Me.CustomScrollBar1.Maximum = Me.Panel1.DisplayRectangle.Height
Me.CustomScrollBar1.Value = Math.Abs(Me.Panel1.AutoScrollPosition.Y)
Me.CustomScrollBar1.Refresh()
Me.CustomScrollBar1.Update()
End Sub
Private Sub CustomScrollBar1_Scroll(sender As Object, e As ScrollEventArgs) Handles CustomScrollBar1.Scroll
Panel1.AutoScrollPosition = New Point(5, CustomScrollBar1.Value)
CustomScrollBar1.Invalidate()
End Sub
Private Sub CustomScrollBar1_ValueChanged() Handles CustomScrollBar1.ValueChanged
Panel1.AutoScrollPosition = New Point(5, CustomScrollBar1.Value)
CustomScrollBar1.Invalidate()
End Sub
感谢您的帮助
Thanks for any help
推荐答案
这是我想出来做我想做的事情......但是有两组滚动条......一个用于17像素的面板和我设计的40个像素。
虽然不完美,但这种方法似乎有效!
---
Here's what I came up with to get this to do what I wanted... However there are 2 sets of scrollbars... The one for the Panel at 17 pixels and the ones that I designed at 40 pixels.
While not perfect, this methodology seems to work!
---
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Panel1 As System.Windows.Forms.Panel
Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
Friend WithEvents Panel2 As System.Windows.Forms.Panel
Friend WithEvents PictureBox2 As System.Windows.Forms.PictureBox
Friend WithEvents HScrollBar1 As System.Windows.Forms.HScrollBar
Friend WithEvents Panel3 As System.Windows.Forms.Panel
Friend WithEvents VScrollBar1 As System.Windows.Forms.VScrollBar
Friend WithEvents Splitter2 As System.Windows.Forms.Splitter
Friend WithEvents Splitter1 As System.Windows.Forms.Splitter
Private Sub InitializeComponent()
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
Me.Panel1 = New System.Windows.Forms.Panel
Me.Panel3 = New System.Windows.Forms.Panel
Me.VScrollBar1 = New System.Windows.Forms.VScrollBar
Me.Panel2 = New System.Windows.Forms.Panel
Me.HScrollBar1 = New System.Windows.Forms.HScrollBar
Me.PictureBox2 = New System.Windows.Forms.PictureBox
Me.PictureBox1 = New System.Windows.Forms.PictureBox
Me.Splitter2 = New System.Windows.Forms.Splitter
Me.Splitter1 = New System.Windows.Forms.Splitter
Me.Panel1.SuspendLayout()
Me.Panel3.SuspendLayout()
Me.Panel2.SuspendLayout()
Me.SuspendLayout()
'
'Panel1
'
Me.Panel1.AutoScroll = True
Me.Panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.Panel1.Controls.Add(Me.PictureBox1)
Me.Panel1.Dock = System.Windows.Forms.DockStyle.Fill
Me.Panel1.Location = New System.Drawing.Point(0, 0)
Me.Panel1.Name = "Panel1"
Me.Panel1.Size = New System.Drawing.Size(494, 408)
Me.Panel1.TabIndex = 0
'
'Panel3
'
Me.Panel3.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.Panel3.Controls.Add(Me.Splitter1)
Me.Panel3.Controls.Add(Me.VScrollBar1)
Me.Panel3.Controls.Add(Me.PictureBox2)
Me.Panel3.Dock = System.Windows.Forms.DockStyle.Right
Me.Panel3.Location = New System.Drawing.Point(494, 0)
Me.Panel3.Name = "Panel3"
Me.Panel3.Size = New System.Drawing.Size(42, 450)
Me.Panel3.TabIndex = 2
'
'VScrollBar1
'
Me.VScrollBar1.Dock = System.Windows.Forms.DockStyle.Fill
Me.VScrollBar1.LargeChange = 50
Me.VScrollBar1.Location = New System.Drawing.Point(0, 0)
Me.VScrollBar1.Name = "VScrollBar1"
Me.VScrollBar1.Size = New System.Drawing.Size(40, 408)
Me.VScrollBar1.TabIndex = 1
'
'Panel2
'
Me.Panel2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.Panel2.Controls.Add(Me.HScrollBar1)
Me.Panel2.Dock = System.Windows.Forms.DockStyle.Bottom
Me.Panel2.Location = New System.Drawing.Point(0, 408)
Me.Panel2.Name = "Panel2"
Me.Panel2.Size = New System.Drawing.Size(494, 42)
Me.Panel2.TabIndex = 1
'
'HScrollBar1
'
Me.HScrollBar1.Dock = System.Windows.Forms.DockStyle.Fill
Me.HScrollBar1.LargeChange = 50
Me.HScrollBar1.Location = New System.Drawing.Point(0, 0)
Me.HScrollBar1.Name = "HScrollBar1"
Me.HScrollBar1.Size = New System.Drawing.Size(492, 40)
Me.HScrollBar1.TabIndex = 2
'
'PictureBox2
'
Me.PictureBox2.Dock = System.Windows.Forms.DockStyle.Bottom
Me.PictureBox2.Image = CType(resources.GetObject("PictureBox2.Image"), System.Drawing.Image)
Me.PictureBox2.Location = New System.Drawing.Point(0, 408)
Me.PictureBox2.Name = "PictureBox2"
Me.PictureBox2.Size = New System.Drawing.Size(40, 40)
Me.PictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize
Me.PictureBox2.TabIndex = 0
Me.PictureBox2.TabStop = False
'
'PictureBox1
'
Me.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.PictureBox1.Location = New System.Drawing.Point(380, 328)
Me.PictureBox1.Name = "PictureBox1"
Me.PictureBox1.Size = New System.Drawing.Size(368, 316)
Me.PictureBox1.TabIndex = 0
Me.PictureBox1.TabStop = False
'
'Splitter2
'
Me.Splitter2.Dock = System.Windows.Forms.DockStyle.Bottom
Me.Splitter2.Enabled = False
Me.Splitter2.Location = New System.Drawing.Point(0, 407)
Me.Splitter2.Name = "Splitter2"
Me.Splitter2.Size = New System.Drawing.Size(494, 1)
Me.Splitter2.TabIndex = 3
Me.Splitter2.TabStop = False
'
'Splitter1
'
Me.Splitter1.BackColor = System.Drawing.Color.Black
Me.Splitter1.Dock = System.Windows.Forms.DockStyle.Bottom
Me.Splitter1.Location = New System.Drawing.Point(0, 407)
Me.Splitter1.Name = "Splitter1"
Me.Splitter1.Size = New System.Drawing.Size(40, 1)
Me.Splitter1.TabIndex = 2
Me.Splitter1.TabStop = False
Me.Splitter1.Visible = False
'
'Form1
'
Me.AutoScale = False
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(536, 450)
Me.Controls.Add(Me.Splitter2)
Me.Controls.Add(Me.Panel1)
Me.Controls.Add(Me.Panel2)
Me.Controls.Add(Me.Panel3)
Me.Name = "Form1"
Me.Text = "Form1"
Me.Panel1.ResumeLayout(False)
Me.Panel3.ResumeLayout(False)
Me.Panel2.ResumeLayout(False)
Me.ResumeLayout(False)
End Sub
#End Region
Dim PictureRightSize As Integer
Dim PictureBottomSize As Integer
Dim Panel1Width As Integer
Dim Panel1Height As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Panel1.AutoScroll = True
'MsgBox("Sadly the Normal Scrollbar WIDTH and HEIGHT don't seem to be easily resized at this point in time!")
RecalculateFormScrollBarData()
End Sub
Private Sub RecalculateFormScrollBarData()
' +17 is for the Panel ScrollBars then Add 10 so that we have a little "wiggle" room!
Dim ScrollSize As Integer = Panel2.Height + 17 + 10
Panel1Width = Panel1.Width
Panel1Height = Panel1.Height
PictureRightSize = -Panel1.AutoScrollPosition.X + PictureBox1.Location.X + PictureBox1.Width + ScrollSize - Panel1Width ' + PictureBox1.Width + ScrollSize
PictureBottomSize = -Panel1.AutoScrollPosition.Y + PictureBox1.Location.Y + PictureBox1.Height + ScrollSize - Panel1Height ' + PictureBox1.Height + ScrollSize
HScrollBar1.Maximum = PictureRightSize
VScrollBar1.Maximum = PictureBottomSize
'MsgBox("Panel Bottom Right X, Y Data: " & PictureRightSize & ", " & PictureBottomSize & vbCrLf & vbCrLf & _
' "Current Panel X, Y Data: " & Panel1Width & ", " & Panel1Height)
End Sub
Private Sub Form1_Resize(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Resize
RecalculateFormScrollBarData()
End Sub
Private Sub VScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles VScrollBar1.Scroll
'Panel1.AutoScroll = True
Panel1.AutoScrollPosition = New Point(HScrollBar1.Value, VScrollBar1.Value)
Panel1.Invalidate()
End Sub
Private Sub HScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles HScrollBar1.Scroll
'Panel1.AutoScroll = True
Panel1.AutoScrollPosition = New Point(HScrollBar1.Value, VScrollBar1.Value)
Panel1.Invalidate()
End Sub
End Class
---
在RecalculateFormScrollBarData例程中,计算中的-Panel1.AutoScrollPosition.X和-Panel1.AutoScrollPosition.Y允许我们当前正在查看的Panel1当前位置偏移被考虑在内。
所以这是我目前最好的解决方案。
所以这似乎暂时解决了目前太小的滚动条时间!
---
In the RecalculateFormScrollBarData routine, the -Panel1.AutoScrollPosition.X and -Panel1.AutoScrollPosition.Y in the calculations allow for the Panel1 Current Location Offset that we are currently viewing is taken into consideration.
So this is my best solution at the present time.
So this seems to temporarily solve the too small scrollbars for the present time!
试一试:
自定义滚动条库1.1版 [ ^ ]
滚动自己的滚动条 [ ^ ]
核心自定义滚动条类 [ ^ ]
自定义绘制滚动条 [ ^ ]
使用自定义滚动条控件替换窗口的内部滚动条ol [ ^ ]
Try it:
Custom Scrollbar Library version 1.1[^]
Roll Your Own Scroll Bar[^]
Core Custom Scrollbar Class[^]
Custom Drawn Scrollbar[^]
Replace a Window's Internal Scrollbar with a customdraw scrollbar Control[^]
这篇关于VB.NET自定义滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!