Bitmap.GetHicon 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
返回图标的句柄。
public:
IntPtr GetHicon();
public IntPtr GetHicon ();
member this.GetHicon : unit -> nativeint
Public Function GetHicon () As IntPtr
返回
IntPtr
nativeint
图标的 Windows 句柄,其图像与 Bitmap相同。
例外
操作失败。
示例
下面的代码示例设计用于 Windows 窗体,它需要 PaintEventArgse
,这是 Paint 事件处理程序的参数。 该代码执行以下操作:
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
注解
最初,从句柄创建的图标的大小将与原始位图相同。