Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
The following topics describe how to perform certain tasks related to icons:
Creating an Icon
The following example creates two icon handles: one for the standard question icon and one for a custom icon included as a resource in the application's resource-definition file.
HICON hIcon1 = LoadIcon(NULL, IDI_QUESTION);
HICON hIcon2 = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MYICON));
Applications should implement custom icons as resources and use LoadIcon or LoadImage rather than create the icon at run time. Using icon resources avoids device dependence, simplifies localization, and enables applications to share icon designs.
CreateIcon can be used to create a custom monochrome icon at run time. Use the PackCursorMasks helper and symbol map from Creating a Cursor, then create an icon with one plane and one bit per pixel:
HICON hIcon = CreateIcon(hInstance, CURSOR_SIZE, CURSOR_SIZE, 1, 1, abAnd, abXor);
To create an alpha blended icon at run time, see Creating an Alpha Blended Cursor — pass TRUE for fIcon in ICONINFO to create an icon instead of a cursor.
Before closing, your application must use DestroyIcon to destroy any icon obtained from CreateIcon, CreateIconIndirect, CreateIconFromResourceEx without the LR_SHARED flag, or LoadImage without the LR_SHARED flag. Icons loaded with LoadIcon, or created or loaded with LR_SHARED, are shared system resources and must not be destroyed.
Getting the Icon size
See Getting a Cursor size. The example works identically for icons — pass an HICON in place of HCURSOR.
Displaying an Icon
Your application can load and create icons to display in the application's client area or child windows. The following example demonstrates how to draw an icon in the client area of the window whose device context (DC) is identified by the hdc parameter.
DrawIcon(hdc, 10, 20, hIcon1);
The system automatically displays the class icon(s) for a window. Your application can assign class icons while registering a window class. Your application can replace a class icon by using the SetClassLongPtr function. This function changes the default window settings for all windows of a given class. The following example replaces a class icon with the icon whose resource identifier is 480.
SetClassLongPtr(hWnd, GCLP_HICON,
(LONG_PTR)LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MYICON)));
For more information about window classes, see Window Classes.
Sharing Icon Resources
The following code uses the functions CreateIconFromResourceEx, DrawIcon, and LookupIconIdFromDirectoryEx, and several of the resource functions, to create an icon handle based on icon data from another executable file. Then, it displays the icon in a window.
Security Warning: Using LoadLibrary incorrectly can compromise the security of your application by loading the wrong DLL. Refer to the LoadLibrary documentation for information on how to correctly load DLLs with different versions of Windows.
int cx = GetSystemMetrics(SM_CXICON);
int cy = GetSystemMetrics(SM_CYICON);
// LoadLibraryExW with LOAD_LIBRARY_AS_DATAFILE avoids running the DLL/EXE.
// Always use a fully qualified path to prevent DLL planting.
HINSTANCE hExe = LoadLibraryEx(TEXT("C:\\path\\to\\myapp.exe"), NULL,
LOAD_LIBRARY_AS_DATAFILE);
if (hExe == NULL)
return;
HRSRC hResource = FindResource(hExe, MAKEINTRESOURCE(440), RT_GROUP_ICON);
HGLOBAL hMem = LoadResource(hExe, hResource);
BYTE *lpResource = LockResource(hMem);
int nID = LookupIconIdFromDirectoryEx(lpResource, TRUE, cx, cy, LR_DEFAULTCOLOR);
hResource = FindResource(hExe, MAKEINTRESOURCE(nID), RT_ICON);
hMem = LoadResource(hExe, hResource);
lpResource = LockResource(hMem);
HICON hIcon1 = CreateIconFromResourceEx(lpResource,
SizeofResource(hExe, hResource), TRUE, 0x00030000,
cx, cy, LR_DEFAULTCOLOR);
DrawIcon(hdc, 10, 20, hIcon1);
FreeLibrary(hExe);