Icon.FromHandle(IntPtr) Método

Definición

Crea un Icon GDI+ a partir del identificador de Windows especificado de un icono (HICON).

public:
 static System::Drawing::Icon ^ FromHandle(IntPtr handle);
public static System.Drawing.Icon FromHandle (IntPtr handle);
static member FromHandle : nativeint -> System.Drawing.Icon
Public Shared Function FromHandle (handle As IntPtr) As Icon

Parámetros

handle
IntPtr

nativeint

Identificador de Windows de un icono.

Devoluciones

Icon que crea este método.

Ejemplos

El ejemplo de código siguiente está diseñado para su uso con Windows Forms y requiere PaintEventArgse, que es un parámetro del Paint controlador de eventos. El código realiza las siguientes acciones:

  • Crea una interfaz Bitmap.

  • Dibuja ese objeto en la pantalla.

  • Obtiene un identificador de icono para Bitmap.

  • Establece el Form.Icon atributo del formulario en un icono creado a partir del identificador.

  • Llama a la función DestroyIcon de la API de Windows para liberar recursos.

private:
   [System::Runtime::InteropServices::DllImportAttribute("user32.dll",CharSet=CharSet::Auto)]
   static bool DestroyIcon( IntPtr handle );

private:
   [SecurityPermission(SecurityAction::Demand, Flags=SecurityPermissionFlag::UnmanagedCode)]
   void GetHicon_Example( PaintEventArgs^ e )
   {
      
      // Create a Bitmap object from an image file.
      Bitmap^ myBitmap = gcnew Bitmap( "c:\\FakePhoto.jpg" );
      
      // Draw myBitmap to the screen.
      e->Graphics->DrawImage( myBitmap, 0, 0 );
      
      // Get an Hicon for myBitmap.
      IntPtr Hicon = myBitmap->GetHicon();
      
      // Create a new icon from the handle. 
      System::Drawing::Icon^ newIcon = ::Icon::FromHandle( Hicon );
      
      // Set the form Icon attribute to the new icon.
      this->Icon = newIcon;
      
      // You can now destroy the Icon, since the form creates
      // its own copy of the icon accesible through the Form.Icon property.
      DestroyIcon( newIcon->Handle );
   }

[System.Runtime.InteropServices.DllImport("user32.dll", CharSet = CharSet.Auto)]
extern static bool DestroyIcon(IntPtr handle);

private void GetHicon_Example(PaintEventArgs e)
{

    // Create a Bitmap object from an image file.
    Bitmap myBitmap = new Bitmap(@"c:\FakePhoto.jpg");

    // Draw myBitmap to the screen.
    e.Graphics.DrawImage(myBitmap, 0, 0);

    // Get an Hicon for myBitmap.
    IntPtr Hicon = myBitmap.GetHicon();

    // Create a new icon from the handle. 
    Icon newIcon = Icon.FromHandle(Hicon);

    // Set the form Icon attribute to the new icon.
    this.Icon = newIcon;

    // You can now destroy the icon, since the form creates
    // its own copy of the icon accessible through the Form.Icon property.
    DestroyIcon(newIcon.Handle);
}
<System.Runtime.InteropServices.DllImportAttribute("user32.dll")> _
    Private Shared Function DestroyIcon(ByVal handle _ 
    As IntPtr) As Boolean 
    End Function

   Private Sub GetHicon_Example(ByVal e As PaintEventArgs)

        ' Create a Bitmap object from an image file.
        Dim myBitmap As New Bitmap("c:\FakePhoto.jpg")

        ' Draw myBitmap to the screen.
        e.Graphics.DrawImage(myBitmap, 0, 0)

        ' Get an Hicon for myBitmap.
        Dim HIcon As IntPtr = myBitmap.GetHicon()
    
        ' Create a new icon from the handle.
        Dim newIcon As Icon = System.Drawing.Icon.FromHandle(HIcon)

        ' Set the form Icon attribute to the new icon.
        Me.Icon = newIcon

        ' You can now destroy the icon, since the form creates its 
        ' own copy of the icon accessible through the Form.Icon property.
    DestroyIcon(newIcon.Handle)
    End Sub

Comentarios

Al usar este método, debe eliminar el icono original mediante el método de la DestroyIcon API de Windows para asegurarse de que se liberan los recursos.

Se aplica a