PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

help me display an icon from a file

I'm very new to the windows resources, messaging, and painting. I've been trying to use LoadImage() to load the icon from a file. Someone please point me to an example or give me some information on loading and displaying icons.
Are you trying to get an icon from an external EXE or DLL, or from the EXE you are writing? If you want a handle to an icon in your program's resources, then use LoadIcon or LoadImage. If you want to extact icons directly form other programs, then use ExtractIconEx.

Dan East
I'm hoping it is possible to load the icons from a myIcon.ico file. I would not like to be limited to a finite # of icons in a DLL or EXE.
DLLs and EXEs can store thousands of icons each if so desired. ExtractIconEx will work with .ico files as well.

Dan East
thank you for the help
I'm having trouble getting an Icon handle.
Is my path wrong?

Code:

phIcon = ExtractIconEx(TEXT("\Program File\TodayView\Icons\Books.ico"), 0, NULL, NULL, 1);


phIcon is returning NULL, I've tried a lot of different combinations for the path & I've checked that the other parameters are correct[/code]
The backslash character is actually an escape code in C / C++, and consumes the character that follows it. Some examples:

\n = new line
\r = carriage return
\t = tab
\x010 = ascii character 0x10
\" = "
\\ = \

You need to double your backslashes if you want them to translate into a real backslash:
TEXT("\\Program Files\\TodayView\\Icons\\Books.ico")

Also, "Program Files" is plural. You forgot the s.

Dan East
Thanks Dan for pointing out the errors
I still have an error though:
Code:

HICON phIcon;
...
phIcon = ExtractIconEx(TEXT("\\Program Files\\TodayView\\Icons\\books.ico"), 0, NULL, NULL, 1);
if (phIcon != NULL)
{
if (!DrawIconEx(hdc, 0, phIcon, 0, 0, 0, NULL, DI_NORMAL))
_ltoa(GetLastError(), buffer, 10);
str = buffer;
DrawText(hdc, str, str.GetLength(), &rt;, DT_RIGHT);
DestroyIcon(phIcon);
}
else
{
_ltoa(GetLastError(), buffer, 10);
str = buffer;
DrawText(hdc, str, str.GetLength(), &rt;, DT_RIGHT);
}

phIcon is NULL, and the error is 1814 - The specified resource name cannot be found in the image file.

I've also tried many combinations:
Code:

ExtractIconEx(TEXT("\\Program Files\\TodayView\\Icons\\books.ico"), 0, phIcon, NULL, 1);

phIcon returned not NULL, but when I tried to DrawIconEx() I got 1414 - Invalid icon handle

Displaying an icon is becoming such a pain in the ...
You were close with that second example, except you weren't passing a pointer to a handle.

Try combining lines 3 and 4 as follows:
Code:

if (ExtractIconEx(TEXT("\\Program Files\\TodayView\\Icons\\books.ico"), 0, NULL, &phIcon;, 1))


Dan East
I added the suggestion but

Code:

if (ExtractIconEx(TEXT("\\Program Files\\TodayView\\Icons\\books.ico"), 0, NULL, &phIcon;, 1))
{
if (!DrawIconEx(hdc, 0, 0, phIcon, 0, 0, 0, NULL, DI_NORMAL))
...
}


Returns the same error code as above 1814.

Code:

ExtractIconEx(TEXT("\\Program Files\\TodayView\\Icons\\books.ico"), 0, NULL, &phIcon;, 1);
if (phIcon != NULL)
{
{
if (!DrawIconEx(hdc, 0, 0, phIcon, 0, 0, 0, NULL, DI_NORMAL))
...
}


In this code DrawIconEx() returns error 1414 as stated above (Invalid icon handle).

I've tried different icon files to be sure that wasn't the problem. I've tried simpler paths to the .ico file. I've searched for examples too. Any other suggestions? I appreciate your help.
You are trying to load an icon and not a bitmap resource, correct? Is the icon 16x16 or 32x32? Try extracting an icon from an exe instead of an ico file to see if that works.

Dan East
Without any change to the code (besides the path to the file) it extracted a 32x32 icon from a .dll. It appears to have a problem with .ico files.
Well, I'm not exactly sure the CE version supports ico files. According to the Windows CE API docs:
"This function retrieves the handle from the specified executable file or dynamic-link library (DLL)."

That differs from the description in MSDN for the regular Windows API. I have never tried extracting from ico files before. Also, if the icon you are trying to extract is 32x32, then pass the pointer to the handle as the 3rd parameter instead of the 4th.

Dan East
I don't know if you are fimiliar with the newly released DoobloDate but the PPC App allows users to create icon files and use them in the product. I wonder how they accomplished this?