0

I want to create buttons with custom shapes so I decided to do it with the help of Layered Windows. I create window for button, set bitmap for it, make it layered and then try to make white color fully transparent. As soon as I write on masm, the code looks like this:

mov button, rv(CreateWindowEx, WS_EX_APPWINDOW or WS_EX_LAYERED, 
    chr$("Button"), NULL, 
    WS_CHILD or BS_BITMAP or WS_VISIBLE, 
    300, 10, 81, 98, 
    hWin, 200, hInstance, NULL)
mov bmHandle, rv(LoadImage, NULL, bitmapPath, 
    IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE or LR_DEFAULTSIZE)
invoke SendMessage, button, BM_SETIMAGE, IMAGE_BITMAP, eax
invoke SetLayeredWindowAttributes, hWnd, 00000000h, 0, LWA_COLORKEY

But white color on bitmap does not become transparent though it worked with custom window shape for me.

How can I make this work?

0shn1x
  • 41
  • 9
  • *Note that WS_EX_LAYERED cannot be used for child windows* – RbMm Dec 24 '17 at 18:36
  • @RbMm okay, so how can I make bitmap semi-transparent? – 0shn1x Dec 24 '17 at 18:57
  • probably use custom draw for button and draw bitmap with `AlphaBlend` – RbMm Dec 24 '17 at 19:01
  • 2
    RbMm, According to https://msdn.microsoft.com/en-us/library/windows/desktop/ms632599(v=vs.85).aspx WS_EX_LAYERED can be used with WS_CHILD windows as of windows 8. – SoronelHaetir Dec 24 '17 at 19:30
  • @SoronelHaetir I have Windows 10 so this should work for me. – 0shn1x Dec 24 '17 at 20:22
  • 1
    [SetWindowRgn](https://msdn.microsoft.com/en-us/library/dd145102.aspx). – IInspectable Dec 24 '17 at 22:41
  • 1
    If you want white to be transparent you should use `00ffffffh` for the color key. – Jonathan Potter Dec 25 '17 at 00:05
  • Color keying looks ugly, because it creates jagged edges unless your shapes are strictly rectangular. For antialiasing create a bitmap with an alpha channel and use `UpdateLayeredWindow` to assign that bitmap to the child window. Solution 2 of [this answer](https://stackoverflow.com/a/42603066/7571258) has example code. – zett42 Dec 25 '17 at 00:37
  • @zett42 it's really good example but it shows how to make full rectangle transparent. What I want is to make transparent only one color on bitmap. Is it possible with `UpdateLayeredWindow` ? – 0shn1x Dec 25 '17 at 07:25
  • @IInspectable i already tried it but it's not working. My code looks like: `invoke GetClientRect, button, addr winRect` `mov regTri, rv(CreateEllipticRgn, winRect.left, winRect.top, winRect.right, winRect.bottom)` `invoke SetWindowRgn, button, regTri, 1` – 0shn1x Dec 25 '17 at 07:36
  • *"It doesn't work"* is not a problem statement. – IInspectable Dec 25 '17 at 07:55
  • @IInspectable I run this code, all functions returned no error but button does not become round – 0shn1x Dec 25 '17 at 08:19
  • With `UpdateLayeredWindow` you can achieve that but you would have to edit your bitmap. In an image editor, select all pixels of the color you want to make transparent and "delete" them. This will usually set the alpha channel to 0, making the pixels transparent. Save the bitmap with 32bpp, as outlined in linked answer. – zett42 Dec 25 '17 at 11:06
  • @IInspectable but `SetWindowRgn` does not work for windows created with `WS_CHILD`, unfortunately – 0shn1x Dec 25 '17 at 11:17
  • @zett42 I wrote code similar to one in example but `UpdateLayeredWindow` returns me 57 error (ERROR_INVALID_PARAMETER). Do you know how to parse it and understand which parameter is incorrect? – 0shn1x Dec 25 '17 at 12:00
  • Without seeing the code, it's hard to guess. Make sure you check return values of all other functions too. Here is [another example](http://www.nuonsoft.com/blog/2009/05/27/how-to-use-updatelayeredwindow/). When you use `LoadImage` you need to specify `LR_CREATEDIBSECTION` to keep the alpha channel. – zett42 Dec 25 '17 at 13:28
  • @0shn1x "*but `SetWindowRgn` does not work for windows created with `WS_CHILD`*" - yes, it does. In the past, I've made child controls that use custom regions, it works fine. And prior to Win8, a region is the only way to create a non-rectangular child control without resorting to custom drawing and custom hit testing – Remy Lebeau Dec 26 '17 at 23:04

0 Answers0