Graphics.ReleaseHdc 方法

定義

釋放由先前呼叫這個 GraphicsGetHdc() 方法所取得的裝置內容控制代碼。

多載

ReleaseHdc()

釋放由先前呼叫這個 GraphicsGetHdc() 方法所取得的裝置內容控制代碼。

ReleaseHdc(IntPtr)

釋放由先前呼叫這個 GraphicsGetHdc() 方法所取得的裝置內容控制代碼。

ReleaseHdc()

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

釋放由先前呼叫這個 GraphicsGetHdc() 方法所取得的裝置內容控制代碼。

public:
 virtual void ReleaseHdc();
public void ReleaseHdc ();
abstract member ReleaseHdc : unit -> unit
override this.ReleaseHdc : unit -> unit
Public Sub ReleaseHdc ()

實作

備註

GetHdcReleaseHdc 是兩種方法,可讓您取得和釋放 Windows 裝置的句柄。 當您完成 Windows 句柄時,應該一律遵循 對的呼叫GetHdcReleaseHdc

另請參閱

適用於

ReleaseHdc(IntPtr)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

釋放由先前呼叫這個 GraphicsGetHdc() 方法所取得的裝置內容控制代碼。

public:
 void ReleaseHdc(IntPtr hdc);
public void ReleaseHdc (IntPtr hdc);
member this.ReleaseHdc : nativeint -> unit
Public Sub ReleaseHdc (hdc As IntPtr)

參數

hdc
IntPtr

nativeint

藉由先前呼叫這個 GraphicsGetHdc() 方法所取得的裝置內容控制代碼。

範例

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

  • 定義 Windows DLL 檔案 gdi32.dll 的互操作性 DllImportAttribute 屬性。 此 DLL 包含所需的 GDI 函式,並將該 DLL 中的函式定義為 Rectangle 外部。

  • 建立紅色畫筆。

  • 使用手寫筆,使用 GDI+ DrawRectangle 方法將矩形繪製到畫面。

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

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

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

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

public:
   void GetHdcForGDI2( 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.
      Rectangle2( hdc, 10, 70, 110, 120 );

      // Release handle to device context.
      e->Graphics->ReleaseHdc( hdc );
   }
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern bool Rectangle2(
    IntPtr hdc,
    int ulCornerX, int ulCornerY,
    int lrCornerX, int lrCornerY);

private void GetHdcForGDI2(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.
    Rectangle2(hdc, 10, 70, 110, 120);

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

<System.Security.Permissions.SecurityPermission( _
System.Security.Permissions.SecurityAction.LinkDemand, Flags:= _
System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode)> _
Private Sub GetHdcForGDI2(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.
    Rectangle2(hdc, 10, 70, 110, 120)

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

備註

裝置內容是以 GDI 為基礎的 Windows 結構,可定義一組圖形物件及其相關聯的屬性,以及影響輸出的圖形模式。

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

適用於