c++ - Slight undesired transparency from FillRectangle -


i have window created ws_ex_layered window style. drawing onto memory bitmap using gdi+, , using updatelayeredwindow update graphical content of layered window.

here's snippet of code:

void redraw(hwnd hwnd, int width, int height) {     static bool floppy = true;      floppy = !floppy;      hdc hscreendc = getdc(hwnd_desktop);     hdc hmemdc = createcompatibledc(hscreendc);     hbitmap hbmp = createcompatiblebitmap(hscreendc, width, height);     hgdiobj hobj = selectobject(hmemdc, hbmp);      graphics gfx(hmemdc);      solidbrush b(color(254, (floppy ? 255 : 0), (floppy ? 0 : 255), 0));     gfx.fillrectangle(&b, rect(0, 0, width, height));      blendfunction blend;     blend.blendop = ac_src_over;     blend.blendflags = 0;     blend.sourceconstantalpha = 255;     blend.alphaformat = ac_src_alpha;      point src = { 0, 0 };      size size;     size.cx = width;     size.cy = height;      assert(updatelayeredwindow(         hwnd,         hscreendc,         null,         &size,         hmemdc,         &src,         rgb(0, 0, 0),         &blend,         ulw_alpha     ));      selectobject(hmemdc, hobj);     deleteobject(hbmp);     deletedc(hmemdc);     releasedc(hwnd_desktop, hscreendc); } 

when creating solidbrush, specified value of 254 alpha component. results in 99.6% opaque fill, not want.

when specify 255 alpha component, there appears no fill; window becomes transparent. issue because wish draw shapes 100% opaque, wish draw aren't.

there seems qwerks fillrectangle. becomes apparent when observe using fillellipse solidbrush alpha component 255, results in shape being rendered (opaque).

here 2 work-arounds came with, each solve issue me:

  • call fillrectangle twice

    solidbrush b(color(254, 255, 0, 0)); gfx.fillrectangle(&b, rect(0, 0, width, height)); gfx.fillrectangle(&b, rect(0, 0, width, height)); 

    since same area being filled twice, blend , create rgb(255, 0, 0) regardless of content behind window (it's 100% opaque). not prefer method, requires every rectangle drawn twice.

  • use fillpolygon instead

    just fillellipse, fillpolygon doesn't seem have colour issue, unless call so:

    solidbrush b(color(255, 255, 0, 0)); point points[4]; points[0] = point(0, 0); points[1] = point(width, 0); points[2] = point(width, height); points[4] = point(0, height); gfx.fillpolygon(&b, points, 4); //don't copy , paste - won't work 

    the above code result in 100% transparent window. guessing either due form of optimisation passes call fillrectangle instead. or - - there problem fillpolygon, called fillrectangle. however, if add point array, can around it:

    solidbrush b(color(255, 255, 0, 0)); point points[5]; points[0] = point(0, 0); points[1] = point(0, 0); //<- points[2] = point(width, 0); points[3] = point(width, height); points[4] = point(0, height); gfx.fillpolygon(&b, points, 5); 

    the above code indeed draw 100% opaque shape, fixes problem.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -