Sign in to follow this  
Treiber

TransparentBlt

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.hIcon         = LoadIcon (NULL, IDI_WINLOGO);	
    wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW);	
															
    wndclass.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
    wndclass.lpszMenuName  = NULL;							
    wndclass.lpszClassName = "Window Class 1";				
    wndclass.hIconSm       = LoadIcon (NULL, IDI_WINLOGO);	

	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;
	imagem_personagem = (HBITMAP)LoadImage(hInstance, "baraka.bmp" ,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
	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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by geekalert
for 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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this