Icon.FromHandle(IntPtr) Methode

Definition

Erstellt ein GDI+-Icon aus dem angegebenen Windows-Handle für ein Symbol (HICON).Creates a GDI+ Icon from the specified Windows handle to an icon (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

Parameter

handle
IntPtr

Ein Windows-Handle für ein Symbol.A Windows handle to an icon.

Gibt zurück

Die Icon, die von dieser Methode erstellt wird.The Icon this method creates.

Beispiele

Das folgende Codebeispiel ist für die Verwendung mit Windows Forms konzipiert und erfordert PaintEventArgs e, wobei es sich um einen Parameter des Paint -Ereignis Handlers handelt.The following code example is designed for use with Windows Forms, and it requires PaintEventArgse, which is a parameter of the Paint event handler. Der Code führt die folgenden Aktionen aus:The code performs the following actions:

  • Erstellt eine Bitmap.Creates a Bitmap.

  • Zeichnet dieses Objekt auf dem Bildschirm.Draws that object to the screen.

  • Ruft ein Symbol Handle für den Bitmapab.Gets an icon handle for the Bitmap.

  • Legt das Form.Icon -Attribut des Formulars auf ein Symbol fest, das aus dem Handle erstellt wurde.Sets the Form.Icon attribute of the form to an icon created from the handle.

  • Ruft die Windows-API DestroyIcon -Funktion auf, um Ressourcen freizugeben.Calls the Windows API function DestroyIcon to release resources.

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

Hinweise

Wenn Sie diese Methode verwenden, müssen Sie das Original Symbol mithilfe der DestroyIcon -Methode in der Windows-API verwerfen, um sicherzustellen, dass die Ressourcen freigegeben werden.When using this method, you must dispose of the original icon by using the DestroyIcon method in the Windows API to ensure that the resources are released.

Sicherheit

SecurityPermission
für den Zugriff auf nicht verwalteten Code.for access to unmanaged code. Verwandte Enumeration UnmanagedCode.Related enumeration UnmanagedCode.

Gilt für: