• Advertisement
Sign in to follow this  

LoadIcon

This topic is 4588 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
Load the icon with a call to LoadImage.
WNDCLASS WndClass;
//...
WndClass.hIcon = reinterpret_cast<HICON>(LoadImage(
NULL, "filename.ico", IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_LOADFROMFILE));
The icon should be destroyed when there are no windows or window classes that uses it anymore. This can be done with a call to DestroyIcon. It is automatically destroyed when the application terminates.

Share this post


Link to post
Share on other sites
ok, i changed it so it's now:
wc.hIcon = reinterpret_cast<HICON>(LoadImage(0, (LPCTSTR)m_Icon, IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_LOADFROMFILE));

but when i compile, the icon is still the default and there's no icon on the window... is there something else wrong?

Share this post


Link to post
Share on other sites
Check the return value of LoadImage. If it fails, it will return NULL. In that case, call GetLastError() to find out what went wrong.

Share this post


Link to post
Share on other sites
Icon = LoadImage(0, (LPCTSTR)m_Icon, IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_LOADFROMFILE);
DWORD Error = GetLastError();

Error is 2. what's that mean?

Share this post


Link to post
Share on other sites
Use this to translate it:

char buffer[256];
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,0,GetLastError(),MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),buffer,256,0);
MessageBox(hwnd,buffer,"Error",MB_OK|MB_ICONERROR);

Share this post


Link to post
Share on other sites
Quote:
Original post by EvilKnuckles666
it says "The handle is invalid"... why??


Your handle of the instance is '0', thus it will load an OEM image. Your application contains the image that you want to load.

hinst

Identifies an instance of the module that contains the image to be loaded. To load an OEM image, set this parameter to zero.

lpszName

Identifies the image to load.

If the hinst parameter is non-NULL and the fuLoad parameter does not include LR_LOADFROMFILE,
lpszName is a pointer to a null-terminated string that contains the name of the image resource in the hinst module.

If hinst is NULL and LR_LOADFROMFILE is not specified, the low-order word of this parameter must be the identifier of the OEM image to load.
The OEM image identifiers are defined in WINUSER.H and have the following prefixes:




Try changing the '0' to GetModuleHandle(NULL) and see if that works.

Share this post


Link to post
Share on other sites
ok, it worked now. but how do i get it so the display icon is the icon i want. like when i would run an actual game and it has it's own icon. know what i mean?

Share this post


Link to post
Share on other sites
Is your assignment to "Icon" the wc.hIcon structure member or just an outside variable? Also, it the icon you are loading coming straight from the game's exectuable or another executable?

If it is from the executable it is loading from, just do this:
wc.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));

"IDI_MYICON" is the ID for the icon that is loaded into your program during compile time.

Share this post


Link to post
Share on other sites
Then in your resource file, you need to stipulate the path so that during compiling, the icon itself is loaded into your executable. Then, when you release your program, you do not need to include that icon file since it is already compiled into your executable.

In your resource file:

//~~~~~Icon
IDI_PRGM ICON DISCARDABLE "icon.ico"


"icon.ico" is the name of your icon's file name.

Make sure to also include, within your resource header, the resource ID: "#define IDI_PRGM 105" <-105 is arbitrary also without the quotes.

Share this post


Link to post
Share on other sites
i had to make a new resouce file and this is what i have in it by default

//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by Project.rc

// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 101
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1001
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif

where should i put the icon stuff?

Share this post


Link to post
Share on other sites

//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by Project.rc

//Icon Resource IDs
#define IDI_PRGM 101

// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 102
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1001
#define _APS_NEXT_SYMED_VALUE 102
#endif
#endif


What is the name of the icon's file you are to load?

Share this post


Link to post
Share on other sites
In your resource file the *.rc extension, type the following:

IDI_PRGM ICON DISCARDABLE "Galaga Icon.ico"


In your windows class, define your icon file to be loaded when executed as the following:

wc.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_PRGM));

Share this post


Link to post
Share on other sites
Quote:
Icon = LoadImage(0, (LPCTSTR)m_Icon, IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_LOADFROMFILE);
DWORD Error = GetLastError();

Error is 2. what's that mean?

Error number 2 means that "The system cannot find the file specified." Make sure that m_Icon is the filename of an existing .ico-file. If the path is relative (like "subdir\filename.ico" or just "filename.ico"), it is relative to the working directory (which often, but not always, is the directory of the .exe).

If you want to embed the icon in your exe, then instead listen to Xiachunyi.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement