@@ -32,27 +32,27 @@ Private Const MODULE_NAME As String = "AlphaBlendLabel"
32
32
' Public enums
33
33
'=========================================================================
34
34
35
- Public Enum UcsTextAlignEnum
36
- ucsBflHorLeft = 0
37
- ucsBflHorCenter = 1
38
- ucsBflHorRight = 2
39
- ucsBflVertTop = 0
40
- ucsBflVertCenter = 4
41
- ucsBflVertBottom = 8
42
- ucsBflCenter = ucsBflHorCenter Or ucsBflVertCenter
35
+ Public Enum UcsLabelTextAlignEnum
36
+ ucsLtaHorLeft = 0
37
+ ucsLtaHorCenter = 1
38
+ ucsLtaHorRight = 2
39
+ ucsLtaVertTop = 0
40
+ ucsLtaVertCenter = 4
41
+ ucsLtaVertBottom = 8
42
+ ucsLtaCenter = ucsLtaHorCenter Or ucsLtaVertCenter
43
43
End Enum
44
44
45
- Public Enum UcsTextFlagsEnum
46
- ucsBflNone = 0
47
- ucsBflDirectionRightToLeft = &H1 * 16
48
- ucsBflDirectionVertical = &H2 * 16
49
- ucsBflNoFitBlackBox = &H4 * 16
50
- ucsBflDisplayFormatControl = &H20 * 16
51
- ucsBflNoFontFallback = &H400 * 16
52
- ucsBflMeasureTrailingSpaces = &H800 & * 16
53
- ucsBflNoWrap = &H1000 & * 16
54
- ucsBflLineLimit = &H2000 & * 16
55
- ucsBflNoClip = &H4000 & * 16
45
+ Public Enum UcsLabelTextFlagsEnum
46
+ ucsLtfNone = 0
47
+ ucsLtfDirectionRightToLeft = &H1 * 16
48
+ ucsLtfDirectionVertical = &H2 * 16
49
+ ucsLtfNoFitBlackBox = &H4 * 16
50
+ ucsLtfDisplayFormatControl = &H20 * 16
51
+ ucsLtfNoFontFallback = &H400 * 16
52
+ ucsLtfMeasureTrailingSpaces = &H800 & * 16
53
+ ucsLtfNoWrap = &H1000 & * 16
54
+ ucsLtfLineLimit = &H2000 & * 16
55
+ ucsLtfNoClip = &H4000 & * 16
56
56
End Enum
57
57
58
58
'=========================================================================
@@ -79,8 +79,10 @@ Private Const AC_SRC_ALPHA As Long = 1
79
79
Private Const UnitPoint As Long = 3
80
80
'--- for GdipSetTextRenderingHint
81
81
Private Const TextRenderingHintAntiAlias As Long = 4
82
+ Private Const TextRenderingHintClearTypeGridFit As Long = 5
82
83
'--- for GdipSetSmoothingMode
83
84
Private Const SmoothingModeAntiAlias As Long = 4
85
+ Private Const DT_CALCRECT As Long = &H400
84
86
85
87
Private Declare Sub CopyMemory Lib "kernel32 " Alias "RtlMoveMemory " (lpDst As Any , lpSrc As Any , ByVal ByteLength As Long )
86
88
Private Declare Function CreateCompatibleDC Lib "gdi32 " (ByVal hDC As Long ) As Long
@@ -93,6 +95,7 @@ Private Declare Function GetModuleHandle Lib "kernel32" Alias "GetModuleHandleA"
93
95
Private Declare Function OleTranslateColor Lib "oleaut32 " (ByVal lOleColor As Long , ByVal lHPalette As Long , ByVal lColorRef As Long ) As Long
94
96
Private Declare Function GetDC Lib "user32 " (ByVal hWnd As Long ) As Long
95
97
Private Declare Function ReleaseDC Lib "user32 " (ByVal hWnd As Long , ByVal hDC As Long ) As Long
98
+ Private Declare Function DrawText Lib "user32 " Alias "DrawTextA " (ByVal hDC As Long , ByVal lpStr As String , ByVal nCount As Long , lpRect As Any , ByVal wFormat As Long ) As Long
96
99
'--- GDI+
97
100
Private Declare Function GdiplusStartup Lib "gdiplus " (hToken As Long , pInputBuf As Any , Optional ByVal pOutputBuf As Long = 0 ) As Long
98
101
Private Declare Function GdipCreateFontFamilyFromName Lib "gdiplus " (ByVal lNamePtr As Long , ByVal hFontCollection As Long , hFontFamily As Long ) As Long
@@ -175,7 +178,7 @@ Private Const DEF_SHADOWOFFSETX As Single = 1
175
178
Private Const DEF_SHADOWOFFSETY As Single = 1
176
179
Private Const DEF_SHADOWCOLOR As Long = vbBlack
177
180
Private Const DEF_SHADOWOPACITY As Single = 0
178
- Private Const DEF_TEXTALIGN As Long = ucsBflCenter
181
+ Private Const DEF_TEXTALIGN As Long = ucsLtaCenter
179
182
Private Const DEF_TEXTFLAGS As Long = 0
180
183
181
184
Private m_bAutoRedraw As Boolean
@@ -193,8 +196,8 @@ Private m_sngShadowOffsetX As Single
193
196
Private m_sngShadowOffsetY As Single
194
197
Private m_clrShadow As OLE_COLOR
195
198
Private m_sngShadowOpacity As Single
196
- Private m_eTextAlign As UcsTextAlignEnum
197
- Private m_eTextFlags As UcsTextFlagsEnum
199
+ Private m_eTextAlign As UcsLabelTextAlignEnum
200
+ Private m_eTextFlags As UcsLabelTextFlagsEnum
198
201
'--- run-time
199
202
Private m_bShown As Boolean
200
203
Private m_eContainerScaleMode As ScaleModeConstants
@@ -404,38 +407,123 @@ Property Let ShadowOpacity(ByVal sngValue As Single)
404
407
End If
405
408
End Property
406
409
407
- Property Get TextAlign() As UcsTextAlignEnum
410
+ Property Get TextAlign() As UcsLabelTextAlignEnum
408
411
TextAlign = m_eTextAlign
409
412
End Property
410
413
411
- Property Let TextAlign(ByVal eValue As UcsTextAlignEnum )
414
+ Property Let TextAlign(ByVal eValue As UcsLabelTextAlignEnum )
412
415
If m_eTextAlign <> eValue Then
413
416
m_eTextAlign = eValue
414
417
pvRefresh
415
418
PropertyChanged
416
419
End If
417
420
End Property
418
421
419
- Property Get TextFlags() As UcsTextFlagsEnum
422
+ Property Get TextFlags() As UcsLabelTextFlagsEnum
420
423
TextFlags = m_eTextFlags
421
424
End Property
422
425
423
- Property Let TextFlags(ByVal eValue As UcsTextFlagsEnum )
426
+ Property Let TextFlags(ByVal eValue As UcsLabelTextFlagsEnum )
424
427
If m_eTextFlags <> eValue Then
425
428
m_eTextFlags = eValue
429
+ If m_bAutoSize And TypeOf Extender Is VBControlExtender Then
430
+ pvSizeExtender Extender
431
+ End If
426
432
pvRefresh
427
433
PropertyChanged
428
434
End If
429
435
End Property
430
436
437
+ Property Get WordWrap() As Boolean
438
+ WordWrap = (m_eTextFlags And ucsLtfNoWrap) = 0
439
+ End Property
440
+
441
+ Property Let WordWrap(ByVal bValue As Boolean )
442
+ If bValue Then
443
+ TextFlags = m_eTextFlags And Not ucsLtfNoWrap
444
+ Else
445
+ TextFlags = m_eTextFlags Or ucsLtfNoWrap
446
+ End If
447
+ End Property
448
+
431
449
Property Get LastError() As String
432
450
LastError = m_sLastError
433
451
End Property
434
452
435
453
'=========================================================================
436
- ' Method
454
+ ' Methods
437
455
'=========================================================================
438
456
457
+ Public Function MeasureString (sCaption As String , sngWidth As Single , sngHeight As Single ) As Boolean
458
+ Dim hDC As Long
459
+ Dim hGraphics As Long
460
+ Dim uRect As RECTF
461
+ Dim uBounds As RECTF
462
+ Dim rcRect(0 To 3 ) As Long
463
+ Dim pFont As IFont
464
+ Dim hPrevFont As Long
465
+ Dim hStringFormat As Long
466
+
467
+ hDC = GetDC(ContainerHwnd)
468
+ If hDC = 0 Then
469
+ GoTo QH
470
+ End If
471
+ If m_hFont <> 0 Then
472
+ If GdipCreateFromHDC(hDC, hGraphics) <> 0 Then
473
+ GoTo QH
474
+ End If
475
+ If GdipSetSmoothingMode(hGraphics, SmoothingModeAntiAlias) <> 0 Then
476
+ GoTo QH
477
+ End If
478
+ If GdipSetTextRenderingHint(hGraphics, IIf (m_bAutoRedraw, TextRenderingHintAntiAlias, TextRenderingHintClearTypeGridFit)) <> 0 Then
479
+ GoTo QH
480
+ End If
481
+ If Not pvPrepareStringFormat(m_eTextAlign Or m_eTextFlags, hStringFormat) Then
482
+ GoTo QH
483
+ End If
484
+ uRect.Right = ScaleX(sngWidth, m_eContainerScaleMode, vbPixels)
485
+ uRect.Bottom = ScaleY(sngHeight, m_eContainerScaleMode, vbPixels)
486
+ If GdipMeasureString(hGraphics, StrPtr(sCaption), Len(sCaption), m_hFont, uRect, hStringFormat, uBounds, 0 , 0 ) <> 0 Then
487
+ GoTo QH
488
+ End If
489
+ '--- ceil
490
+ sngWidth = -Int(-uBounds.Right)
491
+ sngHeight = -Int(-uBounds.Bottom)
492
+ Else
493
+ Set pFont = m_oFont
494
+ hPrevFont = SelectObject(hDC, pFont.hFont)
495
+ rcRect(2 ) = ScaleX(sngWidth, m_eContainerScaleMode, vbPixels)
496
+ rcRect(3 ) = ScaleY(sngHeight, m_eContainerScaleMode, vbPixels)
497
+ Call DrawText (hDC, sCaption, Len(sCaption), rcRect(0 ), DT_CALCRECT)
498
+ Call SelectObject (hDC, hPrevFont)
499
+ sngWidth = rcRect(2 )
500
+ sngHeight = rcRect(3 )
501
+ End If
502
+ sngWidth = ScaleY(sngWidth, vbPixels, m_eContainerScaleMode)
503
+ sngHeight = ScaleY(sngHeight, vbPixels, m_eContainerScaleMode)
504
+ '--- success
505
+ MeasureString = True
506
+ QH:
507
+ If hStringFormat <> 0 Then
508
+ Call GdipDeleteStringFormat (hStringFormat)
509
+ hStringFormat = 0
510
+ End If
511
+ If hGraphics <> 0 Then
512
+ Call GdipDeleteGraphics (hGraphics)
513
+ hGraphics = 0
514
+ End If
515
+ If hDC <> 0 Then
516
+ Call ReleaseDC (ContainerHwnd, hDC)
517
+ hDC = 0
518
+ End If
519
+ End Function
520
+
521
+ Public Sub Refresh ()
522
+ UserControl.Refresh
523
+ End Sub
524
+
525
+ '= private ===============================================================
526
+
439
527
Private Function pvPaintControl (ByVal hDC As Long ) As Boolean
440
528
Const FUNC_NAME As String = "pvPaintControl"
441
529
Dim hGraphics As Long
@@ -448,14 +536,29 @@ Private Function pvPaintControl(ByVal hDC As Long) As Boolean
448
536
Dim sngTop As Single
449
537
Dim sngWidth As Single
450
538
Dim sngHeight As Single
539
+ Dim rcRect(0 To 3 ) As Long
540
+ Dim pFont As IFont
541
+ Dim hPrevFont As Long
451
542
452
543
On Error GoTo EH
544
+ If GetModuleHandle("gdiplus" ) = 0 Then
545
+ rcRect(2 ) = ScaleX(ScaleWidth, ScaleMode, vbPixels)
546
+ rcRect(3 ) = ScaleY(ScaleHeight, ScaleMode, vbPixels)
547
+ Set pFont = m_oFont
548
+ hPrevFont = SelectObject(hDC, pFont.hFont)
549
+ Call DrawText (hDC, m_sCaption, -1 , rcRect(0 ), 0 )
550
+ Call SelectObject (hDC, hPrevFont)
551
+ GoTo QH
552
+ End If
453
553
If GdipCreateFromHDC(hDC, hGraphics) <> 0 Then
454
554
GoTo QH
455
555
End If
456
556
If GdipSetSmoothingMode(hGraphics, SmoothingModeAntiAlias) <> 0 Then
457
557
GoTo QH
458
558
End If
559
+ If GdipSetTextRenderingHint(hGraphics, IIf (m_bAutoRedraw, TextRenderingHintAntiAlias, TextRenderingHintClearTypeGridFit)) <> 0 Then
560
+ GoTo QH
561
+ End If
459
562
hFont = m_hFont
460
563
sCaption = m_sCaption
461
564
sngWidth = ScaleWidth
@@ -479,9 +582,6 @@ Private Function pvPaintControl(ByVal hDC As Long) As Boolean
479
582
If GdipSetSolidFillColor(hBrush, pvTranslateColor(m_clrShadow, m_sngShadowOpacity)) <> 0 Then
480
583
GoTo QH
481
584
End If
482
- If GdipSetTextRenderingHint(hGraphics, TextRenderingHintAntiAlias) <> 0 Then
483
- GoTo QH
484
- End If
485
585
uRect.Left = uRect.Left + m_sngShadowOffsetX
486
586
uRect.Top = uRect.Top + m_sngShadowOffsetY
487
587
If GdipDrawString(hGraphics, StrPtr(sCaption), -1 , hFont, uRect, hStringFormat, hBrush) <> 0 Then
@@ -530,6 +630,9 @@ Private Function pvPrepareFont(oFont As StdFont, hFont As Long) As Boolean
530
630
Dim eStyle As FontStyle
531
631
532
632
On Error GoTo EH
633
+ If GetModuleHandle("gdiplus" ) = 0 Then
634
+ GoTo QH
635
+ End If
533
636
If oFont Is Nothing Then
534
637
GoTo QH
535
638
End If
@@ -642,38 +745,17 @@ Private Sub pvHandleMouseDown(Button As Integer, Shift As Integer, X As Single,
642
745
End Sub
643
746
644
747
Private Sub pvSizeExtender (oExt As VBControlExtender )
645
- Dim hDC As Long
646
- Dim hGraphics As Long
647
748
Dim sngWidth As Single
648
749
Dim sngHeight As Single
649
- Dim uBounds As RECTF
650
750
651
- If m_hFont = 0 Then
652
- GoTo QH
751
+ If WordWrap Then
752
+ sngWidth = oExt.Width
653
753
End If
654
- hDC = GetDC(ContainerHwnd)
655
- If hDC = 0 Then
656
- GoTo QH
657
- End If
658
- If GdipCreateFromHDC(hDC, hGraphics) <> 0 Then
659
- GoTo QH
660
- End If
661
- If GdipMeasureString(hGraphics, StrPtr(m_sCaption), -1 , m_hFont, uBounds, 0 , uBounds, 0 , 0 ) <> 0 Then
662
- GoTo QH
663
- End If
664
- '--- ceil
665
- sngWidth = -Int(-uBounds.Right)
666
- sngHeight = -Int(-uBounds.Bottom)
667
- oExt.Width = ScaleX(sngWidth, vbPixels, m_eContainerScaleMode)
668
- oExt.Height = ScaleY(sngHeight, vbPixels, m_eContainerScaleMode)
669
- QH:
670
- If hGraphics <> 0 Then
671
- Call GdipDeleteGraphics (hGraphics)
672
- hGraphics = 0
673
- End If
674
- If hDC <> 0 Then
675
- Call ReleaseDC (ContainerHwnd, hDC)
676
- hDC = 0
754
+ If MeasureString(m_sCaption, sngWidth, sngHeight) Then
755
+ If Not WordWrap Then
756
+ oExt.Width = sngWidth
757
+ End If
758
+ oExt.Height = sngHeight
677
759
End If
678
760
End Sub
679
761
@@ -892,9 +974,6 @@ Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
892
974
m_eTextFlags = .ReadProperty("TextFlags" , DEF_TEXTFLAGS)
893
975
End With
894
976
pvPrepareFont m_oFont, m_hFont
895
- If m_bAutoSize And TypeOf Extender Is VBControlExtender Then
896
- pvSizeExtender Extender
897
- End If
898
977
QH:
899
978
Exit Sub
900
979
EH:
@@ -931,11 +1010,11 @@ EH:
931
1010
Resume QH
932
1011
End Sub
933
1012
934
- Private Sub UserControl_AmbientChanged (PropertyName As String )
935
- If PropertyName = "ScaleUnits" Then
936
- m_eContainerScaleMode = ToScaleMode(Ambient.ScaleUnits)
937
- End If
938
- End Sub
1013
+ ' Private Sub UserControl_AmbientChanged(PropertyName As String)
1014
+ ' If PropertyName = "ScaleUnits" Then
1015
+ ' m_eContainerScaleMode = ToScaleMode(Ambient.ScaleUnits)
1016
+ ' End If
1017
+ ' End Sub
939
1018
940
1019
'=========================================================================
941
1020
' Base class events
@@ -946,7 +1025,9 @@ Private Sub UserControl_Initialize()
946
1025
947
1026
If GetModuleHandle("gdiplus" ) = 0 Then
948
1027
aInput(0 ) = 1
1028
+ On Error Resume Next
949
1029
Call GdiplusStartup (0 , aInput(0 ))
1030
+ On Error GoTo 0
950
1031
End If
951
1032
m_eContainerScaleMode = vbTwips
952
1033
End Sub
0 commit comments