Icon.FromHandle(IntPtr) Icon.FromHandle(IntPtr) Icon.FromHandle(IntPtr) Icon.FromHandle(IntPtr) Method

정의

지정한 창 핸들에서 아이콘(HICON)까지 GDI+ Icon을 만듭니다.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

매개 변수

handle
IntPtr IntPtr IntPtr IntPtr

아이콘의 창 핸들입니다.A Windows handle to an icon.

반환

이 메서드가 만드는 Icon입니다.The Icon this method creates.

예제

다음 코드 예제는 Windows Forms에서 사용 하도록 설계 되었으며 필요 PaintEventArgs e의 매개 변수는 Paint 이벤트 처리기입니다.The following code example is designed for use with Windows Forms, and it requires PaintEventArgse, which is a parameter of the Paint event handler. 이 코드에서는 다음 작업을 수행합니다.The code performs the following actions:

  • Bitmap을 만듭니다.Creates a Bitmap.

  • 화면에 해당 개체를 그립니다.Draws that object to the screen.

  • 아이콘 핸들을 가져옵니다는 Bitmap합니다.Gets an icon handle for the Bitmap.

  • 집합의 Form.Icon 생성 된 핸들에서 아이콘을 폼의 특성입니다.Sets the Form.Icon attribute of the form to an icon created from the handle.

  • Windows API 함수 DestroyIcon 를 호출 하 여 리소스를 해제 합니다.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

설명

이 메서드를 사용 하는 경우 리소스가 해제 되도록 Windows API의 메서드를 DestroyIcon 사용 하 여 원래 아이콘을 삭제 해야 합니다.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.

보안

SecurityPermission
비관리 코드에 액세스 합니다.for access to unmanaged code. 열거형 관련 UnmanagedCode합니다.Related enumeration UnmanagedCode.

적용 대상