• 13
• 18
• 19
• 27
• 9

# TransparentBlt

This topic is 4785 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I’m trying to learn TransparentBlt() but it’s not working for me. Take a look at my code (from gametutorials.com)
#include <windows.h>
#include <stdio.h>
#pragma comment(lib, "msimg32.lib")							// Include this library so we can call TransparentBlit()

HBITMAP imagem_personagem, imagem_old;
HINSTANCE handle_hinst;
HDC hdc, image_dc;

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
HWND        hwnd;
MSG         msg;
WNDCLASSEX  wndclass;

wndclass.cbSize        = sizeof (wndclass);
wndclass.style         = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc   = WndProc;
wndclass.cbClsExtra    = 0;
wndclass.cbWndExtra    = 0;
wndclass.hInstance     = hInstance;

wndclass.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
wndclass.lpszClassName = "Window Class 1";

RegisterClassEx (&wndclass);

hwnd = CreateWindow ("Window Class 1",
"Janela de teste",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);

ShowWindow (hwnd, iCmdShow);
UpdateWindow (hwnd);
handle_hinst = hInstance;
hdc = GetDC(hwnd);
image_dc = CreateCompatibleDC(hdc);
imagem_old = (HBITMAP)SelectObject(image_dc, imagem_personagem);
//	BitBlt(hdc,200,100,300,200,image_dc,0,0,SRCCOPY);
TransparentBlt(hdc, 200, 100, 300, 200, image_dc, 200, 100, 300, 200, RGB(255,255,255));

while(1)
{
// Check message(s) if there are any
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if(msg.message == WM_QUIT)
break;

TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{

}
}

UnregisterClass("Window Class 1",hInstance);

return msg.wParam ;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{

switch (iMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
}

return DefWindowProc (hwnd, iMsg, wParam, lParam);
}

Well, if I take out the comments of BitBlt() and comment TransparentBlt() it works fine and shows the bitmap normally. But if do the oposite, it shows just a black screen. I’m not sure about all the parameters but as I understood, it should be working. The picture is fine and the only parts which I want to be transparent are the white ones. It has 100 x 100 pixels. I'm not sure yet on how this function works. And is it the only / better function to do transparency? What am I doing wrong?

##### Share on other sites
Sorry for the post. I solved as soon as I pressed the send button. And the image wasn't 100x 100 as I told. It was 140 x 160. And I solved with this line:

TransparentBlt(hdc, 200, 100, 140, 165, image_dc, 0, 0, 140, 165, RGB(255,255,255));

But the second question remains:
is it the only / better function to do transparency?

##### Share on other sites
for the win32 api, TransparentBlt() is the only function for transparency, but you could also use AlphaBlend() for translucency (i.e. 50% transparent)

##### Share on other sites
Quote:
 Original post by geekalertfor the win32 api, TransparentBlt() is the only function for transparency, but you could also use AlphaBlend() for translucency (i.e. 50% transparent)

Hmmm... cool! I'll take a look.
Thank you.