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 Forms 使用而設計,而且需要 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
方法來處置原始圖示,以確保資源已釋放。