Graphics.GetHdc 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得與此 Graphics相關裝置上下文的handle。
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
傳回
nativeint
處理到與此 Graphics相關的裝置上下文。
實作
範例
以下程式碼範例是為 Windows Forms 設計的,並需要 PaintEventArgse,這是 Paint 事件處理程序的參數。 範例說明呼叫 Windows GDI 函式來執行與 GDI+ Graphics 方法相同的任務。 此程式碼會執行下列動作:
定義了Windows DLL檔案的互通性 DllImportAttribute 屬性 gdi32.dll。 此 DLL 包含所需的 GDI 函式。
定義 Rectangle 該 DLL 中的函式為外部。
會產生紅色筆。
用筆,會用 GDI+ DrawRectangle 方法在螢幕上畫一個矩形。
定義一個內部指標型態變數
hdc,並將其值設為 handle 以對應表單的裝置上下文。使用 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 結構,定義一組圖形物件及其相關屬性,以及影響輸出的圖形模式。 此方法會回傳該裝置上下文,但字型除外。 由於未選取字型,使用該方法回傳FromHdc的句柄呼叫GetHdc方法會失敗。
對 和 GetHdcReleaseHdc 方法的呼叫必須成對出現。 在 和 GetHdc 方法對的作用域ReleaseHdc內,通常只會呼叫 GDI 函式。 在該範圍內呼叫產生Graphics參數的 GDI+ 方法hdc時會出ObjectBusy錯。 此外,GDI+ 會忽略後續操作中參數Graphicshdc狀態的變更。