943,832 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 1570
  • VB.NET RSS
Jul 23rd, 2009
0

Reading Icons from an Icon Library

Expand Post »
Ive made an icon library for my program, so that I dont have my program depend on Windows for icons. I want to know how I can have my program use the icons from that dll file instead of from individual icons that I have in the program directory.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
Tamir09 is offline Offline
27 posts
since Feb 2009
Jul 23rd, 2009
0

Re: Reading Icons from an Icon Library

This code should do it:

VB Syntax (Toggle Plain Text)
  1. Imports System
  2. Imports System.Collections.Generic
  3. Imports System.ComponentModel
  4. Imports System.Data
  5. Imports System.Drawing
  6. Imports System.Text
  7. Imports System.Windows.Forms
  8. Imports System.Runtime.InteropServices
  9.  
  10. Namespace ExtractIcon
  11. Partial Public Class Form1
  12. Inherits Form
  13. ' Constants that we need in the function call
  14. Private Const SHGFI_ICON As Integer = &H100
  15. Private Const SHGFI_SMALLICON As Integer = &H1
  16. Private Const SHGFI_LARGEICON As Integer = &H0
  17.  
  18. ' This structure will contain information about the file
  19. Public Structure SHFILEINFO
  20. ' Handle to the icon representing the file
  21. Public hIcon As IntPtr
  22. ' Index of the icon within the image list
  23. Public iIcon As Integer
  24. ' Various attributes of the file
  25. Public dwAttributes As UInteger
  26. ' Path to the file
  27. <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=256)> _
  28. Public szDisplayName As String
  29. ' File type
  30. <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> _
  31. Public szTypeName As String
  32. End Structure
  33. ' The signature of SHGetFileInfo (located in Shell32.dll)
  34.  
  35. <DllImport("Shell32.dll")> _
  36. Public Shared Function SHGetFileInfo(ByVal pszPath As String, ByVal dwFileAttributes As UInteger, ByRef psfi As SHFILEINFO, ByVal cbFileInfo As Integer, ByVal uFlags As UInteger) As IntPtr
  37. End Function
  38.  
  39. Public Sub New()
  40. InitializeComponent()
  41. End Sub
  42.  
  43. Private Sub btnExtract_Click(ByVal sender As Object, ByVal e As EventArgs)
  44. ' Will store a handle to the small icon
  45. Dim hImgSmall As IntPtr
  46. ' Will store a handle to the large icon
  47. Dim hImgLarge As IntPtr
  48. Dim shinfo As New SHFILEINFO()
  49. ' Open the file that we wish to extract the icon from
  50. If openFile.ShowDialog() = DialogResult.OK Then
  51. ' Store the file name
  52. Dim FileName As String = openFile.FileName
  53. ' Store the icon in this myIcon object
  54. Dim myIcon As System.Drawing.Icon
  55. ' Get a handle to the small icon
  56. hImgSmall = SHGetFileInfo(FileName, 0, shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON Or SHGFI_SMALLICON)
  57. ' Get the small icon from the handle
  58. myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon)
  59. ' Display the small icon
  60. picIconSmall.Image = myIcon.ToBitmap()
  61. ' Get a handle to the large icon
  62. hImgLarge = SHGetFileInfo(FileName, 0, shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON Or SHGFI_LARGEICON)
  63. ' Get the large icon from the handle
  64. myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon)
  65. ' Display the large icon
  66. picIconLarge.Image = myIcon.ToBitmap()
  67. End If
  68. End Sub
  69. End Class
  70. End Namespace
Reputation Points: 10
Solved Threads: 2
Newbie Poster
djjaries is offline Offline
6 posts
since Mar 2008
Jul 23rd, 2009
0

Re: Reading Icons from an Icon Library

Would that be able to work with my custom icon library, rather than the Windows Icon library? The icons Im using are only found in Vista, and I want this to work in any OS.
Reputation Points: 10
Solved Threads: 0
Light Poster
Tamir09 is offline Offline
27 posts
since Feb 2009
Jul 23rd, 2009
0

Re: Reading Icons from an Icon Library

Also, Im getting errors that OpenFile.Filename is not available. And the picIcons are not declared? I dont even know why that is giving me an error.
Reputation Points: 10
Solved Threads: 0
Light Poster
Tamir09 is offline Offline
27 posts
since Feb 2009
Jul 23rd, 2009
0

Re: Reading Icons from an Icon Library

Quote ...
Also, Im getting errors that OpenFile.Filename is not available. And the picIcons
The code doesn't work "as is". You have to modify it for your purposes like you do with any other sample code. Just drop OpenFileDialog and Picture Box controls on the form.

I didn't test djjaries's code but I've used the same API call to extract icons. It works at least with XP and Vista. Never tested or checked that API call with any pre-XP OS or with Win7 RC1. Instead of API call there's also a .NET way to do it.

Quote ...
Ive made an icon library for my program
That DLL is called a satellite assembly and you access it with ResourceManager class. Satellite assembly and how to use it is explained very well (with sample code) in MSDN .NET framework developer's guide article Retrieving Resources in Satellite Assemblies.
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Jul 25th, 2009
0

Re: Reading Icons from an Icon Library

Ive manipulated the code to get it to work, but sort of. I still need it to use the icon I tell it, as the icon of the program, and not just display the icon. Also, its not supposed to be a user selected icon, this has to be hard coded.
Reputation Points: 10
Solved Threads: 0
Light Poster
Tamir09 is offline Offline
27 posts
since Feb 2009
Jul 25th, 2009
0

Re: Reading Icons from an Icon Library

I guess you didn't use ResourseManager.

Quote ...
its not supposed to be a user selected icon, this has to be hard coded.
Assuming your assembly ends up to same directory as the main executable, you get the path to your dll in this way
VB.NET Syntax (Toggle Plain Text)
  1. Dim StartUpFolder As String
  2. ' Get the path where your app starts
  3. StartUpFolder = Application.StartupPath
  4. Dim MyDLLFile As String
  5. ' Append your dll's name to path
  6. MyDLLFile = StartUpFolder & "\myicondll.DLL"
Quote ...
I still need it to use the icon I tell it, as the icon of the program
Once you've managed to load the right icon from the dll, assign it to (main) form's icon property
VB.NET Syntax (Toggle Plain Text)
  1. Dim myIcon As System.Drawing.Icon
  2. ' DEBUG: Load from the file
  3. myIcon = New Icon("D:\icon.ico")
  4.  
  5. ' Set the icon as the app's (i.e. main form's) icon
  6. Me.Icon = myIcon

HTH
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Aug 4th, 2009
0

Re: Reading Icons from an Icon Library

still didnt work after all that, it wont load the icon from the dll, and gives me an error. I'll try to mess around some more.
Reputation Points: 10
Solved Threads: 0
Light Poster
Tamir09 is offline Offline
27 posts
since Feb 2009
Aug 5th, 2009
0

Re: Reading Icons from an Icon Library

Quote ...
The icons Im using are only found in Vista, and I want this to work in any OS.
You did write your own icon library, right? Are you sure your DLL contains valid resources i.e. ico-files?

I still suggest taking a look at Retrieving Resources in Satellite Assemblies. With ResourceManager class you should have a code that works in any platform that supports .NET Framework.
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: SQL Datareader Skips First Row
Next Thread in VB.NET Forum Timeline: simple collision detection





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC