Link to home
Start Free TrialLog in
Avatar of atomicgs12
atomicgs12Flag for United States of America

asked on

Add icon from file to Windows CE application

I have some custom icons(.ico) I want to load to my Windows CE/Mobile 6 application as it is running. I have tried LoadImage but found since then that LoadImage only works with an HINSTANCE in the first parameter for windows ce. Meaning LoadImage will only load icons from the compilied application and not from an outside source as needed in my case from a file.

I have my files located in a file on the device, e.g. c:\custom\icon1.ico.

I see many folks talk about this on the net but yet to find any example or pointers as to what function(s) are needed to make this work in ce.

I have seen some reference to functions such as SHLoadImageFile, SHGetFileInfo and etc. but have yet been able to get any of the above or others to work.

I will need some working example code to get this problem solved.

Thanks
Avatar of taabello
taabello

hi atom,

found this link and thought it might be helpful.
no official solution was accepted, but you might just try out the suggested options and you might get lucky.

http://social.msdn.microsoft.com/Forums/en-US/vssmartdevicesnative/thread/df6fcbc3-aad4-4a12-9e80-d3d76cf29671
SHLoadImageFile:
http://msdn.microsoft.com/en-us/library/bb416681.aspx
An example:
Loading Images with undocumented API in PPC 2002
http://www.codeproject.com/KB/mobile/shloadimage.aspx
If it's from a file, it makes a sense to switch to another format - JPEG, PNG,... if it is about WM 6.1, maybe to add the alpha-blending.
 
Avatar of atomicgs12

ASKER

thanks pqnatyuk -

I've used the SHLoadImageFile but even thought the documentation says it will take ico(icon) files it will not work for me. I can load up bmp images but I need a way to get the ico image loaded. Do you know of a way to convert a loaded bmp file to an ico?
I'm sure I used SHLoadImageFile to load the icons.
In order to test your code, I'd try to load the same icon from the application resources. If it will work, I will agree with you.

SHLoadImageFile is a function from Shell API. cab-files, homescreen used icon files for many years and use exactly this Shell API.

I've proposed another way for you - convert the files to other format.

As an option, you can try CxImage:
http://www.codeproject.com/KB/graphics/cximage.aspx?msg=2647331

For test (maybe you lose your icon when you draw it), you can check this code:
HMODULE hGWES = LoadLibraryEx( L"gwes.exe", NULL, LOAD_LIBRARY_AS_DATAFILE );
HICON hIcon = LoadIcon( hGWES, MAKEINTRESOURCE(MB_ICONQUESTION) );
Icons in Win32
http://msdn.microsoft.com/en-us/library/ms997538.aspx

In the worst case you can make your own function that will load the icons. This way will always work.
This link has the code.
pqnatyuk -

Your link "http://msdn.microsoft.com/en-us/library/ms997538.aspx" started out sounding like a good idea but after some time porting the code over, once one has the ReadIconFromICOFile then what? How does one get it to an HIMAGE or HICON and display it. I looked further and it has a call MakeIconFromResource but that function uses CreateIconFromResource which is not in CE.

I can load any of my icon from with in my resource/program. So I know they are good.

SHLoadImageFIle does not work
I can get an hicon handle from my file resource with SHGetFIleInfo but when I use SHNotifyIcon to add the icon I get an empty icon display, an icon with the default windows logo.

Any other ideas?
Try to draw the icon in the dialog, or in a static control. For a test.

I posted that link not because of this function that you cannot find, but because I wanted to show you the low-level format of the icon file. There are many examples reading the icon from the file and they do not use any strange API - plain C and simple Win32 that for sure works on CE. CreateIconIndirect creates the icon.

Please do not forget that the icon should have the correct size (SM_CXICON and SM_CYICON system metric values):
http://msdn.microsoft.com/en-us/library/aa926301.aspx

This is about Microsoft Windows CE:
Creating Icons, Bitmaps, Images, and Strings
http://msdn.microsoft.com/en-us/library/ms927657.aspx
If you decided to take the icon from the resource - it says how to do it.

SHLoadImageFIle? Do you mean SHLoadImageFile? It exists if you have aygshell.
http://msdn.microsoft.com/en-us/library/bb416681.aspx

"This function converts files of several types, including GIF (Graphics Interchange Format), PNG (Portable Network Graphics), JPG (Joint Photographic Experts Group), ICO (icon), and BMP (bitmap) file formats. Other image file types may be supported if the correct decoder is installed."

aygshell.h
aygshell.lib

Windows CE .NET 4.0 and later


"Please do not forget that the icon should have the correct size (SM_CXICON and SM_CYICON system metric values):
http://msdn.microsoft.com/en-us/library/aa926301.aspx"
Yes I know the sizes are correct because as I stated before I have taken my 'file' icons placed them in the .rc of the project, built it and the resource icons are displayed.

"This is about Microsoft Windows CE:
Creating Icons, Bitmaps, Images, and Strings
http://msdn.microsoft.com/en-us/library/ms927657.aspx
If you decided to take the icon from the resource - it says how to do it."
As I stated in my original request I need to add the icon from a 'FILE". I can do resource icon loading all day long. Loading from a FILE is the problem.

No 'SHLoadImageFIle" does NOT return a handle with GetLastError it returns an error 6 which equals "The handle is invalid. " Look up 'SHGetFileInfo http://msdn.microsoft.com/en-us/library/ms942615.aspx the SHFILEINFOR data will return an icon handle. Only when I place the icon handle in Shell_NotifyIcon it displays a default Windows icon, like an empty icon.

Have you ever used SHell_NotifyIcon.

Thanks
Yes. On PPC 2003 in 2005.
It works since CE 1.0:
http://msdn.microsoft.com/en-us/library/aa922175.aspx
http://msdn.microsoft.com/en-us/library/aa453686.aspx

The icon was in the app resources, the size was 16x16, LoadImage loaded that fine.

I used an article from Nancy Nicolaisen. Maybe this one:
Managing Shell Functionality
http://www.developer.com/net/net/article.php/2239131/Managing-Shell-Functionality.htm

In the SDK samples you can find TrayApp:
http://msdn.microsoft.com/en-us/library/bb158770.aspx

MSDN. Using the Microsoft .NET Compact Framework MessageWindow Class
http://msdn.microsoft.com/en-us/library/Aa446525.aspx

CodeProject.Adding Icons to the System Tray
http://www.codeproject.com/KB/shell/systemtray.aspx
pgnatyuk-
I really appreciate you responding to all my comments but your last message only delt with icons retrieved from resources, nothing abut icons retrieved from files.
I would suggest you create an icon, place it in a file location, run some app you have and try to retrieve and display the icon from the file location at run time in your app. If you can do that then you have solved solution else we are just going in circles. You talking about resource icons and I am talking about file icons.

Thanks
If you really need to load an icon from the ico-file, you'll do it yourself. I'm trying simply to help. I do not have (and cannot have) any personal interest about this task. I do not need to load any icon from any file right now. If I need I'd put the icon into the app resources. But I prefer to use the png-format everywhere.

Icons and this SHLoadImage is an old story.  I do see in Google now that many people complained about SHLoadImage and ico-format. SHLoadImageFile returns HBITMAP. Icon is a bit different format. But MSDN says that SHLoadImageFile load the icons.

You can try one more way. Here you may find an example.
CodePoject. Display PNG, JPG, etc. on Pocket PC
http://www.codeproject.com/KB/windows/png__jpg__etc_on_pocketpc.aspx
There is BOOL LoadICO(LPCTSTR lpszFileName) function.

I'd recommend simply switch to any other format if you have the image in an external file.
If you will download the code from that article, you will find CxImage library inside. There is the ICO-decoder in ximaico.cpp. You can use the full library or take only this code from
bool CxImageICO::Decode(CxFile *hFile).
Still researching this.
how about using ExtractIconEx() and putting icons in dll? otherwise use CxImage library
ASKER CERTIFIED SOLUTION
Avatar of pgnatyuk
pgnatyuk
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial