Icon.FromHandle(IntPtr) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
从指定的 Windows 句柄创建 GDI+ 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
nativeint
图标的 Windows 句柄。
返回
此方法创建的 Icon。
示例
下面的代码示例设计用于 Windows 窗体,它需要 PaintEventArgse
,这是 Paint 事件处理程序的参数。 该代码执行以下操作:
创建 Bitmap。
将该对象绘制到屏幕。
获取 Bitmap的图标句柄。
将窗体的 Form.Icon 属性设置为从句柄创建的图标。
调用 Windows API 函数
DestroyIcon
释放资源。
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
方法释放原始图标,以确保释放资源。