Microsoft KB Archive/811400

= How to support multiple bitmap icons for Visual Basic .NET or Visual Basic 2005 applications =

Article ID: 811400

Article Last Modified on 11/13/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-





IN THIS TASK

 * SUMMARY
 * IconEx Class
 * Test IconEx Class
 * REFERENCES
 * REFERENCES



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
 Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. On the File menu, point to New, and then click Project. Under Project Types, click to select Visual Basic Projects.

Note In Visual Studio 2005, click Visual Basic under Project Types. Under Templates, click to select Class Library. Name the project MyIconExClassLibrary, type C:\ in the Location box, and then click OK.  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 &quot;user32&quot; _ Alias &quot;GetDC&quot; (ByVal hwnd As Int32) As Int32

Private Declare Function GetDeviceCaps Lib &quot;gdi32&quot; _ Alias &quot;GetDeviceCaps&quot; (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 & &quot;\&quot; & resolution.ToString & &quot;\&quot; & fileName) Catch Try Return New Icon(iconRootDirectory & &quot;\default\&quot; & fileName) Catch Return Nothing End Try End Try End Function End Class  In Solution Explorer, right-click MyIconExClassLibrary, and then click Add Reference.</li> Click the .NET tab, and then click to select System.Drawing.dll.</li> Click Select, and then click OK.

Note In Visual Studio 2005, you do not have to click Select.</li> On the Build menu, click Build Solution.</li></ol>

back to the top

Test IconEx Class
<ol> Create a subfolder named C:\IconDir.</li> Under C:\IconDir, create subfolders for various screen resolutions, and then name the folders 16, 24 , 32 , and others.</li> Create a subfolder under C:\IconDir named default.

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

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

The icon is assigned to the corresponding screen resolution.</li></ol>

back to the top

<div class="references_section">