AlphaBlend() can make 1 color transparent too?

using the AlphaBlend() can make 1 color transparent too?
what i mean is make a color transparent(like TransparentBlt()) and using the AlphaBlend())... TransparentBlt() + AlphaBlend() at same time.
What do you mean by "one colour"?
There are always two (Sith). You have A. You put B in front of A.
If B is opaque, then you see only B. If B is transparent, then the resulting colour is a mix of A and B.

You should refer to Microsoft documentation about Microsoft API; it has very little to do with C++.
1st lets see if i can use the AlphaBlend()....

heres my Bitmap class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
class Bitmap
{
    public:
        unsigned int Width =0;
        unsigned int Height =0;
        RGBQUAD *Pixels=NULL;
        HBITMAP HBitmap=NULL;
        HBITMAP OldHBitmap =NULL;
        HDC HDCBitmap;
        BITMAPINFO BitInfo;
        void *BufferMemory=NULL;

        Bitmap(unsigned int SizeWidth, unsigned int SizeHeight, COLORREF BackColor=RGB(0,0,0))
        {
            New(SizeWidth,SizeHeight,BackColor);
        }
        void Disposed()
        {
            Pixels=NULL;
            VirtualFree(BufferMemory, 0,MEM_RELEASE);
            SelectObject(HDCBitmap, OldHBitmap);
            DeleteDC(HDCBitmap);
            DeleteObject(HBitmap);
        }

        void New(unsigned int SizeWidth, unsigned int SizeHeight, COLORREF BackColor=RGB(0,0,0))
        {
            int BufferSize = SizeWidth * SizeHeight * sizeof(RGBQUAD);

            if(BufferMemory) Disposed();
            BufferMemory = VirtualAlloc(0, BufferSize,MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
            Pixels =static_cast<RGBQUAD *> (BufferMemory);

            Width = SizeWidth;
            Height = SizeHeight;
            ZeroMemory(&BitInfo, sizeof(BITMAPINFO));
            BitInfo.bmiHeader.biSize = sizeof(BitInfo.bmiHeader);
            BitInfo.bmiHeader.biWidth = Width;
            BitInfo.bmiHeader.biHeight = -Height;
            BitInfo.bmiHeader.biPlanes = 1;
            BitInfo.bmiHeader.biBitCount = 32;
            BitInfo.bmiHeader.biCompression = BI_RGB;
            //BitInfo.bmiHeader.biSizeImage =  BitInfo.bmiHeader.biWidth  * (-BitInfo.bmiHeader.biHeight)* sizeof(RGBQUAD);
            HBitmap = CreateBitmap(Width, Height,1,32,Pixels);
            HDCBitmap = CreateCompatibleDC(NULL);
            OldHBitmap= (HBITMAP)SelectObject(HDCBitmap, HBitmap);
            Clear(BackColor);
        }

        void Clear(COLORREF BackColor=RGB(0,0,0), BYTE AlphaValue=255)
        {
            if(!BufferMemory) return;
            for(unsigned int PosY=0; PosY<Height; PosY++)
            {
                for(unsigned int PosX=0; PosX<Width; PosX++)
                {
                    Pixels[PosY*Width + PosX].rgbRed =GetRValue(BackColor)*AlphaValue/255;
                    Pixels[PosY*Width + PosX].rgbGreen =GetGValue(BackColor)*AlphaValue/255;
                    Pixels[PosY*Width + PosX].rgbBlue =GetBValue(BackColor)*AlphaValue/255;
                }
            }
            SetDIBits(HDCBitmap,HBitmap,0,Height,Pixels,&BitInfo,DIB_RGB_COLORS);
        }

        void Draw(HDC HDCDestination, int PosX, int PosY, BYTE AlphaValue=5 )
        {
           //AlphaBlendWithTransparentColor (HDCDestination,PosX, PosY, Width, Height,HDCBitmap,PosX, PosY, Width,Height, SourceConstantAlpha, RGB(0,0,0));
            BLENDFUNCTION bf;
            bf.AlphaFormat = 0;
            bf.BlendFlags = 0;
            bf.BlendOp = AC_SRC_OVER;
            bf.SourceConstantAlpha = AlphaValue;
            AlphaBlend(HDCDestination, PosX, PosY, Width, Height,HDCBitmap, 0,0,Width, Height, bf);
        }

        void SetPixel(unsigned int PosX, unsigned int PosY, COLORREF Color)
        {
            if(BufferMemory)
            {
                if(PosX>=Width|| PosX<0) PosX = Width-1;
                if(PosY>=Height || PosY<0) PosY = Height-1;
                Pixels[PosY*Width + PosX].rgbRed =GetRValue(Color);
                Pixels[PosY*Width + PosX].rgbGreen =GetGValue(Color);
                Pixels[PosY*Width + PosX].rgbBlue =GetBValue(Color);
            }
            SetDIBits(HDCBitmap,HBitmap,0,Height,Pixels,&BitInfo,DIB_RGB_COLORS);
        }

        RGBQUAD GetPixel(unsigned int PosX, unsigned int PosY)
        {
            if(BufferMemory)
            {
                if(PosX>=Width|| PosX<0) PosX = Width-1;
                if(PosY>=Height || PosY<0) PosY = Height-1;
                return Pixels[PosY*Width + PosX];
            }
        }

        void DrawLine3D(Position3D Origin, Position3D Destination, COLORREF Color, BYTE Alpha=0)
        {
            if(!BufferMemory) return;
            std::vector<Position3D> GetLineDots = GetLinePoints(Origin, Destination);
            for(unsigned int x=0; x<GetLineDots.size()-1; x++)
            {
                unsigned int PosX = GetLineDots[x].PosX;
                unsigned int PosY = GetLineDots[x].PosY;
                if(PosX>=Width|| PosX<0) PosX = Width-1;
                if(PosY>=Height || PosY<0) PosY = Height-1;
                Pixels[PosY*Width + PosX].rgbRed =GetRValue(Color)*Alpha/255;
                Pixels[PosY*Width + PosX].rgbGreen =GetGValue(Color)*Alpha/255;
                Pixels[PosY*Width + PosX].rgbBlue =GetBValue(Color)*Alpha/255;

            }
            SetDIBits(HDCBitmap,HBitmap,0,Height,Pixels,&BitInfo,DIB_RGB_COLORS);
        }
};

when i draw it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//Bitmap class:
void Draw(HDC HDCDestination, int PosX, int PosY, BYTE AlphaValue=5 )
        {
           //AlphaBlendWithTransparentColor (HDCDestination,PosX, PosY, Width, Height,HDCBitmap,PosX, PosY, Width,Height, SourceConstantAlpha, RGB(0,0,0));
            BLENDFUNCTION bf;
            bf.AlphaFormat = 0;
            bf.BlendFlags = 0;
            bf.BlendOp = AC_SRC_OVER;
            bf.SourceConstantAlpha = AlphaValue;
            AlphaBlend(HDCDestination, PosX, PosY, Width, Height,HDCBitmap, 0,0,Width, Height, bf);
        }

///.....
int main()
{


    HWND HWNDConsoleWindow = GetConsoleWindow();
    HDC HDCConsoleWindow = GetDC(HWNDConsoleWindow);

    Bitmap Bit(500,500,RGB(255 ,0,0));
    HBRUSH FillColor = CreateSolidBrush(RGB(0,255,0));
    RECT rec ={0,0,500,500};
    do
    {

        FillRect(HDCConsoleWindow,&rec,FillColor );
        Bit.Draw(HDCConsoleWindow,0,0,255);

    }while(!GetAsyncKeyState(VK_ESCAPE));
    DeleteObject(FillColor);
    return 0;
}

why the red isn't drawed or opacity? :(
finally i get it to work:
1 - the 'AlphaFormat' must be 'AC_SRC_ALPHA' test if the pixels have 255 for be transparent(TransparentBlt());
2 - the 'BlendOp' must be 'AC_SRC_OVER';
3 - the 'SourceConstantAlpha' is the General Alpha value.
4 - using the 'AlphaBlend' the output can be stretch.
1
2
3
4
5
6
BLENDFUNCTION bf;
            bf.AlphaFormat = AC_SRC_ALPHA;//use Alpha pixels... TransparentBlt()
            bf.BlendFlags = 0;
            bf.BlendOp = AC_SRC_OVER;
            bf.SourceConstantAlpha = AlphaValue;// use the AlphaBlend for all pixels
            AlphaBlend(HDCDestination, PosX, PosY, Width, Height,HDCBitmap, 0,0,Width, Height, bf);

i need understand 1 thing: why, sometimes, seems, the 'HDCBitmap' is showed on bottom instead above?

test:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
void SimpleConstantAlphaBlend(HDC hDC)
{
  const int size = 100;

  for (int i=0; i<3; i++)
  {
    RECT  rect = { i*(size+10) + 20, 20+size/3,
               i*(size+10) + 20 + size, 20+size/3 + size };

    COLORREF Color[]={ RGB(0xFF,0,0), RGB(0,0xFF,0), RGB(0,0,0xFF) };

    HBRUSH hBrush = CreateSolidBrush(Color[i]);
    FillRect(hDC, & rect, hBrush);  // three original solid rectangles
    DeleteObject(hBrush);

    BLENDFUNCTION blend = { AC_SRC_OVER, 0, 255/2, 0 }; // alpha 0.5
    blend.SourceConstantAlpha=120;

    AlphaBlend(hDC, 360+((3-i)%3)*size/3, 20+i*size/3, size, size,
               hDC, i*(size+10)+20, 20+size/3, size, size, blend);
  }
}


int main()
{


    HWND HWNDConsoleWindow = GetConsoleWindow();
    HDC HDCConsoleWindow = GetDC(HWNDConsoleWindow);
    Bitmap Bit(100,100);

    do
    {

        SimpleConstantAlphaBlend(HDCConsoleWindow);//draw some rectangles and half of them are AlphaBlend
        //Bit is an image with a circle and the backcolor alpha value is 0 for transparent
        Bit.Draw(HDCConsoleWindow, 100,100,150);
        Sleep(100);

    }while(!GetAsyncKeyState(VK_ESCAPE));
    return 0;
}

results:
https://imgur.com/Pg6F6fg
see the image, seems the 'Bit' is drawed on bottom instead top :(
so why these bad effect?
Last edited on
after several tests, i notice these:
1 - zero will be Alpha Transparent;
2 - 255 will be Opaque;
3 - between 0 and 128, it will be Bottom Alpha Transparent;
4 - between 128 and 255, it will be Top Alpha Transparent.
thanks for all to all
Topic archived. No new replies allowed.