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 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
| Option Explicit
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, _
ByVal hObject As Long) As Long
Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, _
ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, _
ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, _
ByVal dwRop As Long) As Long
Private Declare Function StretchBlt Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, _
ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, _
ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, _
ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long
Private Declare Function TransparentBlt Lib "msimg32.dll" (ByVal hdc As Long, _
ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, _
ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, _
ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal crTransparent As Long) As Boolean
Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function SetTextColor Lib "gdi32" (ByVal hdc As Long, _
ByVal crColor As Long) As Long
Private Declare Function CopyImage Lib "user32" (ByVal handle As Long, ByVal imageType As Long, _
ByVal newWidth As Long, ByVal newHeight As Long, ByVal lFlags As Long) As Long
Private Declare Function DrawTextEx Lib "user32" Alias "DrawTextExA" (ByVal hdc As Long, _
ByVal lpsz As String, ByVal n As Long, lpRect As RECT, ByVal un As Long, _
ByVal lpDrawTextParams As Any) As Long
Private Declare Function SetRect Lib "user32" (lpRect As RECT, ByVal X1 As Long, _
ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Const DT_RIGHT As Long = &H2
Private Const DT_LEFT As Long = &H0
Private Const DT_CENTER As Long = &H1
Private Const DT_WORDBREAK = &H10
Private Const DT_VCENTER As Long = &H4
Private Const IMAGE_BITMAP = 0
Private Const LR_LOADFROMFILE = &H10
Private Const LR_COPYRETURNORG = &H4
Private Const SRCAND = &H8800C6
Private Const SRCPAINT = &HEE0086
Private Enum eStateConstant
sUp = 0
sDefault = 1
sDown = 2
End Enum
Enum AlignmentConstants
[Left Justify] = 0
[Right Justify] = 1
[Center] = 2
End Enum
Enum StyleConstants
Normal = 0
Graphical = 1
End Enum
'Déclarations d'événements:
Event Click()
Event DblClick()
Event KeyDown(KeyCode As Integer, Shift As Integer)
Event KeyPress(KeyAscii As Integer)
Event KeyUp(KeyCode As Integer, Shift As Integer)
Event MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
Event MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
Event MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
Dim m_State As eStateConstant
Dim m_Alignment As AlignmentConstants
Dim m_Caption As String
Dim m_Dx As Long
Dim m_Dy As Long
Dim m_Enabled As Boolean
Dim m_ForeColor As OLE_COLOR
Dim m_ImgWidth As Long
Dim m_ImgHeight As Long
Dim m_SrcHeight As Long
Dim m_SrcWidth As Long
Dim m_MaskColor As OLE_COLOR
Dim m_ImgLeft As Long
Dim m_ImgTop As Long
Dim m_Picture As StdPicture
Dim m_PictureDown As StdPicture
Dim m_PictureDisabled As StdPicture
Dim m_Style As StyleConstants
Dim m_ImgButton As StdPicture
Dim m_Top As Integer
Dim hbm As Long, hbm2 As Long, hbm3 As Long, hbm4 As Long
Dim Tmp_Dc As Long, Tmp_Dc2 As Long, Tmp_Dc3 As Long, Tmp_Dc4 As Long
Public Property Get Alignment() As AlignmentConstants
Alignment = m_Alignment
End Property
Public Property Let Alignment(ByVal New_Alignment As AlignmentConstants)
m_Alignment = New_Alignment
DrawControl
PropertyChanged "Alignment"
End Property
Public Property Get Caption() As String
Caption = m_Caption
End Property
Public Property Let Caption(ByVal New_Caption As String)
m_Caption = New_Caption
DrawControl
PropertyChanged "Caption"
End Property
Public Property Get Dx() As Long
Dx = m_Dx
End Property
Public Property Let Dx(ByVal New_Dx As Long)
m_Dx = New_Dx
DrawControl
PropertyChanged "Dx"
End Property
Public Property Get Dy() As Long
Dy = m_Dy
End Property
Public Property Let Dy(ByVal New_Dy As Long)
m_Dy = New_Dy
DrawControl
PropertyChanged "Dy"
End Property
Public Property Get Enabled() As Boolean
Enabled = m_Enabled
End Property
Public Property Let Enabled(ByVal New_Enabled As Boolean)
m_Enabled = New_Enabled
If Not m_Enabled Then
UserControl.Extender.Default = False
End If
DrawControl
PropertyChanged "Enabled"
End Property
Public Property Get Font() As StdFont
Set Font = UserControl.Font
End Property
Public Property Set Font(ByVal New_Font As StdFont)
Set UserControl.Font = New_Font
DrawControl
End Property
Public Property Get ForeColor() As OLE_COLOR
ForeColor = m_ForeColor
End Property
Public Property Let ForeColor(ByVal New_ForeColor As OLE_COLOR)
m_ForeColor = New_ForeColor
DrawControl
PropertyChanged "ForeColor"
End Property
Public Property Get ImgHeight() As Long
ImgHeight = m_ImgHeight
End Property
Public Property Let ImgHeight(ByVal New_ImgHeight As Long)
m_ImgHeight = New_ImgHeight
DrawControl
PropertyChanged "ImgHeight"
End Property
Public Property Get ImgLeft() As Long
ImgLeft = m_ImgLeft
End Property
Public Property Let ImgLeft(ByVal New_ImgLeft As Long)
m_ImgLeft = New_ImgLeft
DrawControl
PropertyChanged "ImgLeft"
End Property
Public Property Get ImgTop() As Long
ImgTop = m_ImgTop
End Property
Public Property Let ImgTop(ByVal New_ImgTop As Long)
m_ImgTop = New_ImgTop
DrawControl
PropertyChanged "ImgTop"
End Property
Public Property Get ImgWidth() As Long
ImgWidth = m_ImgWidth
End Property
Public Property Let ImgWidth(ByVal New_ImgWidth As Long)
m_ImgWidth = New_ImgWidth
DrawControl
PropertyChanged "ImgWidth"
End Property
Public Property Get MaskColor() As OLE_COLOR
MaskColor = m_MaskColor
End Property
Property Let MaskColor(ByVal New_MaskColor As OLE_COLOR)
m_MaskColor = New_MaskColor
DrawControl
PropertyChanged "MaskColor"
End Property
Public Property Get Picture() As StdPicture
Set Picture = m_Picture
End Property
Public Property Set Picture(New_Picture As StdPicture)
Set m_Picture = New_Picture
SetImage
DrawControl
PropertyChanged "Picture"
End Property
Public Property Get PictureDown() As StdPicture
Set PictureDown = m_PictureDown
End Property
Public Property Set PictureDown(New_PictureDown As StdPicture)
Set m_PictureDown = New_PictureDown
SetImage
DrawControl
PropertyChanged "PictureDown"
End Property
Public Property Get PictureDisabled() As StdPicture
Set PictureDisabled = m_PictureDisabled
End Property
Public Property Set PictureDisabled(New_PictureDisabled As StdPicture)
Set m_PictureDisabled = New_PictureDisabled
SetImage
DrawControl
PropertyChanged "PictureDisabled"
End Property
Public Property Get Style() As StyleConstants
Style = m_Style
End Property
Public Property Let Style(ByVal New_Style As StyleConstants)
m_Style = New_Style
DrawControl
PropertyChanged "Style"
End Property
Public Property Get Top() As Integer
Top = UserControl.Extender.Top
End Property
Public Property Let Top(ByVal New_Top As Integer)
m_Top = New_Top
UserControl.Extender.Top = m_Top
DrawControl
PropertyChanged "Top"
End Property
Private Sub DrawControl()
SelectObject Tmp_Dc, hbm
Dim test As String
test = m_State
If UserControl.Extender.Default And m_State <> sDown Then
m_State = sDefault
ElseIf m_State <> sDown Then
m_State = sUp
End If
If Not m_Enabled And m_State = sDown Then m_State = sUp
test = test & ":" & m_State
'MsgBox test
'dessine le controle morceau par morceau
'HG
BitBlt UserControl.hdc, 0, 0, 4, 4, Tmp_Dc, 0, m_State * 20, vbSrcCopy
'H
StretchBlt UserControl.hdc, 4, 0, ScaleWidth - 8, 4, Tmp_Dc, 4, m_State * 20, 79, 4, vbSrcCopy
'HD
BitBlt UserControl.hdc, ScaleWidth - 4, 0, 4, 4, Tmp_Dc, 83, m_State * 20, vbSrcCopy
'D
StretchBlt UserControl.hdc, ScaleWidth - 4, 4, 4, ScaleHeight - 8, Tmp_Dc, 83, (m_State * 20) + 4, 4, 12, vbSrcCopy
'BD
BitBlt UserControl.hdc, ScaleWidth - 4, ScaleHeight - 4, 4, 4, Tmp_Dc, 83, (m_State * 20) + 16, vbSrcCopy
'B
StretchBlt UserControl.hdc, 4, ScaleHeight - 4, ScaleWidth - 8, 4, Tmp_Dc, 4, (m_State * 20) + 16, 79, 4, vbSrcCopy
'BG
BitBlt UserControl.hdc, 0, ScaleHeight - 4, 4, 4, Tmp_Dc, 0, (m_State * 20) + 16, vbSrcCopy
'G
StretchBlt UserControl.hdc, 0, 4, 4, ScaleHeight - 8, Tmp_Dc, 0, (m_State * 20) + 4, 4, 12, vbSrcCopy
'Centre
StretchBlt UserControl.hdc, 4, 4, ScaleWidth - 8, ScaleHeight - 8, Tmp_Dc, 4, (m_State * 20) + 4, 79, 12, vbSrcCopy
DrawText
DrawImage
UserControl.Refresh
End Sub
Private Sub DrawImage()
If m_Style = Graphical Then
SetImage
If m_Enabled Then
If m_State = sDefault Or m_State = sUp Then
SelectObject Tmp_Dc2, hbm2
TransparentBlt UserControl.hdc, m_ImgLeft, m_ImgTop, m_ImgWidth, _
m_ImgHeight, Tmp_Dc2, 0, 0, m_SrcWidth, m_SrcHeight, m_MaskColor
Else
SelectObject Tmp_Dc3, hbm3
TransparentBlt UserControl.hdc, m_ImgLeft, m_ImgTop + 1, m_ImgWidth, _
m_ImgHeight, Tmp_Dc3, 0, 0, m_SrcWidth, m_SrcHeight, m_MaskColor
End If
Else
SelectObject Tmp_Dc4, hbm4
TransparentBlt UserControl.hdc, m_ImgLeft, m_ImgTop, m_ImgWidth, _
m_ImgHeight, Tmp_Dc4, 0, 0, m_SrcWidth, m_SrcHeight, m_MaskColor
End If
End If
End Sub
Private Sub DrawText()
'inscription Caption
Dim x As Long, y As Long
Dim lblW As Long, lblH As Long
Dim lblAlignment As Long
'dimensions du caption
lblW = TextWidth(m_Caption)
'lblH = TextHeight(m_Caption)
Dim tmph As Long
tmph = Round(Font.Size / 4)
lblH = Font.Size + tmph
'alignement
Select Case m_Alignment
Case Center
lblAlignment = DT_CENTER
Case [Left Justify]
lblAlignment = DT_LEFT
Case [Right Justify]
lblAlignment = DT_RIGHT
End Select
Dim r As RECT
Dim tmp As Long
tmp = (lblW \ (ScaleWidth - 4)) + 1
y = (((ScaleHeight) - (tmp * lblH) - ((tmp - 1) * (Font.Size / 4))) / 2) + Dy
y = y - (tmp * (Font.Size / 4))
x = 2 + m_Dx
SetRect r, x, y, ScaleWidth - 4, ScaleHeight
If m_Enabled Then
SetTextColor UserControl.hdc, m_ForeColor
If m_State = sDown Then
SetRect r, x, y + 1, ScaleWidth - 4, ScaleHeight
'si bouton enfoncé-> on baisse le caption d'1px
'TextOut UserControl.hDC, X, Y + 1, m_Caption, Len(m_Caption)
DrawTextEx UserControl.hdc, m_Caption, Len(m_Caption), r, DT_WORDBREAK Or lblAlignment Or DT_VCENTER, ByVal 0&
Else
'TextOut UserControl.hDC, X, Y, m_Caption, Len(m_Caption)
DrawTextEx UserControl.hdc, m_Caption, Len(m_Caption), r, DT_WORDBREAK Or lblAlignment Or DT_VCENTER, ByVal 0&
End If
Else
'effet 3D avec liseré de blanc
SetTextColor UserControl.hdc, vbWhite
'TextOut UserControl.hDC, X + 1, Y + 1, m_Caption, Len(m_Caption)
SetRect r, x + 1, y + 1, ScaleWidth - 4, ScaleHeight
DrawTextEx UserControl.hdc, m_Caption, Len(m_Caption), r, DT_WORDBREAK Or lblAlignment, ByVal 0&
SetTextColor UserControl.hdc, &H99A8AC
'TextOut UserControl.hDC, X, Y, m_Caption, Len(m_Caption)
SetRect r, x, y, ScaleWidth - 4, ScaleHeight
DrawTextEx UserControl.hdc, m_Caption, Len(m_Caption), r, DT_WORDBREAK Or lblAlignment, ByVal 0&
End If
End Sub
Private Sub SetImage()
'récupération des dimensions de l'image
If Not m_Picture Is Nothing Then
m_SrcHeight = UserControl.ScaleY(m_Picture.Height, vbHimetric, vbPixels)
m_SrcWidth = UserControl.ScaleX(m_Picture.Width, vbHimetric, vbPixels)
'initialisation des dimensions de sortie
If ImgHeight = 0 Then ImgHeight = m_SrcHeight
If ImgWidth = 0 Then ImgWidth = m_SrcWidth
'chargements
'img_up
hbm2 = CopyImage(m_Picture.handle, IMAGE_BITMAP, m_SrcWidth, m_SrcHeight, LR_COPYRETURNORG)
Tmp_Dc2 = CreateCompatibleDC(hdc)
If Not m_PictureDown Is Nothing Then
'img_down
hbm3 = CopyImage(m_PictureDown.handle, IMAGE_BITMAP, m_SrcWidth, m_SrcHeight, LR_COPYRETURNORG)
Tmp_Dc3 = CreateCompatibleDC(hdc)
End If
If Not m_PictureDisabled Is Nothing Then
'img_disabled
hbm4 = CopyImage(m_PictureDisabled.handle, IMAGE_BITMAP, m_SrcWidth, m_SrcHeight, LR_COPYRETURNORG)
Tmp_Dc4 = CreateCompatibleDC(hdc)
End If
End If
End Sub
Private Sub UserControl_AccessKeyPress(KeyAscii As Integer)
'afin d'enabler la propriété Default
If m_Enabled Then RaiseEvent Click
End Sub
Private Sub UserControl_AmbientChanged(PropertyName As String)
If UserControl.Extender.Default = True Then
UserControl.Extender.TabIndex = 0
End If
DrawControl 'pour le cas où la propriété défault bouge
End Sub
Private Sub UserControl_Click()
If m_Enabled Then RaiseEvent Click
End Sub
Private Sub UserControl_DblClick()
If m_Enabled Then RaiseEvent DblClick
End Sub
Private Sub UserControl_Initialize()
UserControl.ScaleMode = vbPixels
'chargement du bitmap
Set m_ImgButton = LoadResPicture(100, vbResBitmap)
hbm = CopyImage(m_ImgButton.handle, IMAGE_BITMAP, 87, 60, LR_COPYRETURNORG)
Tmp_Dc = CreateCompatibleDC(0)
SelectObject Tmp_Dc, hbm
End Sub
Private Sub UserControl_InitProperties()
m_Alignment = Center
m_State = sUp
m_Caption = UserControl.Extender.Name
Set Me.Font = Ambient.Font
m_ForeColor = vbBlack
m_Enabled = True
m_MaskColor = vbWhite
SetImage
End Sub
Private Sub UserControl_KeyDown(KeyCode As Integer, Shift As Integer)
If m_Enabled Then RaiseEvent KeyDown(KeyCode, Shift)
End Sub
Private Sub UserControl_KeyPress(KeyAscii As Integer)
If m_Enabled Then RaiseEvent KeyPress(KeyAscii)
End Sub
Private Sub UserControl_KeyUp(KeyCode As Integer, Shift As Integer)
If m_Enabled Then RaiseEvent KeyUp(KeyCode, Shift)
End Sub
Private Sub UserControl_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
If m_Enabled Then
RaiseEvent MouseDown(Button, Shift, x, y)
m_State = sDown
End If
DrawControl
End Sub
Private Sub UserControl_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
If m_Enabled Then RaiseEvent MouseMove(Button, Shift, x, y)
End Sub
Private Sub UserControl_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
If m_Enabled Then
RaiseEvent MouseUp(Button, Shift, x, y)
If UserControl.Extender.Default Then
m_State = sDefault
Else
m_State = sUp
End If
End If
DrawControl
End Sub
Private Sub UserControl_Paint()
DrawControl
End Sub
Private Sub UserControl_Show()
DrawControl
End Sub
Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
m_Alignment = PropBag.ReadProperty("Alignment", Center)
m_Caption = PropBag.ReadProperty("Caption", UserControl.Extender.Name)
m_Dx = PropBag.ReadProperty("Dx", 0)
m_Dy = PropBag.ReadProperty("Dy", 0)
m_Enabled = PropBag.ReadProperty("Enabled", True)
Set Font = PropBag.ReadProperty("Font", Ambient.Font)
m_ForeColor = PropBag.ReadProperty("ForeColor", vbBlack)
m_ImgHeight = PropBag.ReadProperty("ImgHeight", 0)
m_ImgLeft = PropBag.ReadProperty("ImgLeft", 5)
m_ImgTop = PropBag.ReadProperty("ImgTop", 5)
m_ImgWidth = PropBag.ReadProperty("ImgWidth", 0)
m_MaskColor = PropBag.ReadProperty("MaskColor", vbWhite)
Set m_Picture = PropBag.ReadProperty("Picture", Nothing)
Set m_PictureDown = PropBag.ReadProperty("PictureDown", Nothing)
Set m_PictureDisabled = PropBag.ReadProperty("PictureDisabled", Nothing)
m_Style = PropBag.ReadProperty("Style", Normal)
m_Top = PropBag.ReadProperty("Top", UserControl.Extender.Top)
End Sub
Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
Call PropBag.WriteProperty("Alignment", m_Alignment, Center)
Call PropBag.WriteProperty("Caption", m_Caption, UserControl.Extender.Name)
Call PropBag.WriteProperty("Dx", m_Dx, 0)
Call PropBag.WriteProperty("Dy", m_Dy, 0)
Call PropBag.WriteProperty("Enabled", m_Enabled, True)
Call PropBag.WriteProperty("Font", Font, Ambient.Font)
Call PropBag.WriteProperty("ForeColor", m_ForeColor, vbBlack)
Call PropBag.WriteProperty("ImgHeight", m_ImgHeight, 0)
Call PropBag.WriteProperty("ImgLeft", m_ImgLeft, 5)
Call PropBag.WriteProperty("ImgTop", m_ImgTop, 5)
Call PropBag.WriteProperty("ImgWidth", m_ImgWidth, 0)
Call PropBag.WriteProperty("MaskColor", m_MaskColor, vbWhite)
Call PropBag.WriteProperty("Picture", m_Picture, Nothing)
Call PropBag.WriteProperty("PictureDown", m_PictureDown, Nothing)
Call PropBag.WriteProperty("PictureDisabled", m_PictureDisabled, Nothing)
Call PropBag.WriteProperty("Style", m_Style, Normal)
Call PropBag.WriteProperty("Top", m_Top, UserControl.Extender.Top)
End Sub
Private Sub UserControl_Resize()
If Width < 200 Then Width = 200
If Height < 200 Then Height = 200
DrawControl
End Sub
Private Sub UserControl_Terminate()
'nettoyage
DeleteObject hbm
DeleteDC Tmp_Dc
DeleteObject hbm2
DeleteDC Tmp_Dc2
DeleteObject hbm3
DeleteDC Tmp_Dc3
DeleteObject hbm4
DeleteDC Tmp_Dc4
Set m_Picture = Nothing
Set m_PictureDown = Nothing
Set m_PictureDisabled = Nothing
Set m_ImgButton = Nothing
End Sub |
Partager