Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load window icon dynamically

Tags:

c++

winapi

When registering a window class WNDCLASSEX wcex, I use wcex.hIcon = LoadIcon( hInstance, (LPCTSTR) IDI_APPLICATION ) to set the icon of the window.

Is there a way to dynamically load the icon from a file for registering the window? Something like LoadIcon ( hInstance, "iconfile.ico" ) or may be create the Icon Resource using the file.

like image 624
Bhupen Avatar asked Nov 02 '12 00:11

Bhupen


1 Answers

You can use LoadImage:

wcex.hIcon = (HICON) LoadImage( // returns a HANDLE so we have to cast to HICON
  NULL,             // hInstance must be NULL when loading from a file
  "iconfile.ico",   // the icon file name
  IMAGE_ICON,       // specifies that the file is an icon
  0,                // width of the image (we'll specify default later on)
  0,                // height of the image
  LR_LOADFROMFILE|  // we want to load a file (as opposed to a resource)
  LR_DEFAULTSIZE|   // default metrics based on the type (IMAGE_ICON, 32x32)
  LR_SHARED         // let the system release the handle when it's no longer used
);

Make sure to either set wcex.hIconSm (small icon) to NULL or load a small icon. When you set it to NULL, it will use the image specified by hIcon automatically. When you load a small icon with LoadImage, you should set the width and height to 16 and remove the LR_DEFAULTSIZE flag. If it's an icon designed to have transparent parts, add the LR_LOADTRANSPARENT flag

like image 110
Anonymous Coward Avatar answered Oct 21 '22 17:10

Anonymous Coward