diff --git a/examples/example1/res/mdpi/dlangui-logo1.png b/examples/example1/res/mdpi/dlangui-logo1.png index 84593ee7..da4f1f56 100644 Binary files a/examples/example1/res/mdpi/dlangui-logo1.png and b/examples/example1/res/mdpi/dlangui-logo1.png differ diff --git a/src/dlangui/graphics/drawbuf.d b/src/dlangui/graphics/drawbuf.d index 059013ba..0bd84e5f 100644 --- a/src/dlangui/graphics/drawbuf.d +++ b/src/dlangui/graphics/drawbuf.d @@ -830,10 +830,16 @@ class ColorDrawBuf : ColorDrawBufBase { /// create copy of ColorDrawBuf this(ColorDrawBuf v) { this(v.width, v.height); - _buf.length = v._buf.length; + //_buf.length = v._buf.length; for (int i = 0; i < _buf.length; i++) _buf[i] = v._buf[i]; } + /// create resized copy of ColorDrawBuf + this(ColorDrawBuf v, int dx, int dy) { + this(dx, dy); + fill(0xFFFFFFFF); + drawRescaled(Rect(0, 0, dx, dy), v, Rect(0, 0, v.width, v.height)); + } void invertAlpha() { foreach(pixel; _buf) pixel ^= 0xFF000000; diff --git a/src/dlangui/platforms/windows/win32drawbuf.d b/src/dlangui/platforms/windows/win32drawbuf.d index 512c908f..355a1e7e 100644 --- a/src/dlangui/platforms/windows/win32drawbuf.d +++ b/src/dlangui/platforms/windows/win32drawbuf.d @@ -11,8 +11,62 @@ class Win32ColorDrawBuf : ColorDrawBufBase { HDC _drawdc; HBITMAP _drawbmp; @property HDC dc() { return _drawdc; } + @property HBITMAP bmp() { return _drawdc; } this(int width, int height) { resize(width, height); + } + /// create resized copy of ColorDrawBuf + this(ColorDrawBuf v, int dx, int dy) { + this(dx, dy); + fill(0xFFFFFFFF); + if (_dx == dx && _dy == dy) + drawImage(0, 0, v); + else + drawRescaled(Rect(0, 0, dx, dy), v, Rect(0, 0, v.width, v.height)); + } + void invertAlpha() { + for(int i = _dx * _dy - 1; i >= 0; i--) + _pixels[i] ^= 0xFF000000; + } + /// returns HBITMAP for alpha + HBITMAP createTransparencyBitmap() { + int hbytes = (((_dx + 7) / 8) + 1) & 0xFFFFFFFE; + static ubyte[] buf; + buf.length = hbytes * _dy * 2; + //for (int y = 0; y < _dy; y++) { + // uint * src = scanLine(y); + // ubyte * dst1 = buf.ptr + (_dy - 1 - y) * hbytes; + // ubyte * dst2 = buf.ptr + (_dy - 1 - y) * hbytes + hbytes * _dy; + // for (int x = 0; x < _dx; x++) { + // ubyte pixel1 = 0x80; //(src[x] >> 24) > 0x80 ? 0 : 0x80; + // ubyte pixel2 = (src[x] >> 24) < 0x80 ? 0 : 0x80; + // int xi = x >> 3; + // dst1[xi] |= (pixel1 >> (x & 7)); + // dst2[xi] |= (pixel2 >> (x & 7)); + // } + //} + // debug + for(int i = 0; i < hbytes * _dy; i++) + buf[i] = 0xFF; + for(int i = hbytes * _dy; i < buf.length; i++) + buf[i] = 0; //0xFF; + + BITMAP b; + b.bmWidth = _dx; + b.bmHeight = _dy; + b.bmWidthBytes = hbytes; + b.bmPlanes = 1; + b.bmBitsPixel = 1; + b.bmBits = buf.ptr; + return CreateBitmapIndirect(&b); + //return CreateBitmap(_dx, _dy, 1, 1, buf.ptr); + } + /// destroy object, but leave bitmap as is + HBITMAP destoryLeavingBitmap() { + HBITMAP res = _drawbmp; + _drawbmp = null; + destroy(this); + return res; } override uint * scanLine(int y) { if (y >= 0 && y < _dy) @@ -23,9 +77,11 @@ class Win32ColorDrawBuf : ColorDrawBufBase { clear(); } override void clear() { - if (_drawbmp !is null) { - DeleteObject( _drawbmp ); - DeleteObject( _drawdc ); + if (_drawbmp !is null || _drawdc !is null) { + if (_drawbmp) + DeleteObject(_drawbmp); + if (_drawdc) + DeleteObject(_drawdc); _drawbmp = null; _drawdc = null; _pixels = null; diff --git a/src/dlangui/platforms/windows/winapp.d b/src/dlangui/platforms/windows/winapp.d index 143fb29e..b2c3870b 100644 --- a/src/dlangui/platforms/windows/winapp.d +++ b/src/dlangui/platforms/windows/winapp.d @@ -325,16 +325,38 @@ class Win32Window : Window { _platform.closeWindow(this); } + HICON _icon; + /// sets window icon @property override void windowIcon(DrawBufRef buf) { + if (_icon) + DestroyIcon(_icon); + _icon = null; ColorDrawBuf icon = cast(ColorDrawBuf)buf.get; if (!icon) { Log.e("Trying to set null icon for window"); return; } - //icon = new ColorDrawBuf(icon); - //icon.invertAlpha(); - //destroy(icon); + Win32ColorDrawBuf resizedicon = new Win32ColorDrawBuf(icon, 32, 32); + resizedicon.invertAlpha(); + ICONINFO ii; + HBITMAP mask = resizedicon.createTransparencyBitmap(); + HBITMAP color = resizedicon.destoryLeavingBitmap(); + ii.fIcon = TRUE; + ii.xHotspot = 0; + ii.yHotspot = 0; + ii.hbmMask = mask; + ii.hbmColor = color; + _icon = CreateIconIndirect(&ii); + if (_icon) { + SendMessage(_hwnd, WM_SETICON, ICON_SMALL, cast(int)_icon); + SendMessage(_hwnd, WM_SETICON, ICON_BIG, cast(int)_icon); + } else { + Log.e("failed to create icon"); + } + if (mask) + DeleteObject(mask); + DeleteObject(color); } private void paintUsingGDI() {