win32 - how change window icon?

i'm trying changind the window icon without sucess :(
1
2
3
4
5
6
7
8
9
HICON hWindowIcon=NULL;
        void SetIcon(string stricon)
        {
            if(hWindowIcon!=NULL)
                DestroyIcon(hWindowIcon);
            hWindowIcon =LoadIcon(GetModuleHandle(NULL),stricon.c_str());
            SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM)hWindowIcon);
            SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hWindowIcon);
        }

what i'm doing wrong?
what i'm doing wrong?


The same as usual - you don't check the return values - neither from LoadIcon nor from SendMessage.
the hWindowIcon isn't NULL. how can i check the SendMessage(), when give me an error?
no errors:
1
2
3
4
5
6
7
8
9
10
11
12
13
HICON hWindowIcon=NULL;
        void SetIcon(string stricon)
        {
            if(hWindowIcon!=NULL)
                DestroyIcon(hWindowIcon);
            hWindowIcon =LoadIcon(GetModuleHandle(NULL),stricon.c_str());
            if(hWindowIcon==NULL)
                DebugText("icon error: " + to_string(GetLastError()));
            if(SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM)hWindowIcon)!=WM_SETICON)
                DebugText("icon error: " + to_string(GetLastError()));
            if(SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hWindowIcon)!=WM_SETICON)
                DebugText("icon error: " + to_string(GetLastError()));
        }
I just create a demo - have a look if it works on your PC.

Main.cpp
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
#include <windows.h>
#include <tchar.h>
#include "resource.h"
#include <crtdbg.h>

LRESULT CALLBACK WndProc (HWND hwnd, unsigned int msg, WPARAM wParam, LPARAM lParam)
{
  switch (msg)
  {
    case WM_CREATE:
    {
      HINSTANCE hInstance = ((LPCREATESTRUCT)lParam)->hInstance;
      HICON hIcon = LoadIcon (hInstance, MAKEINTRESOURCE (IDI_ICON1));
      _ASSERTE (hIcon != 0);
      SendMessage (hwnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
      return 0;
    }
    case WM_COMMAND:
    {
      return 0;
    }
    case WM_DESTROY:
    {
      PostQuitMessage (0);
      return 0;
    }
  }

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


int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevIns, LPSTR lpszArgument, int iShow)
{
  TCHAR szClassName[] = _T("Template");
  TCHAR szWindowName[] = _T("Template");
  WNDCLASSEX wc = { 0 };
  MSG messages;
  HWND hWnd;

  wc.lpszClassName = szClassName;
  wc.lpfnWndProc = WndProc;
  wc.cbSize = sizeof (WNDCLASSEX);
  wc.hbrBackground = (HBRUSH)COLOR_BTNSHADOW;
  wc.hInstance = hInstance;
  
  _ASSERTE(RegisterClassEx (&wc) !=0);
  
  hWnd = CreateWindowEx (0, szClassName, szWindowName, WS_OVERLAPPEDWINDOW, 
      CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 
      HWND_DESKTOP, 0, hInstance, 0);
      
  _ASSERTE(::IsWindow(hWnd));
  
  ShowWindow (hWnd, iShow);
  while (GetMessage (&messages, NULL, 0, 0))
  {
    TranslateMessage (&messages);
    DispatchMessage (&messages);
  }

  return static_cast<int>(messages.wParam);
}


resource.h
 
#define IDI_ICON1                       101 


resource.rc
1
2
#include "resource.h"
IDI_ICON1               ICON                    "Icon.ico"


The Icon file needs to be in the root folder
finally i fix it and i don't know why was not working :(
maybe was the icon size, i don't know
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
HICON hWindowIcon=NULL;
        HICON hWindowIconBig=NULL;
        void SetIcon(string stricon)
        {
            if(hWindowIcon!=NULL)
                DestroyIcon(hWindowIcon);
            if(hWindowIconBig!=NULL)
                DestroyIcon(hWindowIconBig);
            if(stricon=="")
            {
                SendMessage( hwnd, WM_SETICON, ICON_SMALL, (LPARAM)NULL );
                SendMessage( hwnd, WM_SETICON, ICON_BIG, (LPARAM)NULL );
            }
            else
            {
                hWindowIcon =(HICON)LoadImage(NULL, stricon.c_str(), IMAGE_ICON, 16, 16, LR_LOADFROMFILE);
                hWindowIconBig =(HICON)LoadImage(NULL, stricon.c_str(), IMAGE_ICON, 32, 32, LR_LOADFROMFILE);
                SendMessage( hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hWindowIcon );
                SendMessage( hwnd, WM_SETICON, ICON_BIG, (LPARAM)hWindowIconBig );
            }

        }

i'm sorry, but seen the resource.h and resource.rc, can we use variables?
I don't think that you can use variables only constants.
my objective is create a class for change that :(
on rc file we use a string and a const name
Last edited on
Topic archived. No new replies allowed.