ChooseFont Function
The ChooseFont
function displays the Windows font dialog, which allows the user to select a font, style, and size. If the user clicks the OK button, the function returns nonzero and updates the CHOOSEFONT
structure with the user's choices. If the user clicks the Cancel button, the function returns zero.
Syntax
BOOL ChooseFont(
[in, out] LPCHOOSEFONT lpcf
);
Parameters
Parameter | Type | Description |
---|---|---|
lpcf |
LPCHOOSEFONT |
A pointer to a CHOOSEFONT structure that contains information about the font dialog. When the function returns, this structure contains information about the font chosen by the user. |
Return Value
If the user clicks the OK button, the return value is nonzero.
If the user clicks the Cancel button, the return value is zero.
If the function fails due to insufficient memory, the return value is zero. To get extended error information, call GetLastError
.
Remarks
The CHOOSEFONT
structure can be initialized with default values before being passed to ChooseFont
. The dialog box can be configured using various flags within the Flags
member of the CHOOSEFONT
structure.
Commonly used flags include:
CF_EFFECTS
: Enables the selection of font effects like strikethrough and underline.CF_LOGFONT
: Initializes the dialog box with the font specified in thelpLogFont
member.CF_NOSTYLESEL
: Disables the style selection.CF_NOSIZES
: Disables the size selection.CF_SELECTFONT
: Selects the font specified by thelpLogFont
member.CF_SCRIPTUNION
: Enables selection of character script.CF_TTCFONTS
: Enables TrueType font selection.CF_WSELECTALL
: Selects all fonts if the specified font is not found.
Note
For a complete list of flags and a detailed explanation of the CHOOSEFONT
structure members, please refer to the CHOOSEFONT structure documentation.
Example
The following code snippet demonstrates how to display the font dialog and use the chosen font.
#include <windows.h>
#include <commdlg.h>
// ...
CHOOSEFONT cf;
LOGFONT lf;
HDC hdc;
ZeroMemory(&cf, sizeof(cf));
cf.lStructSize = sizeof(cf);
cf.hwndOwner = hWnd; // Your window handle
cf.lpLogFont = &lf;
cf.Flags = CF_SCREENFONTS | CF_LIMITSIZE | CF_EFFECTS; // Example flags
if (ChooseFont(&cf)) {
// User selected a font, use cf.lpLogFont to get the chosen font details
hdc = GetDC(hWnd);
HFONT hFont = CreateFontIndirect(cf.lpLogFont);
// ... use the font ...
SelectObject(hdc, hFont);
ReleaseDC(hWnd, hdc);
DeleteObject(hFont);
} else {
// User cancelled or an error occurred
}
Requirements
Header | Library |
---|---|
commdlg.h |
Comdlg32.lib |