CA1K Productions
CA1K Productions

Reputation: 47

C++ NetBeans Win32 hwnd icon

I am having difficulties with adding an icon to my application window in NetBeans. The code is Win32 related. Whenever I add my icon to the resources folder (in the NetBeans IDE) the LoadImage() or the LoadIcon() methods fail to retrieve the icon file, and the result I get is these screenshots:

So I am wondering, is there something wrong with the location I put the icon? If so, where could I put the icon in? (Project directories below):

enter image description here

If it has nothing to do with the location, or I am using the right location, could it be the code I am using? (Code below):

WNDCLASSEX wc;
HWND hwnd;
MSG Msg;

//Step 1: Registering the Window Class
wc.cbSize        = sizeof(WNDCLASSEX);
wc.style         = 0;
wc.lpfnWndProc   = WndProc;
wc.cbClsExtra    = 0;
wc.cbWndExtra    = 0;
wc.hInstance     = hInstance;
wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName  = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

Feed back would be VERY appreciated. I really want to know what is going on with this. Thanks.

-CA1K

EDIT: The code shown above is set back to the usual, I am just trying to find ways to retrieve the icon file.

Upvotes: 1

Views: 1137

Answers (1)

Barmak Shemirani
Barmak Shemirani

Reputation: 31599

@andlabs is right. IDI_APPLICATION is defined in WinUser.h as:

#define IDI_APPLICATION MAKEINTRESOURCE(32512)

You need instead:

wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON));
wc.hIconSm = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SMALLICON));

Where you define IDI_ICON as a number:

#define IDI_ICON 1

And in resource.rc:

IDI_ICON ICON "icon.ico"

This will set the icon in taskbar. For the icon in title bar, use this in WM_CREAT:

HICON hicon = (HICON)LoadImage(GetModuleHandleW(NULL), MAKEINTRESOURCE(IDI_ICON), 
    IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR | LR_DEFAULTSIZE);
SendMessage(hWnd, WM_SETICON, ICON_BIG, (LPARAM)hicon);

----------------------------------------------------------

Edit:

For a simple test, try

In resource.rc file:

1 ICON "icon.ico"

In .cpp file:

wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(1));

Upvotes: 2

Related Questions