2019/11/17(Sun) 10:06:33 編集(投稿者)
2019/11/16(Sat) 22:17:50 編集(投稿者)
<pre><pre>■No93046 (小次郎 さん) に返信
No93034とほぼ同じですが、継承コントロールとして実装してみました。
WndProcをOverridesしてメッセージを自前で処理するというような事は、
今回の場合は必要ないと思います。
【追記】-----------------------------------------------------------
OrientationプロパティがVerticalの場合やRightToLeftプロパティがYesは考慮していません。
これらも考慮するなら、OnMouseDownメソッド内で目盛領域を取得して、
座標の大小判定を場合分けすれば良いと思います。
-------------------------------------------------------------------
Public Class TrackBarEx
Inherits TrackBar
Private oldValue As Integer
Private channel As Rectangle
Private Structure RECT
Public left, top, right, bottom As Integer
End Structure
<DllImport("user32.dll", EntryPoint:="SendMessage")>
Private Shared Function SendMessageRect(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wp As IntPtr, ByRef lp As RECT) As IntPtr
End Function
Private Function GetChanelRectangle() As Rectangle
Const TBM_GETCHANNELRECT As Integer = &H400 + 26
Dim rc As RECT = New RECT()
SendMessageRect(Handle, TBM_GETCHANNELRECT, IntPtr.Zero, rc)
Return New Rectangle(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top)
End Function
Protected Overrides Sub OnHandleCreated(e As EventArgs)
MyBase.OnHandleCreated(e)
oldValue = Minimum
Value = Minimum
channel = GetChanelRectangle()
End Sub
Private Sub TrackBarValueCheck()
If oldValue <> Value Then
MyBase.OnValueChanged(New EventArgs)
oldValue = Value
End If
End Sub
Protected Overrides Sub OnMouseDown(e As MouseEventArgs)
If e.X < channel.Left Then
Value = Minimum
ElseIf e.X > channel.Right Then
Value = Maximum
Else
Dim ticWidth As Double = channel.Width / (Maximum - Minimum)
Value = CInt((e.X - channel.Left) / ticWidth) + Minimum
End If
End Sub
Protected Overrides Sub OnMouseUp(e As MouseEventArgs)
TrackBarValueCheck()
End Sub
Protected Overrides Sub OnKeyUp(e As KeyEventArgs)
TrackBarValueCheck()
End Sub
Protected Overrides Sub OnMouseMove(e As MouseEventArgs)
TrackBarValueCheck()
End Sub
Protected Overrides Sub OnValueChanged(e As EventArgs)
End Sub
End Class
呼び出し側では
Private Sub TrackBarEx1_ValueChanged(sender As Object, e As EventArgs) Handles TrackBarEx1.ValueChanged
Debug.WriteLine($"ValueChanged: Value=【{TrackBarEx1.Value}】")
' 計算処理
End Sub
</pre></pre>