Loading an icon from a file (Win32)
I’m always perturbed by how difficult it is to load an icon in Win32 at times. So many HICON/HANDLE and whatnot special data members and folks on forums doing strange stuff. Here’s a snippet of code that just loads a simple icon file and displays it as your executable icon. I’m sure there is a more correct/newer/snazzier way to do this – but this works fine for me…
LPCTSTR iconPathName= L"../MyIcon.ico";
UINT icon_flags = LR_LOADFROMFILE | LR_DEFAULTSIZE;
HANDLE hIcon = LoadImage(hInstance, iconPathName, IMAGE_ICON, 0, 0, icon_flags);
// set up RegisterClass struct
wcex.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = (HICON) hIcon;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = m_AppTitle.c_str();
// register the window class
return RegisterClassEx(&wcex);