How to support multiple bitmap icons for Visual Basic .NET or Visual Basic 2005 applications (811400)



The information in this article applies to:

  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)

SUMMARY

This step-by-step article describes how to support multiple icons for your Microsoft Visual Basic .NET or Microsoft Visual Basic 2005 applications, based on the screen resolution of the computer. Typically, the icon that the application uses is fixed. The same icon is used for any resolution of the screen. You may create a requirement to load different icons, based on the resolution of the screen. This article describes how to find the screen resolution, and then load corresponding icons.

back to the top

IconEx Class

  1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
  2. On the File menu, point to New, and then click Project.
  3. Under Project Types, click to select Visual Basic Projects.

    Note In Visual Studio 2005, click Visual Basic under Project Types.
  4. Under Templates, click to select Class Library.
  5. Name the project MyIconExClassLibrary, type C:\ in the Location box, and then click OK.
  6. Replace the existing code with the following code:
    Option Strict On
    Option Explicit On 
    
    ' Extended Icon Class
    Imports System.IO
    Imports System
    Imports System.Drawing
    
    Public Class IconEx
    
       Private Declare Function GetDC Lib "user32" _
       Alias "GetDC" (ByVal hwnd As Int32) As Int32
    
       Private Declare Function GetDeviceCaps Lib "gdi32" _
       Alias "GetDeviceCaps" (ByVal hdc As Int32, ByVal nIndex As Int32) As Int32
    
       Private Const BITSPIXEL As Integer = 12         '  Number of bits per pixel
    
       Private iconRootDirectory As String
       Private fileName As String
    
       ' A public constructor that accepts absolute path of icon directory
       ' and the name of the icon file. The name of the icon file should be
       ' unique for icon files for all resolutions. 
       Sub New(ByVal iconDirPath As String, ByVal iconFileName As String)
          iconRootDirectory = iconDirPath
          fileName = iconFileName
       End Sub
    
       ' This public method returns the corresponding icon for the resolution.
       ' The icon in the default directory is used when no icon exists for the 
       ' current resolution.
       Public Function GetIcon() As Icon
          Dim resolution As Integer
          resolution = GetDeviceCaps(GetDC(Nothing), BITSPIXEL)
          Try
             Return New Icon(iconRootDirectory & "\" & resolution.ToString() & "\" & fileName)
          Catch
             Try
                Return New Icon(iconRootDirectory & "\default\" & fileName)
             Catch
                Return Nothing
             End Try
          End Try
       End Function
    End Class
    
    
  7. In Solution Explorer, right-click MyIconExClassLibrary, and then click Add Reference.
  8. Click the .NET tab, and then click to select System.Drawing.dll.
  9. Click Select, and then click OK.

    Note In Visual Studio 2005, you do not have to click Select.
  10. On the Build menu, click Build Solution.
back to the top

Test IconEx Class

  1. Create a subfolder named C:\IconDir.
  2. Under C:\IconDir, create subfolders for various screen resolutions, and then name the folders 16, 24, 32, and others.
  3. Create a subfolder under C:\IconDir named default.

    You use the icon in this subfolder when no icon exists for the present screen resolution.
  4. Save the icon files in the corresponding C:\IconDir subfolders, based on the resolution.
  5. Make the icon file name unique for each of the resolutions (for example, AppIcon.ico).
  6. On the File menu, point to Add Project, and then click New Project.
  7. Under Project Types, click to select Visual Basic Projects.

    Note In Visual Studio 2005, click Visual Basic under Project Types.
  8. Under Templates, click to select Windows Application.
  9. Name the project MyIconExTestApplication, enter C:\ in the Location box, and then click OK.
  10. Add the following code to the constructor of the Windows form:
    Dim myIcon As New MyIconExClassLibrary.IconEx("C:\IconDir", "AppIcon.ico")
    Me.Icon = myIcon.GetIcon()
  11. In Solution Explorer, click Set as StartUp Project.
  12. In Solution Explorer, right-click MyIconExTestAplication, and then click Add Reference.
  13. Click the Projects tab, click to select MyIconExClassLibrary, click Select, and then click OK.
  14. On the Debug menu, click Start.

    The icon is assigned to the corresponding screen resolution.
back to the top

REFERENCES

For more information about icons, visit the following MSDN Web site:back to the top

Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbvs2005applies kbvs2005swept kbIcon kbBitmap kbHOWTOmaster KB811400 kbAudDeveloper kbAudITPRO