OneLoneCoder / olcPixelGameEngine

The official distribution of olcPixelGameEngine, a tool used in javidx9's YouTube videos and projects
Other
3.85k stars 913 forks source link

How do I add icon in windows? #263

Open axelbozic opened 2 years ago

axelbozic commented 2 years ago

I am using visual studio 2019. I tried adding icons but it only applies to the console and not the game window. How would I apply icons to the game window?

gurkanctn commented 2 years ago

you might be luckier / faster to find a solution on the broader Internet, if you search for the Visual Studio app development forums/sites/tutorials, or MS support/help pages.

oblaser commented 2 years ago

You need a resource file (e.g. resources.rc) added to your VS Project:

// the alphabetical first ICON is used by Windows as app icon
AAAAA_MainIcon ICON "relative\\path\\to\\assets\\icon.ico"

Add this to the OnUserCreate() method (tested on PGE v2.16, but I think it will work for all v2.x):

#ifdef OLC_PLATFORM_WINAPI
    HWND hwnd_pge = FindWindowExW(nullptr, nullptr, L"OLC_PIXEL_GAME_ENGINE", nullptr);
    HICON hicon = LoadIconW(GetModuleHandleW(nullptr), L"AAAAA_MainIcon");
    if (hwnd_pge && hicon)
    {
        SendMessageW(hwnd_pge, WM_SETICON, ICON_SMALL, (LPARAM)hicon);
        SendMessageW(hwnd_pge, WM_SETICON, ICON_BIG, (LPARAM)hicon);
    }
#endif

and add the Win32 API include as the last include (!):

#ifdef OLC_PLATFORM_WINAPI
#include <Windows.h>
#endif

(if Windows.h causes name collisions, create a function with the above code in a separate file, and call the function from OnUserCreate())