CreateFont Function

Declare Function CreateFont Lib "gdi32.dll" Alias "CreateFontA" (ByVal nHeight As Long, ByVal nWidth As Long, ByVal nEscapement As Long, ByVal nOrientation As Long, ByVal fnWeight As Long, ByVal fdwItalic As Long, ByVal fdwUnderline As Long, ByVal fdwStrikeOut As Long, ByVal fdwCharSet As Long, ByVal fdwOutputPrecision As Long, ByVal fdwClipPrecision As Long, ByVal fdwQuality As Long, ByVal fdwPitchAndFamily As Long, ByVal lpszFace As String) As Long

Platforms

Description & Usage

CreateFont creates a font object, which allows a font with given attributes to be used to draw text on a device. The font created by the function is the one which most closely matches the attributes of the logical font information passed via the numerous parameters. After your program is finished using the font, it must be deleted by using DeleteObject.

Return Value

If an error occured, the function returns 0 (Windows NT, 2000: use GetLastError to get the error code). If successful, the function returns a handle to the font just created.

Visual Basic-Specific Issues

None.

Parameters

nHeight
The height of the font's character cell, in logical units (also known as the em height). If positive, the font mapper converts this value directly into device units and matches it with the cell height of the possible fonts. If 0, the font mapper uses a default character height. If negative, the font mapper converts the absolute value into device units and matches it with the character height of the possible fonts.
nWidth
The average width of the font's characters. If 0, the font mapper tries to determine the best value.
nEscapement
The angle between the font's baseline and escapement vectors, in units of 1/10 degrees. Windows 95, 98: This must be equal to lfOrientation.
nOrientation
The angle between the font's baseline and the device's x-axis, in units of 1/10 degrees. Windows 95, 98: This must be equal to lfEscapement.
fnWeight
One of the following flags specifying the boldness (weight) of the font:
FW_DONTCARE
Default weight.
FW_THIN
Thin weight.
FW_EXTRALIGHT
Extra-light weight.
FW_ULTRALIGHT
Same as FW_EXTRALIGHT.
FW_LIGHT
Light weight.
FW_NORMAL
Normal weight.
FW_REGULAR
Same as FW_NORMAL.
FW_MEDIUM
Medium weight.
FW_SEMIBOLD
Semi-bold weight.
FW_DEMIBOLD
Same As FW_SEMIBOLD.
FW_BOLD
Bold weight.
FW_EXTRABOLD
Extra-bold weight.
FW_ULTRABOLD
Same as FW_EXTRABOLD.
FW_HEAVY
Heavy weight.
FW_BLACK
Same as FW_HEAVY.
fdwItalic
A non-zero value if the font is italicized, 0 if not.
fdwUnderline
A non-zero value if the font is underlined, 0 if not.
fdwStrikeOut
A non-zero value if the font is striked out, 0 if not.
fdwCharSet
Exactly one of the following flags specifying the character set of the font:
ANSI_CHARSET
ANSI character set.
ARABIC_CHARSET
Windows NT, 2000: Arabic character set.
BALTIC_CHARSET
Windows 95, 98: Baltic character set.
CHINESEBIG5_CHARSET
Chinese Big 5 character set.
DEFAULT_CHARSET
Default character set.
EASTEUROPE_CHARSET
Windows 95, 98: Eastern European character set.
GB2312_CHARSET
GB2312 character set.
GREEK_CHARSET
Windows 95, 98: Greek character set.
HANGEUL_CHARSET
HANDEUL character set.
HEBREW_CHARSET
Windows NT, 2000: Hebrew character set.
JOHAB_CHARSET
Windows 95, 98: Johab character set.
MAC_CHARSET
Windows 95, 98: Mac character set.
OEM_CHARSET
Original equipment manufacturer (OEM) character set.
RUSSIAN_CHARSET
Windows 95, 98: Russian character set.
SHIFTJIS_CHARSET
ShiftJis character set.
SYMBOL_CHARSET
Symbol character set.
THAI_CHARSET
Windows NT, 2000: Thai character set.
TURKISH_CHARSET
Windows 95, 98: Turkish character set.
fdwOutPrecision
Exactly one of the following flags specifying the desired precision (closeness of the match) between the logical font ideally described by the structure and the actual logical font. This value is used by the font mapper to produce the logical font.
OUT_DEFAULT_PRECIS
The default font mapping behavior.
OUT_DEVICE_PRECIS
Choose a device font if there are multiple fonts in the system with the same name.
OUT_OUTLINE_PRECIS
Windows NT, 2000: Choose a TrueType or other outline-based font.
OUT_RASTER_PRECIS
Choose a raster font if there are multiple fonts in the system with the same name.
OUT_STRING_PRECIS
Raster font (used for enumeration only).
OUT_STROKE_PRECIS
Windows 95, 98: Vector font (used for enumeration only). Windows NT, 2000: TrueType, outline-based, or vector font (used for enumeration only).
OUT_TT_ONLY_PRECIS
Choose only a TrueType font.
OUT_TT_PRECIS
Choose a TrueType font if there are multiple fonts in the system with the same name.
fdwClipPrecision
Exactly one of the following flags specifying the clipping precision to use when the font's characters must be clipped:
CLIP_DEFAULT_PRECIS
The default clipping behavior.
CLIP_EMBEDDED
This flag must be set for an embedded read-only font.
CLIP_LH_ANGLES
The direction of any rotations is determined by the coordinate system (or else all rotations are counterclockwise).
CLIP_STROKE_PRECIS
Raster, vector, or TrueType font (used for enumeration only).
fdwQuality
Exactly one of the following flags specifying the output quality of the logical font as compared to the ideal font:
ANTIALIASED_QUALITY
Windows 95, 98, NT 4.0 or later, 2000: The font is always antialiased if possible.
DEFAULT_QUALITY
The default quality: the appearance of the font does not matter.
DRAFT_QUALITY
The appearance of the font is less important then in PROOF_QUALITY.
NONANTIALIASED_QUALITY
Windows 95, 98, NT 4.0 or later, 2000: The font is never antialiased.
PROOF_QUALITY
The quality of the appearance of the font is more important than exactly matching the specified font attributes.
fdwPitchAndFamily
A bitwise OR combination of exactly one *_PITCH flag specifying the pitch of the font and exactly one FF_* flag specifying the font face family of the font:
DEFAULT_PITCH
The default pitch.
FIXED_PITCH
Fixed pitch.
VARIABLE_PITCH
Variable pitch.
FF_DECORATIVE
Showy, decorative font face.
FF_DONTCARE
Do not care about the font face.
FF_MODERN
Modern font face (monospaced, sans serif font).
FF_ROMAN
Roman font face (proportional-width, serif font).
FF_SCRIPT
Script font face which imitates script handwriting.
FF_SWISS
Swiss font face (proportional-width, sans serif font).
lpszFace
The name of the font face to use.

Constant Definitions

Const FW_DONTCARE = 0
Const FW_THIN = 100
Const FW_EXTRALIGHT = 200
Const FW_ULTRALIGHT = 200
Const FW_LIGHT = 300
Const FW_NORMAL = 400
Const FW_REGULAR = 400
Const FW_MEDIUM = 500
Const FW_SEMIBOLD = 600
Const FW_DEMIBOLD = 600
Const FW_BOLD = 700
Const FW_EXTRABOLD = 800
Const FW_ULTRABOLD = 800
Const FW_HEAVY = 900
Const FW_BLACK = 900
Const ANSI_CHARSET = 0
Const ARABIC_CHARSET = 178
Const BALTIC_CHARSET = 186
Const CHINESEBIG5_CHARSET = 136
Const DEFAULT_CHARSET = 1
Const EASTEUROPE_CHARSET = 238
Const GB2312_CHARSET = 134
Const GREEK_CHARSET = 161
Const HANGEUL_CHARSET = 129
Const HEBREW_CHARSET = 177
Const JOHAB_CHARSET = 130
Const MAC_CHARSET = 77
Const OEM_CHARSET = 255
Const RUSSIAN_CHARSET = 204
Const SHIFTJIS_CHARSET = 128
Const SYMBOL_CHARSET = 2
Const THAI_CHARSET = 222
Const TURKISH_CHARSET = 162
Const OUT_DEFAULT_PRECIS = 0
Const OUT_DEVICE_PRECIS = 5
Const OUT_OUTLINE_PRECIS = 8
Const OUT_RASTER_PRECIS = 6
Const OUT_STRING_PRECIS = 1
Const OUT_STROKE_PRECIS = 3
Const OUT_TT_ONLY_PRECIS = 7
Const OUT_TT_PRECIS = 4
Const CLIP_DEFAULT_PRECIS = 0
Const CLIP_EMBEDDED = 128
Const CLIP_LH_ANGLES = 16
Const CLIP_STROKE_PRECIS = 2
Const ANTIALIASED_QUALITY = 4
Const DEFAULT_QUALITY = 0
Const DRAFT_QUALITY = 1
Const NONANTIALIASED_QUALITY = 3
Const PROOF_QUALITY = 2
Const DEFAULT_PITCH = 0
Const FIXED_PITCH = 1
Const VARIABLE_PITCH = 2
Const FF_DECORATIVE = 80
Const FF_DONTCARE = 0
Const FF_ROMAN = 16
Const FF_SCRIPT = 64
Const FF_SWISS = 32

Example

' This code is licensed according to the terms and conditions listed here.

' Draw the alphabet in the upper-left corner of window Form1 using
' the goofy Symbol font.
Dim hFont As Long  ' handle to the Symbol font which is created
Dim hOldFont As Long  ' handle to the font previously selected by Form1
Dim retval As Long  ' return value

' Create a font object using the Symbol font.  Only apply boldface
' formatting to the font -- use defaults for most other settings.
hFont = CreateFont(0, 0, 0, 0, FW_BOLD, 0, 0, 0, SYMBOL_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH Or FF_DECORATIVE, "Symbol")

' Select that font for use with Form1, saving the previous selection.
hOldFont = SelectObject(Form1.hDC, hFont)
' Draw the alphabet near the upper-left corner.
retval = TextOut(Form1.hDC, 10, 10, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 26)

' Select the previous font back.
retval = SelectObject(Form1.hDC, hOldFont)
' Delete the font we created to free up resources.
retval = DeleteObject(hFont)

See Also

CreateFontIndirect

Category

Fonts & Text

Go back to the alphabetical Function listing.
Go back to the Reference section index.


Last Modified: October 13, 1999
This page is copyright © 1999 Paul Kuliniewicz. Copyright Information Revised October 29, 2000
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/c/createfont.html