implement window icon for win

This commit is contained in:
Eduardo Bart 2011-11-23 14:35:16 -02:00
parent c8d5ca1cfb
commit dfb8b428eb
3 changed files with 51 additions and 2 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 248 B

After

Width:  |  Height:  |  Size: 518 B

View File

@ -27,6 +27,9 @@
#include <dir.h> #include <dir.h>
#include <physfs.h> #include <physfs.h>
#include <framework/thirdparty/apngloader.h>
#include <framework/core/resourcemanager.h>
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
struct Win32PlatformPrivate { struct Win32PlatformPrivate {
@ -424,6 +427,45 @@ bool Platform::createWindow(int x, int y, int width, int height, int minWidth, i
return true; return true;
} }
void Platform::setWindowIcon(const std::string& pngIcon)
{
apng_data apng;
std::stringstream fin;
g_resources.loadFile(pngIcon, fin);
if(load_apng(fin, &apng) == 0) {
if(apng.bpp != 4)
logError("could not set app icon, icon image must have 4 channels");
int n = apng.width * apng.height;
std::vector<uint32> iconData(n);
for(int i=0; i < n;++i) {
uint8 *pixel = (uint8*)&iconData[i];
pixel[2] = *(apng.pdata + (i * 4) + 0);
pixel[1] = *(apng.pdata + (i * 4) + 1);
pixel[0] = *(apng.pdata + (i * 4) + 2);
pixel[3] = *(apng.pdata + (i * 4) + 3);
}
HBITMAP hbmColor = CreateBitmap(apng.width, apng.height, 1, 32, &iconData[0]);
HBITMAP hbmMask = CreateCompatibleBitmap(GetDC(NULL), apng.width, apng.height);
ICONINFO ii;
ii.fIcon = TRUE;
ii.hbmColor = hbmColor;
ii.hbmMask = hbmMask;
ii.xHotspot = 0;
ii.yHotspot = 0;
HICON icon = CreateIconIndirect(&ii);
DeleteObject(hbmMask);
DeleteObject(hbmColor);
SendMessage(win32.window, WM_SETICON, ICON_SMALL, (LPARAM)icon);
SendMessage(win32.window, WM_SETICON, ICON_BIG, (LPARAM)icon);
} else
logError("could not load app icon");
}
void Platform::destroyWindow() void Platform::destroyWindow()
{ {
if(win32.hrc) { if(win32.hrc) {

View File

@ -704,12 +704,19 @@ void Platform::setWindowIcon(const std::string& pngIcon)
std::stringstream fin; std::stringstream fin;
g_resources.loadFile(pngIcon, fin); g_resources.loadFile(pngIcon, fin);
if(load_apng(fin, &apng) == 0) { if(load_apng(fin, &apng) == 0) {
if(apng.bpp != 4)
logError("could not set app icon, icon image must have 4 channels");
int n = apng.width * apng.height; int n = apng.width * apng.height;
std::vector<unsigned long int> iconData(n + 2); std::vector<unsigned long int> iconData(n + 2);
iconData[0] = apng.width; iconData[0] = apng.width;
iconData[1] = apng.height; iconData[1] = apng.height;
for(int i=0; i < n;++i) for(int i=0; i < n;++i) {
iconData[2 + i] = *(uint32_t*)(apng.pdata + (i * 4)); uint8 *pixel = (uint8*)&iconData[2 + i];
pixel[2] = *(apng.pdata + (i * 4) + 0);
pixel[1] = *(apng.pdata + (i * 4) + 1);
pixel[0] = *(apng.pdata + (i * 4) + 2);
pixel[3] = *(apng.pdata + (i * 4) + 3);
}
Atom property = XInternAtom(x11.display, "_NET_WM_ICON", 0); Atom property = XInternAtom(x11.display, "_NET_WM_ICON", 0);
if(!XChangeProperty(x11.display, x11.window, property, XA_CARDINAL, 32, PropModeReplace, (const unsigned char*)&iconData[0], iconData.size())) if(!XChangeProperty(x11.display, x11.window, property, XA_CARDINAL, 32, PropModeReplace, (const unsigned char*)&iconData[0], iconData.size()))