This article was previously published under Q259673
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.
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("C:\Program Files\Microsoft Visual " _ & "Studio\Common\Graphics\Icons\Misc\BULLSEYE.ICO")
By using the LoadResPicture function to load the Icon from a resource (.res) file. For example:
Set Me.Icon = LoadResPicture(101, vbResIcon)
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 ExplicitPublic Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _ (ByVal hWnd As Long, ByVal nIndex As Long) As LongPublic Declare Function SendMessage Lib "user32" Alias "SendMessageA" _ (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As LongPublic Declare Function LoadIcon Lib "user32" Alias "LoadIconA" _ (ByVal hInstance As Long, lpIconName As Any) As LongPublic Const GWL_HWNDPARENT = (-8)Public Const WM_GETICON = &H7FPublic Const WM_SETICON = &H80Public Const ICON_SMALL = 0Public Const ICON_BIG = 1
Click on Form1 and set an icon in the Properties window. This becomes the default application icon.
Add a PictureBox control to Form1.
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.
Add a CommandButton control to Form1.
Add the following code to the code window of Form1:
Private nRet As LongPrivate nMainhWnd As LongPrivate Sub Form_Load() nRet = GetWindowLong(Me.hWnd, GWL_HWNDPARENT) Do While nRet nMainhWnd = nRet nRet = GetWindowLong(nMainhWnd, GWL_HWNDPARENT) LoopEnd SubPrivate 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
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.
Click the CommandButton and press ALT+TAB again. Note that the application icon has changed.