Problems with transparency.

Hello i tried to draw one bitmap on top of another, but the bitmap that is on top
have a tint of bottom bitmap. How do i get rid that tint?

Here is my callback function:
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
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_CREATE:
            {

                picture1 = LoadImage(NULL, "picture1.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);//Loading picture 1 to HBITMAP.
                if(picture1 == NULL){MessageBox(hwnd, "Could not load picture1!", "Error", MB_OK | MB_ICONEXCLAMATION);}
		picture1 = CreateBitmapMask(picture1, RGB(0, 0, 0));//Creating picture 1 mask black is trasparent.
                if(gameInstance.picture1mask == NULL){MessageBox(hwnd, "Could not load picture1 mask!", "Error", MB_OK | MB_ICONEXCLAMATION);}

                picture2 = LoadImage(NULL, "picture2.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);//Loading picture 1 HBITMAP.
                if(picture2 == NULL){MessageBox(hwnd, "Could not load picture2!", "Error", MB_OK | MB_ICONEXCLAMATION);}
                picture2mask = CreateBitmapMask(picture2, RGB(0, 0, 0));//Creating picture 2 mask black is trasparent.
                if(picture2.picture2mask == NULL){MessageBox(hwnd, "Could not load picture2 mask!", "Error", MB_OK | MB_ICONEXCLAMATION);}

            }
            break;
        case WM_TIMER:
            {

                RECT rcClient;

                HDC hdc = GetDC(hwnd);

                GetClientRect(hwnd, &rcClient);

                HDC hdcBuffer = CreateCompatibleDC(hdc);

                HBITMAP hbmBuffer = CreateCompatibleBitmap(hdc, (&rcClient)->right, (&rcClient)->bottom);
                HBITMAP hbmOldBuffer = SelectObject(hdcBuffer, hbmBuffer);

                HDC hdcMem = CreateCompatibleDC(hdc);
                HDC hdcMem2 = CreateCompatibleDC(hdc);

                HBITMAP hbmOld = SelectObject(hdcMem, picture1mask);
                HBITMAP hbmOld2 = SelectObject(hdcMem2, picture2mask);

                FillRect(hdcBuffer, (&rcClient), GetStockObject(NULL_BRUSH));//Clearing screen.

		BitBlt(hdcBuffer, 0, 0, 16, 16, hdcMem, 0, 0, SRCAND);
                SelectObject(hdcMem2, picture1);//Loading picture 1 to hdcMem.
                BitBlt(hdcBuffer, 0, 0, 16, 16, hdcMem, 0, 0, SRCPAINT);//Painting picture1 from hdcMem to hdcBuffer.

                BitBlt(hdcBuffer, 0, 0, 16, 16, hdcMem2, 0, 0, SRCAND);
                SelectObject(hdcMem2, picture2);//Loading picture 2 to hdcMem2.
                BitBlt(hdcBuffer, 0, 0, 16, 16, hdcMem2, 0, 0, SRCPAINT);//Painting picture2 from hdcMem2 to hdcBuffer.


                BitBlt(hdc, 0, 0, (&rcClient)->right, (&rcClient)->bottom, hdcBuffer, 0, 0, SRCCOPY);//Now that we are done with drawing we will coppy the whole thing to screen.


                SelectObject(hdcMem, hbmOld);
                DeleteDC(hdcMem);
                SelectObject(hdcMem2, hbmOld2);
                DeleteDC(hdcMem2);

                SelectObject(hdcBuffer, hbmOldBuffer);
                DeleteDC(hdcBuffer);
                ReleaseDC(hwnd, hdc);
                DeleteObject(hbmBuffer);

            }
            break;
        case WM_CLOSE:
            {
            DestroyWindow(hwnd);
            }
        break;
        case WM_DESTROY:
            {
            KillTimer(hwnd, ID_TIMER);
            DeleteObject(picture1);
            DeleteObject(picture1mask);
            DeleteObject(picture2);
            DeleteObject(picture2mask);
            PostQuitMessage(0);
            }
        break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}


Thank you for your help.
Last edited on
closed account (E0p9LyTq)
TransparentBlt function (Windows) - msdn.microsoft.com
https://msdn.microsoft.com/en-us/library/windows/desktop/dd145141(v=vs.85).aspx

If you use this function you need to include the msimg32.lib library, it isn't included automatically.
Tankyou FurryGuy you soved my problem!
That TransparentBlt function is a bit difficult to set up for but now everything is ok.

This is how my code look like now:
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

// declare this 7 lines before your #include windows.h 
#ifndef WINVER
#define WINVER 0x0501
#else
#if defined(_WIN32_WINNT) && (WINVER 0x0400)
#error WINVER setting conflicts with _WIN32_WINNT setting
#endif
#endif


LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_CREATE:
            {

                picture1 = LoadImage(NULL, "picture1.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);//Loading picture 1 to HBITMAP.
                if(picture1 == NULL){MessageBox(hwnd, "Could not load picture1!", "Error", MB_OK | MB_ICONEXCLAMATION);}

                picture2 = LoadImage(NULL, "picture2.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);//Loading picture 2 HBITMAP.
                if(picture2 == NULL){MessageBox(hwnd, "Could not load picture2!", "Error", MB_OK | MB_ICONEXCLAMATION);}

            }
            break;
        case WM_TIMER:
            {

                RECT rcClient;

                HDC hdc = GetDC(hwnd);

                GetClientRect(hwnd, &rcClient);

                HDC hdcBuffer = CreateCompatibleDC(hdc);

                HBITMAP hbmBuffer = CreateCompatibleBitmap(hdc, (&rcClient)->right, (&rcClient)->bottom);
                HBITMAP hbmOldBuffer = SelectObject(hdcBuffer, hbmBuffer);

                HDC hdcMem = CreateCompatibleDC(hdc);
                HDC hdcMem2 = CreateCompatibleDC(hdc);

                FillRect(hdcBuffer, (&rcClient), GetStockObject(NULL_BRUSH));//Clearing screen.


                SelectObject(hdcMem, picture1);//Loading picture 1 to hdcMem.
                TransparentBlt(hdcBuffer, 0, 0, 16, 16, hdcMem, 0, 0, 16, 16, RGB(0, 0, 0));

                SelectObject(hdcMem2, picture2);//Loading picture 2 to hdcMem2.
                TransparentBlt(hdcBuffer, 0, 0, 16, 16, hdcMem2, 0, 0, 16, 16, RGB(0, 0, 0));


                BitBlt(hdc, 0, 0, (&rcClient)->right, (&rcClient)->bottom, hdcBuffer, 0, 0, SRCCOPY);//Now that we are done with drawing we will coppy the whole thing to screen.



                DeleteDC(hdcMem);
                DeleteDC(hdcMem2);

                SelectObject(hdcBuffer, hbmOldBuffer);
                DeleteDC(hdcBuffer);
                ReleaseDC(hwnd, hdc);
                DeleteObject(hbmBuffer);

            }
            break;
        case WM_CLOSE:
            {
            DestroyWindow(hwnd);
            }
        break;
        case WM_DESTROY:
            {
            KillTimer(hwnd, ID_TIMER);
            DeleteObject(picture1);
            DeleteObject(picture2);
            PostQuitMessage(0);
            }
        break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}


You will need to inlcude more stuff for TransparentBlt to work here is the name of post and link:
How do I include additional libraries with Code::Blocks?
http://www.cplusplus.com/forum/windows/42673/

Last edited on
closed account (E0p9LyTq)
msimg32.lib is the Visual Studio specific library name needed when using TransparentBlt(), you didn't say you were using C::B in your initial post. libmsimg32.lib is the name GCC compilers use.

How do I include additional libraries with Code::Blocks?

Why not do a web search for information on using the C::B software?

using libraries with Code::Blocks
https://duckduckgo.com/?q=using+libraries+with+code%3A%3Ablocks&t=ffsb&ia=qa
Topic archived. No new replies allowed.