Font Font Font Font Class

Definition

Defines a particular format for text, including font face, size, and style attributes. This class cannot be inherited.

public ref class Font sealed : MarshalByRefObject, ICloneable, IDisposable, System::Runtime::Serialization::ISerializable
[System.ComponentModel.TypeConverter(typeof(System.Drawing.FontConverter))]
[System.Runtime.InteropServices.ComVisible(true)]
[Serializable]
public sealed class Font : MarshalByRefObject, ICloneable, IDisposable, System.Runtime.Serialization.ISerializable
type Font = class
    inherit MarshalByRefObject
    interface ICloneable
    interface ISerializable
    interface IDisposable
Public NotInheritable Class Font
Inherits MarshalByRefObject
Implements ICloneable, IDisposable, ISerializable
Inheritance
Attributes
Implements

Examples

The following code example demonstrates how to use the Font constructor and the Size, SizeInPoints, and Unit properties. This example is designed to be used with a Windows Form that contains a ComboBox named ComboBox1 that is populated with the strings "Bigger" and "Smaller" and a Label named Label1. Paste the following code into the form and associate the ComboBox1_SelectedIndexChanged method with the SelectedIndexChanged event of the ComboBox control.

private:
    void ComboBox1_SelectedIndexChanged(System::Object^ sender,
        System::EventArgs^ e)
    {

        // Cast the sender object back to a ComboBox.
        ComboBox^ ComboBox1 = (ComboBox^) sender;

        // Retrieve the selected item.
        String^ selectedString = (String^) ComboBox1->SelectedItem;

        // Convert it to lowercase.
        selectedString = selectedString->ToLower();

        // Declare the current size.
        float currentSize;

        // If Bigger is selected, get the current size from the 
        // Size property and increase it. Reset the font to the
        //  new size, using the current unit.
        if (selectedString == "bigger")
        {
            currentSize = Label1->Font->Size;
            currentSize += 2.0F;
            Label1->Font =gcnew System::Drawing::Font(Label1->Font->Name, 
                currentSize, Label1->Font->Style, Label1->Font->Unit);

        }
        // If Smaller is selected, get the current size, in
        // points, and decrease it by 2.  Reset the font with
        // the new size in points.
        if (selectedString == "smaller")
        {
            currentSize = Label1->Font->Size;
            currentSize -= 2.0F;
            Label1->Font = gcnew System::Drawing::Font(Label1->Font->Name, 
                currentSize, Label1->Font->Style);

        }
    }
private void ComboBox1_SelectedIndexChanged(System.Object sender, 
    System.EventArgs e)
{

    // Cast the sender object back to a ComboBox.
    ComboBox ComboBox1 = (ComboBox) sender;

    // Retrieve the selected item.
    string selectedString = (string) ComboBox1.SelectedItem;

    // Convert it to lowercase.
    selectedString = selectedString.ToLower();

    // Declare the current size.
    float currentSize;

    // Switch on the selected item. 
    switch(selectedString)
    {

            // If Bigger is selected, get the current size from the 
            // Size property and increase it. Reset the font to the
            //  new size, using the current unit.
        case "bigger":
            currentSize = Label1.Font.Size;
            currentSize += 2.0F;
            Label1.Font = new Font(Label1.Font.Name, currentSize, 
                Label1.Font.Style, Label1.Font.Unit);

            // If Smaller is selected, get the current size, in points,
            // and decrease it by 1.  Reset the font with the new size
            // in points.
            break;
        case "smaller":
            currentSize = Label1.Font.SizeInPoints;
            currentSize -= 1;
            Label1.Font = new Font(Label1.Font.Name, currentSize, 
                Label1.Font.Style);
            break;
    }
}
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

    ' Cast the sender object back to a ComboBox.
    Dim ComboBox1 As ComboBox = CType(sender, ComboBox)

    ' Retrieve the selected item.
    Dim selectedString As String = CType(ComboBox1.SelectedItem, String)

    ' Convert it to lowercase.
    selectedString = selectedString.ToLower()

    ' Declare the current size.
    Dim currentSize As Single

    ' Switch on the selected item. 
    Select Case selectedString

        ' If Bigger is selected, get the current size from the 
        ' Size property and increase it. Reset the font to the
        '  new size, using the current unit.
    Case "bigger"
            currentSize = Label1.Font.Size
            currentSize += 2.0F
            Label1.Font = New Font(Label1.Font.Name, currentSize, _
                Label1.Font.Style, Label1.Font.Unit)

            ' If Smaller is selected, get the current size, in points,
            ' and decrease it by 1.  Reset the font with the new size
            ' in points.
        Case "smaller"
            currentSize = Label1.Font.SizeInPoints
            currentSize -= 1
            Label1.Font = New Font(Label1.Font.Name, currentSize, _
                Label1.Font.Style)
    End Select
End Sub

Remarks

For more information about how to construct fonts, see How to: Construct Font Families and Fonts. Windows Forms applications support TrueType fonts and have limited support for OpenType fonts. If you attempt to use a font that is not supported, or the font is not installed on the machine that is running the application, the Microsoft Sans Serif font will be substituted.

Constructors

Font(Font, FontStyle) Font(Font, FontStyle) Font(Font, FontStyle) Font(Font, FontStyle)

Initializes a new Font that uses the specified existing Font and FontStyle enumeration.

Font(FontFamily, Single) Font(FontFamily, Single) Font(FontFamily, Single) Font(FontFamily, Single)

Initializes a new Font using a specified size.

Font(FontFamily, Single, FontStyle) Font(FontFamily, Single, FontStyle) Font(FontFamily, Single, FontStyle) Font(FontFamily, Single, FontStyle)

Initializes a new Font using a specified size and style.

Font(FontFamily, Single, FontStyle, GraphicsUnit) Font(FontFamily, Single, FontStyle, GraphicsUnit) Font(FontFamily, Single, FontStyle, GraphicsUnit) Font(FontFamily, Single, FontStyle, GraphicsUnit)

Initializes a new Font using a specified size, style, and unit.

Font(FontFamily, Single, FontStyle, GraphicsUnit, Byte) Font(FontFamily, Single, FontStyle, GraphicsUnit, Byte) Font(FontFamily, Single, FontStyle, GraphicsUnit, Byte) Font(FontFamily, Single, FontStyle, GraphicsUnit, Byte)

Initializes a new Font using a specified size, style, unit, and character set.

Font(FontFamily, Single, FontStyle, GraphicsUnit, Byte, Boolean) Font(FontFamily, Single, FontStyle, GraphicsUnit, Byte, Boolean) Font(FontFamily, Single, FontStyle, GraphicsUnit, Byte, Boolean) Font(FontFamily, Single, FontStyle, GraphicsUnit, Byte, Boolean)

Initializes a new Font using a specified size, style, unit, and character set.

Font(FontFamily, Single, GraphicsUnit) Font(FontFamily, Single, GraphicsUnit) Font(FontFamily, Single, GraphicsUnit) Font(FontFamily, Single, GraphicsUnit)

Initializes a new Font using a specified size and unit. Sets the style to Regular.

Font(String, Single) Font(String, Single) Font(String, Single) Font(String, Single)

Initializes a new Font using a specified size.

Font(String, Single, FontStyle) Font(String, Single, FontStyle) Font(String, Single, FontStyle) Font(String, Single, FontStyle)

Initializes a new Font using a specified size and style.

Font(String, Single, FontStyle, GraphicsUnit) Font(String, Single, FontStyle, GraphicsUnit) Font(String, Single, FontStyle, GraphicsUnit) Font(String, Single, FontStyle, GraphicsUnit)

Initializes a new Font using a specified size, style, and unit.

Font(String, Single, FontStyle, GraphicsUnit, Byte) Font(String, Single, FontStyle, GraphicsUnit, Byte) Font(String, Single, FontStyle, GraphicsUnit, Byte) Font(String, Single, FontStyle, GraphicsUnit, Byte)

Initializes a new Font using a specified size, style, unit, and character set.

Font(String, Single, FontStyle, GraphicsUnit, Byte, Boolean) Font(String, Single, FontStyle, GraphicsUnit, Byte, Boolean) Font(String, Single, FontStyle, GraphicsUnit, Byte, Boolean) Font(String, Single, FontStyle, GraphicsUnit, Byte, Boolean)

Initializes a new Font using the specified size, style, unit, and character set.

Font(String, Single, GraphicsUnit) Font(String, Single, GraphicsUnit) Font(String, Single, GraphicsUnit) Font(String, Single, GraphicsUnit)

Initializes a new Font using a specified size and unit. The style is set to Regular.

Properties

Bold Bold Bold Bold

Gets a value that indicates whether this Font is bold.

FontFamily FontFamily FontFamily FontFamily

Gets the FontFamily associated with this Font.

GdiCharSet GdiCharSet GdiCharSet GdiCharSet

Gets a byte value that specifies the GDI character set that this Font uses.

GdiVerticalFont GdiVerticalFont GdiVerticalFont GdiVerticalFont

Gets a Boolean value that indicates whether this Font is derived from a GDI vertical font.

Height Height Height Height

Gets the line spacing of this font.

IsSystemFont IsSystemFont IsSystemFont IsSystemFont

Gets a value indicating whether the font is a member of SystemFonts.

Italic Italic Italic Italic

Gets a value that indicates whether this font has the italic style applied.

Name Name Name Name

Gets the face name of this Font.

OriginalFontName OriginalFontName OriginalFontName OriginalFontName

Gets the name of the font originally specified.

Size Size Size Size

Gets the em-size of this Font measured in the units specified by the Unit property.

SizeInPoints SizeInPoints SizeInPoints SizeInPoints

Gets the em-size, in points, of this Font.

Strikeout Strikeout Strikeout Strikeout

Gets a value that indicates whether this Font specifies a horizontal line through the font.

Style Style Style Style

Gets style information for this Font.

SystemFontName SystemFontName SystemFontName SystemFontName

Gets the name of the system font if the IsSystemFont property returns true.

Underline Underline Underline Underline

Gets a value that indicates whether this Font is underlined.

Unit Unit Unit Unit

Gets the unit of measure for this Font.

Methods

Clone() Clone() Clone() Clone()

Creates an exact copy of this Font.

CreateObjRef(Type) CreateObjRef(Type) CreateObjRef(Type) CreateObjRef(Type)

Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(Inherited from MarshalByRefObject)
Dispose() Dispose() Dispose() Dispose()

Releases all resources used by this Font.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Indicates whether the specified object is a Font and has the same FontFamily, GdiVerticalFont, GdiCharSet, Style, Size, and Unit property values as this Font.

Finalize() Finalize() Finalize() Finalize()

Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.

FromHdc(IntPtr) FromHdc(IntPtr) FromHdc(IntPtr) FromHdc(IntPtr)

Creates a Font from the specified Windows handle to a device context.

FromHfont(IntPtr) FromHfont(IntPtr) FromHfont(IntPtr) FromHfont(IntPtr)

Creates a Font from the specified Windows handle.

FromLogFont(Object) FromLogFont(Object) FromLogFont(Object) FromLogFont(Object)

Creates a Font from the specified GDI logical font (LOGFONT) structure.

FromLogFont(Object, IntPtr) FromLogFont(Object, IntPtr) FromLogFont(Object, IntPtr) FromLogFont(Object, IntPtr)

Creates a Font from the specified GDI logical font (LOGFONT) structure.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Gets the hash code for this Font.

GetHeight() GetHeight() GetHeight() GetHeight()

Returns the line spacing, in pixels, of this font.

GetHeight(Graphics) GetHeight(Graphics) GetHeight(Graphics) GetHeight(Graphics)

Returns the line spacing, in the current unit of a specified Graphics, of this font.

GetHeight(Single) GetHeight(Single) GetHeight(Single) GetHeight(Single)

Returns the height, in pixels, of this Font when drawn to a device with the specified vertical resolution.

GetLifetimeService() GetLifetimeService() GetLifetimeService() GetLifetimeService()

Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
GetType() GetType() GetType() GetType()

Gets the Type of the current instance.

(Inherited from Object)
InitializeLifetimeService() InitializeLifetimeService() InitializeLifetimeService() InitializeLifetimeService()

Obtains a lifetime service object to control the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
MemberwiseClone(Boolean) MemberwiseClone(Boolean) MemberwiseClone(Boolean) MemberwiseClone(Boolean)

Creates a shallow copy of the current MarshalByRefObject object.

(Inherited from MarshalByRefObject)
ToHfont() ToHfont() ToHfont() ToHfont()

Returns a handle to this Font.

ToLogFont(Object) ToLogFont(Object) ToLogFont(Object) ToLogFont(Object)

Creates a GDI logical font (LOGFONT) structure from this Font.

ToLogFont(Object, Graphics) ToLogFont(Object, Graphics) ToLogFont(Object, Graphics) ToLogFont(Object, Graphics)

Creates a GDI logical font (LOGFONT) structure from this Font.

ToString() ToString() ToString() ToString()

Returns a human-readable string representation of this Font.

Explicit Interface Implementations

ISerializable.GetObjectData(SerializationInfo, StreamingContext) ISerializable.GetObjectData(SerializationInfo, StreamingContext) ISerializable.GetObjectData(SerializationInfo, StreamingContext) ISerializable.GetObjectData(SerializationInfo, StreamingContext)

Populates a SerializationInfo with the data needed to serialize the target object.

Applies to

See Also