Share via


Graphics.GetHdc Yöntem

Tanım

Bu ile Graphicsilişkilendirilmiş cihaz bağlamı tutamacını alır.

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

Döndürülenler

IntPtr

nativeint

Bu Graphicsile ilişkili cihaz bağlamını işle.

Uygulamalar

Örnekler

Aşağıdaki kod örneği, Windows Forms ile kullanılmak üzere tasarlanmıştır ve olay işleyicisinin bir parametresi olan öğesini gerektirirPaintEventArgse.Paint Örnekte, GDI+ Graphics yöntemiyle aynı görevi gerçekleştirmek için Bir Windows GDI işlevinin çağrılması gösterilmektedir. Kod aşağıdaki eylemleri gerçekleştirir:

  • Windows DLL dosyası gdi32.dll için birlikte çalışabilirlik DllImportAttribute özniteliğini tanımlar. Bu DLL istenen GDI işlevini içerir.

  • Bu DLL'deki Rectangle işlevi dış olarak tanımlar.

  • Kırmızı kalem oluşturur.

  • Kalemle, GDI+ DrawRectangle yöntemini kullanarak ekrana bir dikdörtgen çizer.

  • bir iç işaretçi türü değişkeni hdc tanımlar ve değerini formun cihaz bağlamı için tanıtıcıya ayarlar.

  • GDI Rectangle işlevini kullanarak ekrana dikdörtgen çizer.

  • parametresiyle temsil edilen cihaz bağlamını hdc serbest bırakır.

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

Açıklamalar

Cihaz bağlamı, GDI'yi temel alan ve bir dizi grafik nesnesi ve bunların ilişkili özniteliklerinin yanı sıra çıkışı etkileyen grafik modlarını tanımlayan bir Windows yapısıdır. Bu yöntem, bir yazı tipi dışında bu cihaz bağlamını döndürür. Yazı tipi seçilmediğinden, yönteminden döndürülen bir tanıtıcı kullanarak yöntemine yapılan GetHdc çağrılar FromHdc başarısız olur.

ve ReleaseHdc yöntemlerine yapılan GetHdc çağrılar çiftler halinde görünmelidir. ve GetHdcReleaseHdc yöntem çiftinin kapsamı sırasında genellikle yalnızca GDI işlevlerine çağrı yaparsınız. Parametreyi oluşturan GDI+ yöntemlerine Graphics yapılan bu kapsamdaki hdc çağrılar bir ObjectBusy hatayla başarısız olur. Ayrıca GDI+, sonraki işlemlerde parametresinde yapılan Graphicshdc durum değişikliklerini yoksayar.

Şunlara uygulanır