-
Notifications
You must be signed in to change notification settings - Fork 1
/
LogFont.cls
230 lines (202 loc) · 6.25 KB
/
LogFont.cls
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
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "clsLogFont"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
DefInt A-Z
' Logical Font
Private Const LF_FACESIZE = 32
Private Const LF_FULLFACESIZE = 64
Private Const CLIP_DEFAULT_PRECIS = 0
Private Const CLIP_CHARACTER_PRECIS = 1
Private Const CLIP_STROKE_PRECIS = 2
Private Const CLIP_MASK = &HF
Private Const CLIP_LH_ANGLES = 16
Private Const CLIP_TT_ALWAYS = 32
Private Const CLIP_EMBEDDED = 128
Private Const DEFAULT_QUALITY = 0
Private Const DRAFT_QUALITY = 1
Private Const PROOF_QUALITY = 2
Private Const DEFAULT_PITCH = 0
Private Const FIXED_PITCH = 1
Private Const VARIABLE_PITCH = 2
Private Const ANSI_CHARSET = 0
Private Const DEFAULT_CHARSET = 1
Private Const SYMBOL_CHARSET = 2
Private Const SHIFTJIS_CHARSET = 128
Private Const HANGEUL_CHARSET = 129
Private Const CHINESEBIG5_CHARSET = 136
Private Const OEM_CHARSET = 255
' Font Families
'
Private Const FF_DONTCARE = 0 ' Don't care or don't know.
Private Const FF_ROMAN = 16 ' Variable stroke width, serifed.
' Times Roman, Century Schoolbook, etc.
Private Const FF_SWISS = 32 ' Variable stroke width, sans-serifed.
' Helvetica, Swiss, etc.
Private Const FF_MODERN = 48 ' Constant stroke width, serifed or sans-serifed.
' Pica, Elite, Courier, etc.
Private Const FF_SCRIPT = 64 ' Cursive, etc.
Private Const FF_DECORATIVE = 80 ' Old English, etc.
' Font Weights
Private Const FW_DONTCARE = 0
Private Const FW_THIN = 100
Private Const FW_EXTRALIGHT = 200
Private Const FW_LIGHT = 300
Private Const FW_NORMAL = 400
Private Const FW_MEDIUM = 500
Private Const FW_SEMIBOLD = 600
Private Const FW_BOLD = 700
Private Const FW_EXTRABOLD = 800
Private Const FW_HEAVY = 900
Private Const FW_ULTRALIGHT = FW_EXTRALIGHT
Private Const FW_REGULAR = FW_NORMAL
Private Const FW_DEMIBOLD = FW_SEMIBOLD
Private Const FW_ULTRABOLD = FW_EXTRABOLD
Private Const FW_BLACK = FW_HEAVY
Private Const OUT_DEFAULT_PRECIS = 0
Private Const OUT_STRING_PRECIS = 1
Private Const OUT_CHARACTER_PRECIS = 2
Private Const OUT_STROKE_PRECIS = 3
Private Const OUT_TT_PRECIS = 4
Private Const OUT_DEVICE_PRECIS = 5
Private Const OUT_RASTER_PRECIS = 6
Private Const OUT_TT_ONLY_PRECIS = 7
Private Const OUT_OUTLINE_PRECIS = 8
Private Type LogFont
lfHeight As Long
lfWidth As Long
lfEscapement As Long
lfOrientation As Long
lfWeight As Long
lfItalic As Byte
lfUnderline As Byte
lfStrikeOut As Byte
lfCharSet As Byte
lfOutPrecision As Byte
lfClipPrecision As Byte
lfQuality As Byte
lfPitchAndFamily As Byte
lfFaceName As String * LF_FACESIZE
End Type
Private Declare Function CreateFontIndirect Lib "gdi32" Alias "CreateFontIndirectA" (lpLogFont As LogFont) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hDC As Long, ByVal nIndex As Long) As Long
Private Const LOGPIXELSY = 90 ' Logical pixels/inch in Y
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hWnd As Long, ByVal hDC As Long) As Long
Private m_Font As StdFont
Private m_hFont As Long
Private m_Rotation As Single
Private Sub Class_Terminate()
'
' Clean-up created objects!!!
'
If m_hFont Then
Call DeleteObject(m_hFont)
Set m_Font = Nothing
End If
End Sub
Public Property Set LogFont(ByVal NewFont As Font)
If m_hFont Then
Call DeleteObject(m_hFont)
m_hFont = 0
End If
If NewFont Is Nothing Then
Set m_Font = Nothing
Else
'
' Stash a copy of the passed object,
' to avoid a new reference to it.
'
Set m_Font = New StdFont
With m_Font
.Bold = NewFont.Bold
.Charset = NewFont.Charset
.Italic = NewFont.Italic
.Name = NewFont.Name
.Size = NewFont.Size
.Strikethrough = NewFont.Strikethrough
.Underline = NewFont.Underline
.Weight = NewFont.Weight
End With
m_hFont = CreateLogFont
End If
End Property
Public Property Get LogFont() As Font
Set LogFont = m_Font
End Property
Public Property Let Rotation(ByVal NewVal As Single)
If NewVal <> m_Rotation Then
m_Rotation = NewVal
If m_hFont Then
Call DeleteObject(m_hFont)
m_hFont = 0
End If
If Not (m_Font Is Nothing) Then
m_hFont = CreateLogFont
End If
End If
End Property
Public Property Get Rotation() As Single
Rotation = m_Rotation
End Property
Public Property Get handle() As Long
handle = m_hFont
End Property
Private Function CreateLogFont() As Long
Dim LF As LogFont
Dim hWnd As Long
Dim hDC As Long
hWnd = GetDesktopWindow
hDC = GetDC(hWnd)
With LF
'
' All but two properties are very straight-forward,
' even with rotation, and map directly.
'
.lfHeight = -(m_Font.Size * GetDeviceCaps(hDC, LOGPIXELSY)) / 72
.lfWidth = 0
.lfEscapement = m_Rotation * 10
.lfOrientation = .lfEscapement
.lfWeight = m_Font.Weight
.lfItalic = m_Font.Italic
.lfUnderline = m_Font.Underline
.lfStrikeOut = m_Font.Strikethrough
.lfClipPrecision = CLIP_DEFAULT_PRECIS
.lfQuality = PROOF_QUALITY
.lfPitchAndFamily = DEFAULT_PITCH Or FF_DONTCARE
.lfFaceName = m_Font.Name & vbNullChar
'
' OEM fonts can't rotate, and we must force
' substitution with something ANSI.
'
.lfCharSet = m_Font.Charset
If .lfCharSet = OEM_CHARSET Then
If (m_Rotation Mod 360) <> 0 Then
.lfCharSet = ANSI_CHARSET
End If
End If
'
' Only TrueType fonts can rotate, so we must
' specify TT-only if angle is not zero.
'
If (m_Rotation Mod 360) <> 0 Then
.lfOutPrecision = OUT_TT_ONLY_PRECIS
Else
.lfOutPrecision = OUT_DEFAULT_PRECIS
End If
End With
CreateLogFont = CreateFontIndirect(LF)
Call ReleaseDC(hWnd, hDC)
End Function