Jesse Moreland
Jesse Moreland

Reputation: 443

Getting my Icon to work

I made an Icon in resource editor using winapi C++ but it shows up as the red error X which I'm assuming that happens because its not loading my icon correctly.

Because of how many lines of code my program is, I'll spare you and only show you where I load the icon, or try to.

 wndclass.cbSize         = sizeof(wndclass);
      wndclass.style         = CS_HREDRAW | CS_VREDRAW;
      wndclass.lpfnWndProc   = WndProc;
      wndclass.cbClsExtra    = 0;
      wndclass.cbWndExtra    = 0;
      wndclass.hInstance     = hInstance;

      hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));
  wndclass.hIcon         = LoadIcon (hInstance, MAKEINTRESOURCE(IDI_ICON1));

  hMenu = LoadMenu(hInstance, MAKEINTRESOURCE(IDR_MENU1));

  wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW);
  wndclass.hbrBackground = static_cast<HBRUSH>(GetStockObject (WHITE_BRUSH));
  wndclass.lpszMenuName  = NULL;
  wndclass.lpszClassName = szAppName;
  wndclass.hIconSm       = LoadIcon(NULL, MAKEINTRESOURCE(IDI_ERROR));

  RegisterClassEx (&wndclass);

The .rc file

// Microsoft Visual C++ generated resource script.
//
#include "resource.h"

#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "afxres.h"

/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS

/////////////////////////////////////////////////////////////////////////////
// English (United States) resources

#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US

#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//

1 TEXTINCLUDE 
BEGIN
    "resource.h\0"
END

2 TEXTINCLUDE 
BEGIN
    "#include ""afxres.h""\r\n"
    "\0"
END

3 TEXTINCLUDE 
BEGIN
    "\r\n"
    "\0"
END

#endif    // APSTUDIO_INVOKED


/////////////////////////////////////////////////////////////////////////////
//
// Menu
//

IDR_MENU1 MENU
BEGIN
    POPUP "&File"
    BEGIN
        MENUITEM "&Erase",                      ID_FILE_ERASE
        MENUITEM "E&xit",                       ID_FILE_EXIT
    END
    POPUP "&Animals"
    BEGIN
        MENUITEM "&Dog",                        ID_ANIMALS_DOG
        MENUITEM "&Cat",                        ID_ANIMALS_CAT
    END
    POPUP "&Date Info"
    BEGIN
        MENUITEM "Current &Date",               ID_DATEINFO_CURRENTDATE
        MENUITEM "Current &Time",               ID_DATEINFO_CURRENTTIME
    END
    POPUP "&Break"
    BEGIN
        MENUITEM "&Sound",                      ID_BREAK_SOUND
        MENUITEM "S&hapes",                     ID_BREAK_SHAPES
    END
    POPUP "&Help"
    BEGIN
        MENUITEM "&System Info",                ID_HELP_SYSTEMINFO
        MENUITEM "&About",                      ID_HELP_ABOUT
    END
END


/////////////////////////////////////////////////////////////////////////////
//
// Icon
//

// Icon with lowest ID value placed first to ensure application icon
// remains consistent on all systems.
IDI_ICON1               ICON                    "icon1.ico"
#endif    // English (United States) resources
/////////////////////////////////////////////////////////////////////////////



#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 3 resource.
//


/////////////////////////////////////////////////////////////////////////////
#endif    // not APSTUDIO_INVOKED

And of course the resource file.

//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by Lab_04_Playing_With_Timer_n_Resources.rc
//
#define IDR_MENU1                       101
#define IDI_ICON1                       102
#define ID_FILE_ERASE                   40001
#define ID_FILE_EXIT                    40002
#define ID_ANIMALS_DOG                  40003
#define ID_ANIMALS_CAT                  40004
#define ID_DATEINFO_CURRENTDATE         40005
#define ID_DATEINFO_CURRENTTIME         40006
#define ID_BREAK_SOUND                  40007
#define ID_BREAK_SHAPES                 40008
#define ID_HELP_SYSTEMINFO              40009
#define ID_HELP_ABOUT                   40010

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

I'm sure its a pretty simple fix. I just can't figure it out. I have looked on msdn and even read my textbook pretty deeply into it and the icon just doesn't happen. The Icon I'm trying to make appear is IDI_ICON1.

Upvotes: 0

Views: 2478

Answers (2)

ProDev7
ProDev7

Reputation: 75

1: I'm sure that your program works well. but pay attention a bit: wndclass.hIcon=LoadIcon (hInstance, MAKEINTRESOURCE(IDI_ICON1)); here you set a big icon . if you want to get it just press ALT+TAB and you'll see your icon (IDI_ICON1). 2: if you want to set the "small icon" the one you see on the upper left top of your main window and you'll see in the task bar just edit your code to be: wndclass.hIconSm= LoadIcon(NULL, MAKEINTRESOURCE(IDI_IDI_ICON1)); hIconSm (handle to the small icon) to see it just look at the top left of your window hIcon (handle to the big icon) to see it press ALT+TAB as a good programming don't use the same icon for both the big and the small icons

Upvotes: 0

Zdeslav Vojkovic
Zdeslav Vojkovic

Reputation: 14591

I believe that it works as expected, but you are looking at the small icon, (i.e. the one show in the upper-left corner of your window):

wndclass.hIconSm = LoadIcon(NULL, MAKEINTRESOURCE(IDI_ERROR));

this is the red 'X' icon. If you load another icon here, it will show up. E.g. if your IDI_ICON1 contains also an 16x16 version, just replace IDI_ERROR with IDI_ICON1. However, if it already contains 16x16 version, setting hIconSm to NULL should have the same effect, so you would typically use this to show a different small icon.

Upvotes: 2

Related Questions