window icon support for win32 api backend

This commit is contained in:
Vadim Lopatin 2014-05-21 15:27:53 +04:00
parent c564bb67a2
commit 901c5be5d7
4 changed files with 91 additions and 7 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.9 KiB

After

Width:  |  Height:  |  Size: 956 B

View File

@ -830,10 +830,16 @@ class ColorDrawBuf : ColorDrawBufBase {
/// create copy of ColorDrawBuf /// create copy of ColorDrawBuf
this(ColorDrawBuf v) { this(ColorDrawBuf v) {
this(v.width, v.height); this(v.width, v.height);
_buf.length = v._buf.length; //_buf.length = v._buf.length;
for (int i = 0; i < _buf.length; i++) for (int i = 0; i < _buf.length; i++)
_buf[i] = v._buf[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() { void invertAlpha() {
foreach(pixel; _buf) foreach(pixel; _buf)
pixel ^= 0xFF000000; pixel ^= 0xFF000000;

View File

@ -11,8 +11,62 @@ class Win32ColorDrawBuf : ColorDrawBufBase {
HDC _drawdc; HDC _drawdc;
HBITMAP _drawbmp; HBITMAP _drawbmp;
@property HDC dc() { return _drawdc; } @property HDC dc() { return _drawdc; }
@property HBITMAP bmp() { return _drawdc; }
this(int width, int height) { this(int width, int height) {
resize(width, 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) { override uint * scanLine(int y) {
if (y >= 0 && y < _dy) if (y >= 0 && y < _dy)
@ -23,9 +77,11 @@ class Win32ColorDrawBuf : ColorDrawBufBase {
clear(); clear();
} }
override void clear() { override void clear() {
if (_drawbmp !is null) { if (_drawbmp !is null || _drawdc !is null) {
DeleteObject( _drawbmp ); if (_drawbmp)
DeleteObject( _drawdc ); DeleteObject(_drawbmp);
if (_drawdc)
DeleteObject(_drawdc);
_drawbmp = null; _drawbmp = null;
_drawdc = null; _drawdc = null;
_pixels = null; _pixels = null;

View File

@ -325,16 +325,38 @@ class Win32Window : Window {
_platform.closeWindow(this); _platform.closeWindow(this);
} }
HICON _icon;
/// sets window icon /// sets window icon
@property override void windowIcon(DrawBufRef buf) { @property override void windowIcon(DrawBufRef buf) {
if (_icon)
DestroyIcon(_icon);
_icon = null;
ColorDrawBuf icon = cast(ColorDrawBuf)buf.get; ColorDrawBuf icon = cast(ColorDrawBuf)buf.get;
if (!icon) { if (!icon) {
Log.e("Trying to set null icon for window"); Log.e("Trying to set null icon for window");
return; return;
} }
//icon = new ColorDrawBuf(icon); Win32ColorDrawBuf resizedicon = new Win32ColorDrawBuf(icon, 32, 32);
//icon.invertAlpha(); resizedicon.invertAlpha();
//destroy(icon); 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() { private void paintUsingGDI() {