Microsoft KB Archive/259673

= How To Change the Application Icon with Visual Basic =

Article ID: 259673

Article Last Modified on 6/29/2004

-

APPLIES TO


 * Microsoft Visual Basic 4.0 Standard Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 32-Bit Enterprise Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q259673



SUMMARY
Sometimes you may want to change the icon associated with an application. This application icon is what appears when you use the ALT+TAB key combination to switch between applications that are currently running on your computer. The icon can be changed by using the SendMessage API function.



MORE INFORMATION
All Visual Basic applications have an invisible top-level window that processes events and messages. In design mode, the window is ThunderMain, in run mode, the window is ThunderRT6Main. The RT6 reflects the version of Visual Basic that is being used. Both windows have the name of the project, or the executable name as the window caption. This is where the application icon is set.

You need to use the GetWindowLong function to walk up the window chain until the function returns a 0, indicating that there are no more parent windows. Alternatively, you could search for ThunderRT6Main with the FindWindow API function, but there might be more than one instance of your application. (FindWindow searches for a window based on it's window class name and/or the window text. If there are two instances of the application, both windows are exactly the same. Therefore, it's not certain that you always find the one that you are actually working with.)

The following Visual Basic sample demonstrates this.

When you set the Icon property, you can get the source Icon in several ways:  By using the Picture property of a PictureBox control (as demonstrated later).

  By using the LoadPicture function to load the Icon from an .ico file. For example: Set Me.Icon = LoadPicture(&quot;C:\Program Files\Microsoft Visual &quot; _     & &quot;Studio\Common\Graphics\Icons\Misc\BULLSEYE.ICO&quot;)   By using the LoadResPicture function to load the Icon from a resource (.res) file. For example: Set Me.Icon = LoadResPicture(101, vbResIcon) 

Step-by-Step Example
 Start a new Standard EXE project in Visual Basic. Form1 is created by default. From the Project menu, add a new module to the project.  Add the following API declarations to Module1: Option Explicit

Public Declare Function GetWindowLong Lib &quot;user32&quot; Alias &quot;GetWindowLongA&quot; _ (ByVal hWnd As Long, ByVal nIndex As Long) As Long Public Declare Function SendMessage Lib &quot;user32&quot; Alias &quot;SendMessageA&quot; _ (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Public Declare Function LoadIcon Lib &quot;user32&quot; Alias &quot;LoadIconA&quot; _ (ByVal hInstance As Long, lpIconName As Any) As Long

Public Const GWL_HWNDPARENT = (-8)

Public Const WM_GETICON = &H7F Public Const WM_SETICON = &H80

Public Const ICON_SMALL = 0 Public Const ICON_BIG = 1 </li> Click on Form1 and set an icon in the Properties window. This becomes the default application icon.</li> Add a PictureBox control to Form1.</li> Click the Picture property and click the ellipsis (...) button. This allows you to choose which picture you want to put in the PictureBox. Choose an icon (.ico) file extension.</li> Add a CommandButton control to Form1.</li>  Add the following code to the code window of Form1: Private nRet        As Long Private nMainhWnd   As Long

Private Sub Form_Load nRet = GetWindowLong(Me.hWnd, GWL_HWNDPARENT) Do While nRet nMainhWnd = nRet nRet = GetWindowLong(nMainhWnd, GWL_HWNDPARENT) Loop End Sub

Private Sub Command1_Click Dim hIcon As Long ' set the icon Set Me.Icon = Picture1.Picture ' get a handle to ICON_BIG hIcon = SendMessage(Me.hWnd, WM_GETICON, ICON_BIG, ByVal 0) ' send ICON_BIG to the main window SendMessage nMainhWnd, WM_SETICON, ICON_BIG, ByVal hIcon End Sub </li> Compile the project and run the new executable file. Press the ALT+TAB key combination to see what your application icon is. If you are testing this in the Visual Basic IDE, note that the default Microsoft Windows icon appears.</li> Click the CommandButton and press ALT+TAB again. Note that the application icon has changed.</li></ol>

Keywords: kbhowto kbicon kbapi KB259673

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.