Este artigo foi traduzido por máquina. Para visualizar o arquivo em inglês, marque a caixa de seleção Inglês. Você também pode exibir o texto Em inglês em uma janela pop-up, movendo o ponteiro do mouse sobre o texto.
Tradução
Inglês

Método Graphics.ReleaseHdc (IntPtr)

 

Libera um identificador de contexto de dispositivo obtido por uma chamada anterior ao método GetHdc desse Graphics.

Namespace:   System.Drawing
Assembly:  System.Drawing (em System.Drawing.dll)

public void ReleaseHdc(
	IntPtr hdc
)

Parâmetros

hdc
Type: System.IntPtr

Identificador para um contexto de dispositivo obtido por uma chamada anterior ao método GetHdc desse Graphics.

The device context is a Windows structure based on ndptecgdi that defines a set of graphical objects and their associated attributes, as well as the graphical modes that affect output.

Calls to the M:System.Drawing.Graphics.GetHdc and M:System.Drawing.Graphics.ReleaseHdc(System.IntPtr) methods must appear in pairs. During the scope of a M:System.Drawing.Graphics.GetHdc and M:System.Drawing.Graphics.ReleaseHdc(System.IntPtr) method pair, you usually make calls only to ndptecgdi functions. Calls in that scope made to ndptecgdiplus methods of the T:System.Drawing.Graphics that produced the hdc parameter fail with an ObjectBusy error. Also, ndptecgdiplus ignores any state changes made to the T:System.Drawing.Graphics of the hdc parameter in subsequent operations.

The following code example is designed for use with Windows Forms, and it requires T:System.Windows.Forms.PaintEventArgse, which is a parameter of the E:System.Windows.Forms.Control.Paint event handler. The example illustrates calling a Windows ndptecgdi function to perform the same task as a ndptecgdiplus T:System.Drawing.Graphics method. The code performs the following actions:

  • Defines the interoperability T:System.Runtime.InteropServices.DllImportAttribute attribute for the Windows DLL file gdi32.dll. This DLL contains the desired ndptecgdi function, and it defines the T:System.Drawing.Rectangle function in that DLL as external.

  • Creates a red pen.

  • With the pen, draws a rectangle to the screen using the ndptecgdiplus Overload:System.Drawing.Graphics.DrawRectangle method.

  • Defines an internal pointer type variable hdc and sets its value to the handle to the device context of the form.

  • Draws a rectangle to the screen using the ndptecgdi T:System.Drawing.Rectangle function.

  • Releases the device context represented by the hdc parameter.

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

[System.Security.Permissions.SecurityPermission(
System.Security.Permissions.SecurityAction.LinkDemand, Flags = 
System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode)]            
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);
}

SecurityPermission

for access to unmanaged code. Related enumeration: F:System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode.

.NET Framework
Disponível desde 1.1
Retornar ao início
Mostrar: