The LoadImage() is not working

/* the code seems correct but is not working.*/
hIcon = LoadImage(NULL,(LPCWSTR)"icon.ico",IMAGE_ICON,32,32,LR_LOADFROMFILE);

if(hIcon)
{
SendMessage(hwnd,WM_SETICON,ICON_BIG,(LPARAM)hIcon);
}
else
{
MessageBox(NULL,TEXT("Problem while loading the Icon."), TEXT("EROOR !!!!"),MB_OK);
}

Errors:
1. The LoadImage() returns HWND, so cant cast to HICON.
2. If I cast it to HICON than also it is not working.
3. If I dont cast the second param than casting problem.
4. finally the icon is just not being loaded. I have kept the .ico file in the directory where VC++ keeps all the .h,.c etc files.


Thanks in advance,
Last edited on
Try:
1
2
3
4
5
6
7
8
9
10
hIcon = LoadImage(NULL,  L"icon.ico",IMAGE_ICON,32,32,LR_LOADFROMFILE); //changed here

if(hIcon)
{
SendMessage(hwnd,WM_SETICON,ICON_BIG,(LPARAM)hIcon);
}
else
{
MessageBox(NULL,TEXT("Problem while loading the Icon."), TEXT("EROOR !!!!"),MB_OK);
}



See if that makes a difference...
Last edited on
thanks,


but it is also not working, still same problem. . . .
That's weird. Are you sure you have a valid .ico file? Does it contain a 32x32 px icon?
closed account (z05DSL3A)
The following should tell you what the error is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <strsafe.h>
//...

hIcon = LoadImage(NULL,  L"icon.ico",IMAGE_ICON,32,32,LR_LOADFROMFILE); //changed here

if(hIcon)
{
    SendMessage(hwnd,WM_SETICON,ICON_BIG,(LPARAM)hIcon);
}
else
{
    LPVOID lpMsgBuf;
    LPVOID lpDisplayBuf;
    DWORD dw = GetLastError(); 

    FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER | 
        FORMAT_MESSAGE_FROM_SYSTEM |
        FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL,
        dw,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPTSTR) &lpMsgBuf,
        0, NULL );

    lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT, 
                                     (lstrlen((LPCTSTR)lpMsgBuf) + 40) * 
                                     sizeof(TCHAR)); 
    
    StringCchPrintf((LPTSTR)lpDisplayBuf, 
                    LocalSize(lpDisplayBuf) / sizeof(TCHAR),
                    TEXT("LoadImage() failed with error %d: %s"), dw, lpMsgBuf); 
    
    MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK); 

    LocalFree(lpMsgBuf);
    LocalFree(lpDisplayBuf);
}
Topic archived. No new replies allowed.