共用方式為


Graphics.FromHdc 方法

定義

從指定的句柄建立新的 Graphics 到裝置內容。

多載

FromHdc(IntPtr)

從指定的句柄建立新的 Graphics 到裝置內容。

FromHdc(IntPtr, IntPtr)

從指定的句柄建立新的 Graphics 到裝置內容,以及裝置的句柄。

FromHdc(IntPtr)

來源:
Graphics.cs
來源:
Graphics.cs
來源:
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 方法來釋放 FromHdc 方法所建立的 Graphics 和相關資源。

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

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

適用於

FromHdc(IntPtr, IntPtr)

來源:
Graphics.cs
來源:
Graphics.cs
來源:
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 方法來釋放 FromHdc 方法所建立的 Graphics 和相關資源。

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

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

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

適用於