-
Creates a Bitmap.
-
Draws that object to the screen.
-
Gets an icon handle for the Bitmap.
-
Sets the Form.Icon attribute of the form to an icon created from the 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:/Blue hills.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;
- // Destroy the Icon, since the form creates
- // its own copy of the icon.
- DestroyIcon(newIcon.Handle);
- }
这是msdn中的方法,它有一个不好的地方就是需要一个PaintEventArgs 参数,我不明白微软为什么要把它搞得这么复杂,
以下是我修改的方法,去掉了参数,只要把它写到form的构造函数中就可以了。
- [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = CharSet.Auto)]
- extern static bool DestroyIcon(IntPtr handle);
- private void GetHicon_Example()
- {
- // Create a Bitmap object from an image file.
- Bitmap myBitmap = new Bitmap(@"c:/Blue hills.jpg");
- // Draw myBitmap to the screen.
- this.CreateGraphics().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;
- // Destroy the Icon, since the form creates
- // its own copy of the icon.
- DestroyIcon(newIcon.Handle);
- }
动态设置form的icon
最新推荐文章于 2024-07-20 00:38:25 发布