Graphics.FromHdc 方法

定義

從指定的裝置內容控制代碼建立新的 Graphics

多載

FromHdc(IntPtr)

從指定的裝置內容控制代碼建立新的 Graphics

FromHdc(IntPtr, IntPtr)

從指定的裝置內容控制代碼和裝置控制代碼,建立新的 Graphics

FromHdc(IntPtr)

來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs

從指定的裝置內容控制代碼建立新的 Graphics

public:
 static System::Drawing::Graphics ^ FromHdc(IntPtr hdc);
public static System.Drawing.Graphics FromHdc (IntPtr hdc);
static member FromHdc : nativeint -> System.Drawing.Graphics
Public Shared Function FromHdc (hdc As IntPtr) As Graphics

參數

hdc
IntPtr

nativeint

裝置內容的控制代碼。

傳回

這個方法會傳回指定之裝置內容的新 Graphics

範例

下列程式代碼範例是設計來搭配 Windows Forms 使用,而且需要 PaintEventArgse,這是事件處理程序的參數Paint。 程式代碼會執行下列動作:

  • 建立內部指標類型變數 hdc ,並將其設定為窗體圖形對象的裝置內容句柄。

  • 使用 hdc建立新的圖形物件。

  • 在畫面上繪製具有新圖形物件 (矩形) 。

  • 使用 hdc釋放新的圖形物件。

public:
   void FromHdcHdc( PaintEventArgs^ e )
   {
      // Get handle to device context.
      IntPtr hdc = e->Graphics->GetHdc();

      // Create new graphics object using handle to device context.
      Graphics^ newGraphics = Graphics::FromHdc( hdc );

      // Draw rectangle to screen.
      newGraphics->DrawRectangle( gcnew Pen( Color::Red,3.0f ), 0, 0, 200, 100 );

      // Release handle to device context and dispose of the      // Graphics object
      e->Graphics->ReleaseHdc( hdc );
      delete newGraphics;
   }
private void FromHdcHdc(PaintEventArgs e)
{
    // Get handle to device context.
    IntPtr hdc = e.Graphics.GetHdc();

    // Create new graphics object using handle to device context.
    Graphics newGraphics = Graphics.FromHdc(hdc);

    // Draw rectangle to screen.
    newGraphics.DrawRectangle(new Pen(Color.Red, 3), 0, 0, 200, 100);

    // Release handle to device context and dispose of the      // Graphics object
    e.Graphics.ReleaseHdc(hdc);
    newGraphics.Dispose();
}
<System.Security.Permissions.SecurityPermission( _
System.Security.Permissions.SecurityAction.LinkDemand, Flags := _
System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode)> _
Private Sub FromHdcHdc(ByVal e As PaintEventArgs)

    ' Get handle to device context.
    Dim hdc As IntPtr = e.Graphics.GetHdc()

    ' Create new graphics object using handle to device context.
    Dim newGraphics As Graphics = Graphics.FromHdc(hdc)

    ' Draw rectangle to screen.
    newGraphics.DrawRectangle(New Pen(Color.Red, 3), 0, 0, 200, 100)

    ' Release handle to device context and dispose of the Graphics 	' object
    e.Graphics.ReleaseHdc(hdc)
    newGraphics.Dispose()
End Sub

備註

您應該一律呼叫 Dispose 方法,以釋放 Graphics 方法所 FromHdc 建立的 和相關資源。

即使顯示裝置有相關聯的 ICM 色彩設定檔,GDI+ 預設也不會使用該配置檔。 若要開啟 的 GraphicsICM,請在您將 HDC (和ICM_ON) 传递至SetICMMode函式之後,從 HDC 建構 Graphics 。 接著,完成 Graphics 的任何繪圖都會根據與顯示裝置相關聯的 ICM 配置檔進行調整。 啟用ICM會導致效能變慢。

在呼叫 FromHdc 時,裝置內容的狀態 (對應模式、邏輯單元和類似) ,可能會影響 完成的 Graphics轉譯。

適用於

FromHdc(IntPtr, IntPtr)

來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs

從指定的裝置內容控制代碼和裝置控制代碼,建立新的 Graphics

public:
 static System::Drawing::Graphics ^ FromHdc(IntPtr hdc, IntPtr hdevice);
public static System.Drawing.Graphics FromHdc (IntPtr hdc, IntPtr hdevice);
static member FromHdc : nativeint * nativeint -> System.Drawing.Graphics
Public Shared Function FromHdc (hdc As IntPtr, hdevice As IntPtr) As Graphics

參數

hdc
IntPtr

nativeint

裝置內容的控制代碼。

hdevice
IntPtr

nativeint

裝置的控制代碼。

傳回

這個方法會傳回指定之裝置內容和裝置的新 Graphics

備註

您應該一律呼叫 Dispose 方法,以釋放 Graphics 方法所 FromHdc 建立的 和相關資源。

即使顯示裝置有相關聯的 ICM 色彩設定檔,GDI+ 預設也不會使用該配置檔。 若要開啟 的 GraphicsICM,請在您將 HDC (和ICM_ON) 传递至SetICMMode函式之後,從 HDC 建構 Graphics 。 接著,完成 Graphics 的任何繪圖都會根據與顯示裝置相關聯的 ICM 配置檔進行調整。 啟用ICM會導致效能變慢。

在呼叫 FromHdc 時,裝置內容的狀態 (對應模式、邏輯單元和類似) ,可能會影響 完成的 Graphics轉譯。

裝置句柄通常用於查詢特定的印表機功能。

適用於