Made uiColorButton on Windows draw with actual alpha values.

This commit is contained in:
Pietro Gagliardi 2016-05-22 16:07:31 -04:00
parent 531f8ea19c
commit 585872839d
1 changed files with 34 additions and 12 deletions

View File

@ -53,9 +53,15 @@ static BOOL onWM_NOTIFY(uiControl *c, HWND hwnd, NMHDR *nmhdr, LRESULT *lResult)
{ {
uiColorButton *b = uiColorButton(c); uiColorButton *b = uiColorButton(c);
NMCUSTOMDRAW *nm = (NMCUSTOMDRAW *) nmhdr; NMCUSTOMDRAW *nm = (NMCUSTOMDRAW *) nmhdr;
RECT r; RECT client;
ID2D1DCRenderTarget *rt;
D2D1_RECT_F r;
D2D1_COLOR_F color;
D2D1_BRUSH_PROPERTIES bprop;
ID2D1SolidColorBrush *brush;
uiWindowsSizing sizing; uiWindowsSizing sizing;
int x, y; int x, y;
HRESULT hr;
if (nmhdr->code != NM_CUSTOMDRAW) if (nmhdr->code != NM_CUSTOMDRAW)
return FALSE; return FALSE;
@ -63,21 +69,37 @@ static BOOL onWM_NOTIFY(uiControl *c, HWND hwnd, NMHDR *nmhdr, LRESULT *lResult)
if (nm->dwDrawStage != CDDS_PREPAINT) if (nm->dwDrawStage != CDDS_PREPAINT)
return FALSE; return FALSE;
// TODO use Direct2D? either way, draw alpha uiWindowsEnsureGetClientRect(b->hwnd, &client);
uiWindowsEnsureGetClientRect(b->hwnd, &r); rt = makeHDCRenderTarget(nm->hdc, &client);
rt->BeginDraw();
uiWindowsGetSizing(b->hwnd, &sizing); uiWindowsGetSizing(b->hwnd, &sizing);
x = 3; // should be enough x = 3; // should be enough
y = 3; y = 3;
uiWindowsSizingDlgUnitsToPixels(&sizing, &x, &y); uiWindowsSizingDlgUnitsToPixels(&sizing, &x, &y);
r.left += x; r.left = client.left + x;
r.top += y; r.top = client.top + y;
r.right -= x; r.right = client.right - x;
r.bottom -= y; r.bottom = client.bottom - y;
// TODO error check
#define comp(x) ((BYTE) (x * 255)) color.r = b->r;
// TODO free brush color.g = b->g;
FillRect(nm->hdc, &r, CreateSolidBrush(RGB(comp(b->r), comp(b->g), comp(b->b)))); color.b = b->b;
#undef comp color.a = b->a;
ZeroMemory(&bprop, sizeof (D2D1_BRUSH_PROPERTIES));
bprop.opacity = 1.0;
bprop.transform._11 = 1;
bprop.transform._22 = 1;
hr = rt->CreateSolidColorBrush(&color, &bprop, &brush);
if (hr != S_OK)
logHRESULT(L"error creating brush for color button", hr);
rt->FillRectangle(&r, brush);
brush->Release();
hr = rt->EndDraw(NULL, NULL);
if (hr != S_OK)
logHRESULT(L"error drawing color on color button", hr);
rt->Release();
// skip default processing (don't draw text) // skip default processing (don't draw text)
*lResult = CDRF_SKIPDEFAULT; *lResult = CDRF_SKIPDEFAULT;