Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for shell32iconsform.cs

(view source code of shell32iconsform.cs as plain text)

  1. using System;
  2. using System.Drawing;
  3. using System.Runtime.InteropServices;
  4. using System.Windows.Forms;
  5.  
  6.  
  7. namespace RobvanderWoude
  8. {
  9. 	public partial class Shell32IconsForm : Form
  10. 	{
  11. 		public static string progver = "2.02";
  12. 		public static int icon = 1;
  13. 		public static NotifyIcon systrayicon = new NotifyIcon( );
  14.  
  15.  
  16. 		public Shell32IconsForm( )
  17. 		{
  18. 			InitializeComponent( );
  19. 			string[] args = Environment.GetCommandLineArgs( );
  20. 			if ( args.Length > 1 )
  21. 			{
  22. 				ShowHelp( );
  23. 			}
  24. 		}
  25.  
  26.  
  27. 		static void ShowHelp( )
  28. 		{
  29. 			string title = String.Format( "Shell32Icons {0} \xA9 2019 Rob van der Woude", progver );
  30. 			string helptext = String.Format( "Shell32Icons,  Version {0}\nView all icons available in Shell32.dll\n\n", progver );
  31. 			helptext += "The selected icon is displayed in this program's title bar and main window, and in the System Tray (a.k.a. Notification Area).\n\n";
  32. 			helptext += "Code to extract icons from Shell32.dll by Thomas Levesque:\nhttp://stackoverflow.com/questions/6873026\n\n";
  33. 			helptext += "Written by Rob van der Woude\nhttp://www.robvanderwoude.com";
  34. 			MessageBox.Show( helptext, title, MessageBoxButtons.OK, MessageBoxIcon.None );
  35. 		}
  36.  
  37.  
  38. 		private void UpdateIcon( )
  39. 		{
  40. 			icon = Convert.ToInt32( numericUpDown.Value );
  41. 			Shell32IconsForm.ActiveForm.Icon = IconExtractor.Extract( "shell32.dll", icon, true );
  42. 			iconBox.BackgroundImage = IconExtractor.Extract( "shell32.dll", icon, true ).ToBitmap( );
  43. 			systrayicon.Icon = IconExtractor.Extract( "shell32.dll", icon, true );
  44. 			systrayicon.Visible = true;
  45. 		}
  46.  
  47.  
  48. 		private void Shell32IconsForm_FormClosing( object sender, FormClosingEventArgs e )
  49. 		{
  50. 			systrayicon.Visible = false;
  51. 			systrayicon.Dispose( );
  52. 		}
  53.  
  54.  
  55. 		private void numericUpDown_KeyUp( object sender, KeyEventArgs e )
  56. 		{
  57. 			UpdateIcon( );
  58. 		}
  59.  
  60.  
  61. 		private void numericUpDown_ValueChanged( object sender, EventArgs e )
  62. 		{
  63. 			UpdateIcon( );
  64. 		}
  65.  
  66.  
  67. 		private void Shell32IconsForm_HelpRequested( object sender, HelpEventArgs hlpevent )
  68. 		{
  69. 			ShowHelp( );
  70. 		}
  71.  
  72.  
  73. 		// Code to extract icons from Shell32.dll by Thomas Levesque
  74. 		// http://stackoverflow.com/questions/6873026
  75. 		public class IconExtractor
  76. 		{
  77.  
  78. 			public static Icon Extract( string file, int number, bool largeIcon )
  79. 			{
  80. 				IntPtr large;
  81. 				IntPtr small;
  82. 				ExtractIconEx( file, number, out large, out small, 1 );
  83. 				try
  84. 				{
  85. 					return Icon.FromHandle( largeIcon ? large : small );
  86. 				}
  87. 				catch
  88. 				{
  89. 					return null;
  90. 				}
  91.  
  92. 			}
  93.  
  94. 			[DllImport( "Shell32.dll", EntryPoint = "ExtractIconExW", CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall )]
  95. 			private static extern int ExtractIconEx( string sFile, int iIndex, out IntPtr piLargeVersion, out IntPtr piSmallVersion, int amountIcons );
  96. 		}
  97. 	}
  98. }
  99.  

page last uploaded: 2019-01-21, 22:48