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)
SUMMARYThis 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
topIconEx 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 "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
- In Solution Explorer, right-click
MyIconExClassLibrary, and then click Add
Reference.
- Click the .NET tab, and then click to
select System.Drawing.dll.
- Click Select, and then click
OK.
Note In Visual Studio 2005, you do not have to click Select. - On the Build menu, click Build
Solution.
back to the topTest IconEx Class- Create a subfolder named
C:\IconDir.
- Under C:\IconDir, create subfolders for
various screen resolutions, and then name the folders
16, 24,
32, and others.
- Create a subfolder under C:\IconDir named
default.
You use the icon in this subfolder when no icon exists for the present screen
resolution. - Save the icon files in the corresponding
C:\IconDir subfolders, based on the resolution.
- Make the icon file name
unique for each of the resolutions (for example,
AppIcon.ico).
- On the File menu, point to Add
Project, and then click New 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
Windows Application.
- Name the project
MyIconExTestApplication, enter
C:\ in the Location box, and then click
OK.
- 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() - In Solution Explorer, click Set as StartUp
Project.
- In Solution Explorer, right-click
MyIconExTestAplication, and then click Add
Reference.
- Click the Projects tab, click to select
MyIconExClassLibrary, click Select, and then
click OK.
- On the Debug menu, click
Start.
The icon is assigned to the corresponding
screen resolution. back to the
topREFERENCESFor more information about icons, visit the following MSDN
Web site: back to the
top
Modification Type: | Minor | Last Reviewed: | 10/3/2006 |
---|
Keywords: | kbvs2005applies kbvs2005swept kbIcon kbBitmap kbHOWTOmaster KB811400 kbAudDeveloper kbAudITPRO |
---|
|