Graphics.GetHdc 方法

定義

取得與這個 Graphics 關聯之裝置內容的控制代碼。

public:
 virtual IntPtr GetHdc();
public:
 IntPtr GetHdc();
public IntPtr GetHdc ();
abstract member GetHdc : unit -> nativeint
override this.GetHdc : unit -> nativeint
member this.GetHdc : unit -> nativeint
Public Function GetHdc () As IntPtr

傳回

IntPtr

nativeint

與這個 Graphics 關聯之裝置內容的控制代碼。

實作

範例

下列程式代碼範例是設計來搭配 Windows Forms 使用,而且需要 PaintEventArgse,這是事件處理程序的參數Paint。 此範例說明如何呼叫 Windows GDI 函式來執行與 GDI+ Graphics 方法相同的工作。 此程式碼會執行下列動作:

  • 定義 Windows DLL 檔案 gdi32.dll 的互操作性 DllImportAttribute 屬性。 此 DLL 包含所需的 GDI 函式。

  • Rectangle將該 DLL 中的函式定義為外部。

  • 建立紅色畫筆。

  • 使用畫筆時,使用 GDI+ DrawRectangle 方法將矩形繪製到螢幕。

  • 定義內部指標類型變數 hdc ,並將其值設定為窗體裝置內容的句柄。

  • 使用 GDI Rectangle 函式將矩形繪製到畫面。

  • 釋放 參數所代表的 hdc 裝置內容。

private:
   [System::Runtime::InteropServices::DllImportAttribute("gdi32.dll")]
   static bool Rectangle( IntPtr hdc, int ulCornerX, int ulCornerY, int lrCornerX, int lrCornerY );

public:
   void GetHdcForGDI1( PaintEventArgs^ e )
   {
      // Create pen.
      Pen^ redPen = gcnew Pen( Color::Red,1.0f );

      // Draw rectangle with GDI+.
      e->Graphics->DrawRectangle( redPen, 10, 10, 100, 50 );

      // Get handle to device context.
      IntPtr hdc = e->Graphics->GetHdc();

      // Draw rectangle with GDI using default pen.
      Rectangle( hdc, 10, 70, 110, 120 );

      // Release handle to device context.
      e->Graphics->ReleaseHdc( hdc );
   }
public class GDI
{
    [System.Runtime.InteropServices.DllImport("gdi32.dll")]
    internal static extern bool Rectangle(
       IntPtr hdc,
       int ulCornerX, int ulCornerY,
       int lrCornerX, int lrCornerY);
}

private void GetHdcForGDI1(PaintEventArgs e)
{
    // Create pen.
    Pen redPen = new Pen(Color.Red, 1);

    // Draw rectangle with GDI+.
    e.Graphics.DrawRectangle(redPen, 10, 10, 100, 50);

    // Get handle to device context.
    IntPtr hdc = e.Graphics.GetHdc();

    // Draw rectangle with GDI using default pen.
    GDI.Rectangle(hdc, 10, 70, 110, 120);

    // Release handle to device context.
    e.Graphics.ReleaseHdc(hdc);
}
Public Class GDI
    <System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")> _
    Friend Shared Function Rectangle(ByVal hdc As IntPtr, _
    ByVal ulCornerX As Integer, ByVal ulCornerY As Integer, ByVal lrCornerX As Integer, _
    ByVal lrCornerY As Integer) As Boolean
    End Function
End Class

<System.Security.Permissions.SecurityPermission( _
System.Security.Permissions.SecurityAction.LinkDemand, Flags:= _
System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode)> _
Private Sub GetHdcForGDI1(ByVal e As PaintEventArgs)

    ' Create pen.
    Dim redPen As New Pen(Color.Red, 1)

    ' Draw rectangle with GDI+.
    e.Graphics.DrawRectangle(redPen, 10, 10, 100, 50)

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

    ' Draw rectangle with GDI using default pen.
    GDI.Rectangle(hdc, 10, 70, 110, 120)

    ' Release handle to device context.
    e.Graphics.ReleaseHdc(hdc)
End Sub

備註

裝置內容是以 GDI 為基礎的 Windows 結構,可定義一組圖形物件及其相關聯的屬性,以及影響輸出的圖形模式。 這個方法會傳回該裝置內容,但字型除外。 因為未選取字型,所以使用方法傳回的GetHdc句柄呼叫 FromHdc 方法將會失敗。

對和 ReleaseHdc 方法的GetHdc呼叫必須以配對顯示。 在 和 ReleaseHdc 方法組的範圍GetHdc期間,您通常只會呼叫 GDI 函式。 對該範圍中產生hdc參數之 GDI+ 方法進行的Graphics呼叫失敗,並出現ObjectBusy錯誤。 此外,GDI+ 會忽略後續作業中對 參數 所做的hdc任何狀態變更Graphics

適用於