This repository has been archived by the owner on Sep 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ButtonPanelHelper.vb
203 lines (175 loc) · 5.9 KB
/
ButtonPanelHelper.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraEditors.Drawing
Imports DevExpress.XtraEditors.Controls
Imports DevExpress.Utils
Imports DevExpress.XtraGrid.Views.Base
Imports DevExpress.Utils.Drawing
Imports DevExpress.XtraGrid.Views.Grid.ViewInfo
Namespace WindowsApplication1
Public Class ButtonPanelHelper
Private _PanelHeight As Integer
Public Property PanelHeight() As Integer
Get
Return _PanelHeight
End Get
Set(ByVal value As Integer)
_PanelHeight = value
End Set
End Property
Private _ButtonWidth As Integer
Public Property ButtonWidth() As Integer
Get
Return _ButtonWidth
End Get
Set(ByVal value As Integer)
_ButtonWidth = value
End Set
End Property
Private _Buttons As EditorButtonCollection
Public Property Buttons() As EditorButtonCollection
Get
Return _Buttons
End Get
Set(ByVal value As EditorButtonCollection)
_Buttons = value
End Set
End Property
Private _PanelAppearance As AppearanceObject
Public Property PanelAppearance() As AppearanceObject
Get
Return _PanelAppearance
End Get
Set(ByVal value As AppearanceObject)
_PanelAppearance = value
End Set
End Property
#Region "Fields"
Private linkedView As GridView
Private offset As Integer = 5
Private previewBounds As Rectangle
Private hotTrackedIndex As Integer = -1
#End Region
#Region "Methods"
Public Sub New(ByVal view As GridView)
Me.linkedView = view
Buttons = New EditorButtonCollection()
InitProperties()
SubscribeEvents()
End Sub
Private Sub InitProperties()
PanelHeight = 40
ButtonWidth = 40
PanelAppearance = New AppearanceObject()
PanelAppearance.BackColor = Color.White
PanelAppearance.BackColor2 = Color.Gray
PanelAppearance.GradientMode = System.Drawing.Drawing2D.LinearGradientMode.Vertical
Dim button As New EditorButton(ButtonPredefines.Glyph)
button.Caption = "Test1"
Buttons.Add(button)
button = New EditorButton(ButtonPredefines.Glyph)
button.Caption = "Test2"
Buttons.Add(button)
button = New EditorButton(ButtonPredefines.Glyph)
button.Caption = "Test3"
Buttons.Add(button)
linkedView.OptionsView.ShowPreview = True
End Sub
Private Sub SubscribeEvents()
AddHandler linkedView.MeasurePreviewHeight, AddressOf linkedView_MeasurePreviewHeight
AddHandler linkedView.CustomDrawRowPreview, AddressOf linkedView_CustomDrawRowPreview
AddHandler linkedView.FocusedRowChanged, AddressOf linkedView_FocusedRowChanged
AddHandler linkedView.MouseMove, AddressOf linkedView_MouseMove
AddHandler linkedView.MouseDown, AddressOf linkedView_MouseDown
End Sub
Private Sub linkedView_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
If hotTrackedIndex > -1 Then
MessageBox.Show(Buttons(hotTrackedIndex).Caption)
End If
End Sub
Private Sub linkedView_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
If hotTrackedIndex <> GetHotButton() Then
InvalidateButtons()
End If
End Sub
Private Sub InvalidateButtons()
For i As Integer = 0 To Buttons.Count - 1
linkedView.InvalidateRect(GetButtonRect(previewBounds, i))
Next i
End Sub
Private Function GetHotButton() As Integer
Dim p As Point = GetMousePosition()
Dim result As Rectangle = Rectangle.Empty
For i As Integer = 0 To Buttons.Count - 1
result = GetButtonRect(previewBounds, i)
If result.Contains(p) Then
hotTrackedIndex = i
Return hotTrackedIndex
End If
Next i
hotTrackedIndex = -1
Return hotTrackedIndex
End Function
Private Sub linkedView_FocusedRowChanged(ByVal sender As Object, ByVal e As FocusedRowChangedEventArgs)
linkedView.LayoutChanged()
End Sub
Private Sub linkedView_CustomDrawRowPreview(ByVal sender As Object, ByVal e As RowObjectCustomDrawEventArgs)
previewBounds = e.Bounds
PanelAppearance.FillRectangle(e.Cache, e.Bounds)
DrawButtons(e)
e.Handled = True
End Sub
Private Sub DrawButtons(ByVal e As RowObjectCustomDrawEventArgs)
For Each button As EditorButton In Buttons
DrawButton(button, e)
Next button
End Sub
Private Function GetMousePosition() As Point
Return linkedView.GridControl.PointToClient(Control.MousePosition)
End Function
Private Function GetHitInfo() As GridHitInfo
Return linkedView.CalcHitInfo(GetMousePosition())
End Function
Private Sub DrawButton(ByVal button As EditorButton, ByVal e As RowObjectCustomDrawEventArgs)
Dim buttonRect As Rectangle = GetButtonRect(e.Bounds, button.Index)
Dim painter As New SkinEditorButtonPainter(linkedView.GridControl.LookAndFeel.ActiveLookAndFeel)
Dim args As New EditorButtonObjectInfoArgs(e.Cache, button, e.Appearance)
args.Bounds = buttonRect
If button.Index = hotTrackedIndex Then
args.State = ObjectState.Hot
End If
painter.DrawObject(args)
End Sub
Private Function GetPanelHeight(ByVal rowHandle As Integer) As Integer
If rowHandle = linkedView.FocusedRowHandle Then
Return PanelHeight
Else
Return 0
End If
End Function
Private Function GetButtonSize() As Size
Return New Size(ButtonWidth, PanelHeight - 2 * offset)
End Function
Private Function GetButtonRect(ByVal clientRect As Rectangle, ByVal buttonIndex As Integer) As Rectangle
Dim buttonSize As Size = GetButtonSize()
Dim rect As New Rectangle(New Point(0, 0), buttonSize)
Dim left As Integer = clientRect.Width - (buttonSize.Width + offset) * (buttonIndex + 1) + offset
Dim top As Integer = clientRect.Top + offset
rect.Offset(left, top)
Return rect
End Function
#End Region
#Region "Events"
Private Sub linkedView_MeasurePreviewHeight(ByVal sender As Object, ByVal e As RowHeightEventArgs)
e.RowHeight = GetPanelHeight(e.RowHandle)
End Sub
#End Region
End Class
End Namespace