This repository has been archived by the owner on Sep 11, 2023. It is now read-only.
/
CustomImageEdit.vb
335 lines (293 loc) · 10.1 KB
/
CustomImageEdit.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports DevExpress.XtraEditors
Imports DevExpress.XtraEditors.Repository
Imports DevExpress.XtraEditors.Registrator
Imports DevExpress.XtraEditors.ViewInfo
Imports DevExpress.XtraEditors.Drawing
Imports DevExpress.XtraEditors.Controls
Imports DevExpress.XtraEditors.Popup
Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Reflection
Namespace MyCustomEdit
<UserRepositoryItem("RegisterCustomImageEdit")> _
Public Class RepositoryItemCustomImageEdit
Inherits RepositoryItemPopupBase
Protected loadButtonIndex_Renamed As Integer
Private openFile As OpenFileDialog
Public Sub New()
MyBase.New()
ActionButtonIndex = 1
loadButtonIndex_Renamed = 0
End Sub
Public Overridable ReadOnly Property LoadButtonIndex() As Integer
Get
Return loadButtonIndex_Renamed
End Get
End Property
Public Overrides Sub CreateDefaultButton()
Dim btn As New EditorButton()
btn.IsDefaultButton = True
btn.Kind = ButtonPredefines.Plus
Buttons.Add(btn)
MyBase.CreateDefaultButton()
End Sub
Public Overridable Sub LoadFile()
If openFile Is Nothing Then
openFile = New OpenFileDialog()
End If
If openFile.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
OwnerEdit.EditValue = openFile.FileName
End If
End Sub
Shared Sub New()
RegisterCustomImageEdit()
End Sub
Public Const CustomImageEditName As String = "CustomImageEdit"
Public Overrides ReadOnly Property EditorTypeName() As String
Get
Return CustomImageEditName
End Get
End Property
Public Shared Sub RegisterCustomImageEdit()
EditorRegistrationInfo.Default.Editors.Add(New EditorClassInfo(CustomImageEditName, GetType(CustomImageEdit), GetType(RepositoryItemCustomImageEdit), GetType(PopupBaseEditViewInfo), New ButtonEditPainter(), True))
End Sub
End Class
Public Class CustomImageEdit
Inherits PopupBaseEdit
Public Sub New()
MyBase.New()
End Sub
Shared Sub New()
RepositoryItemCustomImageEdit.RegisterCustomImageEdit()
End Sub
Public Overrides ReadOnly Property EditorTypeName() As String
Get
Return RepositoryItemCustomImageEdit.CustomImageEditName
End Get
End Property
<DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
Public Shadows ReadOnly Property Properties() As RepositoryItemCustomImageEdit
Get
Return TryCast(MyBase.Properties, RepositoryItemCustomImageEdit)
End Get
End Property
Protected Overrides Function CreatePopupForm() As DevExpress.XtraEditors.Popup.PopupBaseForm
Return New CustomPopupBaseSizeableForm(Me)
End Function
Protected Overrides Sub OnPressButton(ByVal buttonInfo As EditorButtonObjectInfoArgs)
If IsLoadButton(buttonInfo) Then
Properties.LoadFile()
End If
MyBase.OnPressButton(buttonInfo)
End Sub
Protected Overridable Function IsLoadButton(ByVal buttonInfo As EditorButtonObjectInfoArgs) As Boolean
Return Properties.LoadButtonIndex >= 0 AndAlso Properties.Buttons.IndexOf(buttonInfo.Button) = Properties.LoadButtonIndex
End Function
End Class
Public Class CustomPopupBaseSizeableForm
Inherits PopupBaseSizeableForm
Public myImage As Image
Private openButton, okButton, cancelButton, clearButton As SimpleButton
Private openFile As OpenFileDialog
Private resultFileName As String
Private formContentSize As Size
Public Sub New(ByVal ownerEdit As PopupBaseEdit)
MyBase.New(ownerEdit)
CreateButtons()
openFile = New OpenFileDialog()
formContentSize = New Size(200, 200)
End Sub
Protected Overridable Sub CreateButtons()
okButton = New SimpleButton()
okButton.Text = "Ok"
okButton.AllowFocus = False
AddHandler okButton.Click, AddressOf okButton_Click
Controls.Add(okButton)
cancelButton = New SimpleButton()
cancelButton.Text = "Cancel"
cancelButton.AllowFocus = False
AddHandler cancelButton.Click, AddressOf cancelButton_Click
Controls.Add(cancelButton)
openButton = New SimpleButton()
openButton.Text = "Open"
openButton.AllowFocus = False
AddHandler openButton.Click, AddressOf openButton_Click
Controls.Add(openButton)
clearButton = New SimpleButton()
clearButton.Text = "Clear"
clearButton.AllowFocus = False
AddHandler clearButton.Click, AddressOf clearButton_Click
Controls.Add(clearButton)
End Sub
Protected Overrides ReadOnly Property MinFormSize() As Size
Get
Dim newSize As Size = MyBase.MinFormSize
newSize.Width = ViewInfo.ButtonSize.Width * 5
Return newSize
End Get
End Property
Protected Overridable Sub ChangeButtonsPositions()
okButton.Bounds = ViewInfo.OkButtonRect
cancelButton.Bounds = ViewInfo.CancelButtonRect
openButton.Bounds = ViewInfo.OpenButtonRect
clearButton.Bounds = ViewInfo.ClearButtonRect
End Sub
Private Sub okButton_Click(ByVal sender As Object, ByVal e As EventArgs)
ClosePopup(PopupCloseMode.Normal)
End Sub
Private Sub cancelButton_Click(ByVal sender As Object, ByVal e As EventArgs)
ClosePopup(PopupCloseMode.Cancel)
End Sub
Private Sub clearButton_Click(ByVal sender As Object, ByVal e As EventArgs)
myImage = Nothing
openFile.FileName = ""
resultFileName = ""
LayoutChanged()
End Sub
Private Sub openButton_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim res As System.Windows.Forms.DialogResult = openFile.ShowDialog()
OwnerEdit.ShowPopup()
If res = System.Windows.Forms.DialogResult.OK Then
LoadImage(openFile.FileName)
Invalidate()
End If
End Sub
Protected Overrides Sub LayoutChanged()
MyBase.LayoutChanged()
ChangeButtonsPositions()
End Sub
Public Overridable Function LoadImage(ByVal fileName As String) As Boolean
If fileName IsNot Nothing AndAlso fileName <> String.Empty Then
Try
myImage = Image.FromFile(fileName)
Catch
resultFileName = String.Empty
ClearImage()
Return False
End Try
Me.resultFileName = fileName
Return True
End If
resultFileName = String.Empty
ClearImage()
Return False
End Function
Public Sub ClearImage()
If myImage IsNot Nothing Then
myImage.Dispose()
myImage = Nothing
End If
End Sub
Public Overrides Overloads Function CalcFormSize(ByVal contentSize As Size) As Size
Return MyBase.CalcFormSize(formContentSize)
End Function
Public Overrides ReadOnly Property ResultValue() As Object
Get
Return resultFileName
End Get
End Property
Protected Overrides Overloads Sub ClosePopup(ByVal closeMode As PopupCloseMode)
ClearImage()
MyBase.ClosePopup(closeMode)
resultFileName = ""
End Sub
Public Overrides Sub ShowPopupForm()
Dim fileName As String = TryCast(OwnerEdit.EditValue, String)
LoadImage(fileName)
MyBase.ShowPopupForm()
End Sub
Protected Overrides Function CreatePainter() As PopupBaseFormPainter
Return New CustomPopupBaseFormPainter()
End Function
Protected Overrides Function CreateViewInfo() As PopupBaseFormViewInfo
Return New CustomPopupBaseSizeableFormViewInfo(Me)
End Function
Protected Shadows ReadOnly Property ViewInfo() As CustomPopupBaseSizeableFormViewInfo
Get
Return TryCast(MyBase.ViewInfo, CustomPopupBaseSizeableFormViewInfo)
End Get
End Property
Protected Overrides Sub OnSizeChanged(ByVal e As EventArgs)
formContentSize = ViewInfo.ContentRect.Size
MyBase.OnSizeChanged(e)
End Sub
End Class
Public Class CustomPopupBaseFormPainter
Inherits PopupBaseSizeableFormPainter
Public Sub New()
MyBase.New()
End Sub
Public Overrides Sub Draw(ByVal info As PopupFormGraphicsInfoArgs)
MyBase.Draw(info)
DrawImage(info)
End Sub
Private Sub DrawImage(ByVal info As PopupFormGraphicsInfoArgs)
Dim vi As CustomPopupBaseSizeableFormViewInfo = TryCast(info.ViewInfo, CustomPopupBaseSizeableFormViewInfo)
Dim form As CustomPopupBaseSizeableForm = TryCast(vi.Form, CustomPopupBaseSizeableForm)
If form.myImage IsNot Nothing Then
info.Graphics.DrawImage(form.myImage, vi.ImageRect)
End If
End Sub
End Class
Public Class CustomPopupBaseSizeableFormViewInfo
Inherits PopupBaseSizeableFormViewInfo
Private okButtonRect_Renamed, openButtonRect_Renamed, clearButtonRect_Renamed, cancelButtonRect_Renamed, imageRect_Renamed As Rectangle
Private Const borderWidth As Integer = 2, buttonWidth As Integer = 40, buttonHeight As Integer = 18
Private buttonSize_Renamed As Size
Public Sub New(ByVal form As PopupBaseForm)
MyBase.New(form)
buttonSize_Renamed = New Size(buttonWidth, buttonHeight)
imageRect_Renamed = Rectangle.Empty
okButtonRect_Renamed = Rectangle.Empty
openButtonRect_Renamed = Rectangle.Empty
clearButtonRect_Renamed = Rectangle.Empty
cancelButtonRect_Renamed = Rectangle.Empty
End Sub
Public Overridable ReadOnly Property OkButtonRect() As Rectangle
Get
Return okButtonRect_Renamed
End Get
End Property
Public Overridable ReadOnly Property OpenButtonRect() As Rectangle
Get
Return openButtonRect_Renamed
End Get
End Property
Public Overridable ReadOnly Property ClearButtonRect() As Rectangle
Get
Return clearButtonRect_Renamed
End Get
End Property
Public Overridable ReadOnly Property CancelButtonRect() As Rectangle
Get
Return cancelButtonRect_Renamed
End Get
End Property
Public Overridable ReadOnly Property ImageRect() As Rectangle
Get
Return imageRect_Renamed
End Get
End Property
Public Overridable ReadOnly Property ButtonSize() As Size
Get
Return buttonSize_Renamed
End Get
End Property
Protected Overrides Sub CalcRects()
MyBase.CalcRects()
imageRect_Renamed = ContentRect
okButtonRect_Renamed = New Rectangle(imageRect_Renamed.Left + borderWidth, imageRect_Renamed.Bottom + borderWidth, ButtonSize.Width, ButtonSize.Height)
cancelButtonRect_Renamed = okButtonRect_Renamed
cancelButtonRect_Renamed.Offset(borderWidth * 2 + buttonSize_Renamed.Width, 0)
openButtonRect_Renamed = cancelButtonRect_Renamed
openButtonRect_Renamed.Offset(borderWidth * 6 + buttonSize_Renamed.Width, 0)
clearButtonRect_Renamed = openButtonRect_Renamed
clearButtonRect_Renamed.Offset(borderWidth * 2 + buttonSize_Renamed.Width, 0)
End Sub
End Class
End Namespace