Graphics.ReleaseHdc 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
多載
| 名稱 | Description |
|---|---|
| ReleaseHdc() | |
| ReleaseHdc(IntPtr) |
ReleaseHdc()
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
public:
virtual void ReleaseHdc();
public void ReleaseHdc();
abstract member ReleaseHdc : unit -> unit
override this.ReleaseHdc : unit -> unit
Public Sub ReleaseHdc ()
實作
備註
GetHdc 和 ReleaseHdc 是兩種方法,可以讓你拿出並放開Windows裝置的把手。 完成Windows柄後,應在呼叫GetHdc後,再呼叫ReleaseHdc。
另請參閱
適用於
ReleaseHdc(IntPtr)
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
- 來源:
- Graphics.cs
public:
void ReleaseHdc(IntPtr hdc);
public void ReleaseHdc(IntPtr hdc);
member this.ReleaseHdc : nativeint -> unit
Public Sub ReleaseHdc (hdc As IntPtr)
參數
範例
以下程式碼範例是為 Windows Forms 設計的,並需要 PaintEventArgse,這是 Paint 事件處理程序的參數。 範例說明呼叫 Windows GDI 函式來執行與 GDI+ Graphics 方法相同的任務。 此程式碼會執行下列動作:
定義了Windows DLL檔案的互通性 DllImportAttribute 屬性 gdi32.dll。 此 DLL 包含所需的 GDI 函式,並將該 DLL 函式定義 Rectangle 為外部函式。
會產生紅色筆。
用筆,會用 GDI+ DrawRectangle 方法在螢幕上畫一個矩形。
定義一個內部指標型態變數
hdc,並將其值設為 handle 以對應表單的裝置上下文。使用 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 結構,定義一組圖形物件及其相關屬性,以及影響輸出的圖形模式。
對 和 GetHdcReleaseHdc 方法的呼叫必須成對出現。 在 和 GetHdc 方法對的ReleaseHdc作用域內,通常只會呼叫 GDI 函式。 在該範圍內呼叫產生Graphics參數的 GDI+ 方法hdc時會出ObjectBusy錯。 此外,GDI+ 會忽略後續操作中參數Graphicshdc狀態的變更。