Font.FromHfont(IntPtr) Font.FromHfont(IntPtr) Font.FromHfont(IntPtr) Font.FromHfont(IntPtr) Method

定義

從指定的 Windows 控制代碼建立 FontCreates 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

參數

hfont
IntPtr IntPtr IntPtr IntPtr

GDIGDI 字型的 Windows 控制代碼。A Windows handle to a GDIGDI font.

傳回

這個方法建立的 FontThe Font this method creates.

例外狀況

hfont 指向不是 TrueType 字型的物件。hfont points to an object that is not a TrueType font.

範例

下列程式碼範例是針對與 Windows Forms 搭配使用所設計, 而且PaintEventArgs它需要e, 這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. 該程式碼執行下列動作:The code performs the following actions:

  • 取得GDIGDI字型的控制碼。Gets a handle to a GDIGDI font.

  • Font從該控制碼建立。Creates a Font from that handle.

  • 使用新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

安全性

SecurityPermission
用於存取未受管理的程式碼。for access to unmanaged code. 相關列舉:UnmanagedCodeRelated enumeration: UnmanagedCode

適用於