Font.FromHfont(IntPtr) Method

Definition

Cria um Font com base no identificador especificado do Windows.Creates a Font from the specified Windows handle.

public:
 static System::Drawing::Font ^ FromHfont(IntPtr hfont);
public static System.Drawing.Font FromHfont (IntPtr hfont);
static member FromHfont : nativeint -> System.Drawing.Font
Public Shared Function FromHfont (hfont As IntPtr) As Font

Parameters

hfont
IntPtr

Um identificador do Windows para uma fonte GDIGDI.A Windows handle to a GDIGDI font.

Returns

Font

O Font criado por esse método.The Font this method creates.

Exceptions

hfont aponta para um objeto que não é uma fonte TrueType.hfont points to an object that is not a TrueType font.

Examples

O exemplo de código a seguir foi projetado para uso com Windows Forms e requer PaintEventArgse, que é um parâmetro do manipulador de eventos de Paint.The following code example is designed for use with Windows Forms, and it requires PaintEventArgse, which is a parameter of the Paint event handler. O código executa as seguintes ações:The code performs the following actions:

  • Obtém um identificador para uma fonte de GDIGDI.Gets a handle to a GDIGDI font.

  • Cria uma Font a partir desse identificador.Creates a Font from that handle.

  • Desenha o texto na tela, usando o novo Font.Draws text to the screen, using the new Font.

private:
   [System::Runtime::InteropServices::DllImportAttribute("gdi32.dll")]
   static IntPtr GetStockObject( int fnObject );

public:
   void FromHfont_Example( PaintEventArgs^ e )
   {
      // Get a handle for a GDI font.
      IntPtr hFont = GetStockObject( 17 );

      // Create a Font object from hFont.
      System::Drawing::Font^ hfontFont = System::Drawing::Font::FromHfont( hFont );

      // Use hfontFont to draw text to the screen.
      e->Graphics->DrawString( "This font is from a GDI HFONT", hfontFont, Brushes::Black, 0, 0 );
   }
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern IntPtr GetStockObject(int fnObject);
public void FromHfont_Example(PaintEventArgs e)
{
             
    // Get a handle for a GDI font.
    IntPtr hFont = GetStockObject(0);
             
    // Create a Font object from hFont.
    Font hfontFont = Font.FromHfont(hFont);
             
    // Use hfontFont to draw text to the screen.
    e.Graphics.DrawString(
        "This font is from a GDI HFONT", hfontFont,Brushes.Black, 
        0, 0);
}
<System.Runtime.InteropServices.DllImportAttribute("GDI32.DLL")> _
Private Shared Function GetStockObject(ByVal fnObject As Integer) As IntPtr
End Function
Public Sub FromHfont_Example(ByVal e As PaintEventArgs)

    ' Get a handle for a GDI font.
    Dim hFont As IntPtr = GetStockObject(17)

    ' Create a Font object from hFont.
    Dim hfontFont As Font = Font.FromHfont(hFont)

    ' Use hfontFont to draw text to the screen.
    e.Graphics.DrawString("This font is from a GDI HFONT", hfontFont, _
    Brushes.Black, 0, 0)
End Sub

Applies to