WinAPI - Basic Window Icon?

I seem to keep getting an error:

66|: error: 'IDI_WINDOWICON' was not declared in this scope|

for this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <windows.h>
#include <string>
#include <fstream>
#include <winver.h>
#include "res.h"

using namespace std;

LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);

HINSTANCE   hInst;
HICON       main_icon;

int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, PSTR lpCmdLine, int nCmdShow) {

    MSG            msg;
    WNDCLASS    wc;
    HWND        hwnd = NULL;
    hInst  = hThisInstance;

    wc.cbClsExtra       = 0;
    wc.cbWndExtra       = 0;
    wc.hbrBackground    = CreateSolidBrush(RGB(192, 192, 192));
    wc.hCursor          = LoadCursor(NULL, IDC_ARROW);
    wc.hIcon            = LoadIcon(NULL, IDI_APPLICATION);
    wc.hInstance        = hThisInstance;
    wc.lpfnWndProc      = WinProc;
    wc.lpszClassName    = "Window";
    wc.lpszMenuName     = NULL;
    wc.style            = CS_HREDRAW | CS_VREDRAW;

    if (!RegisterClass(&wc)) {
        MessageBox(NULL, "Error registering class", "ERROR", MB_OK);
        return 0;
    }

    hwnd = CreateWindow("Window",
        "HW2BDEd v1.0.0",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        640,
        400,
        NULL,
        NULL,
        hThisInstance,
        NULL);

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    while (GetMessage(&msg, (HWND)NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return msg.wParam;
}

LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {

    switch (msg) {

  case WM_CREATE: {

      main_icon = LoadIcon(hInst, MAKEINTRESOURCE(IDI_WINDOWICON));
      SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM)main_icon);

    } break;

  case WM_DESTROY: {
            PostQuitMessage(0);
    return 0;
  } break;

  case WM_CLOSE: {
            DestroyWindow(hwnd);
  } break;
    }

    return DefWindowProc(hwnd, msg, wParam, lParam);
}


I'm assuming adding an icon to a window at runtime can be done in a simple way with resource files and a little coding. For some reason, it doesn't accept my main icon ID: "IDI_WINDOWICON". It says it wasn't declared in this scope. WTH? Suggestions?
The ID's of icons have to be defined before you use them. IDI_WINDOWICON is not an existing icon ID as far as I Know. You could try IDI_APPLICATION instead. You can find more examples at http://msdn.microsoft.com/en-us/library/windows/desktop/ms648072%28v=vs.85%29.aspx.
You should change the hInst to NULL, when using standard icons.
Last edited on
Thanks, I solved it. All I had to do was use quotes to show the ID of the icon. Like so:

1
2
HICON main_icon = LoadIcon(hInst, "IDI_WINDOWICON");
SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM)main_icon);


Hope this helps others in the future.
Topic archived. No new replies allowed.