TransparentBlt

Started by
2 comments, last by Treiber 19 years, 2 months ago
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?
Gimme fuel, gimme fire, gimme that which I desire.Metallica / Fuel
Advertisement
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?
Gimme fuel, gimme fire, gimme that which I desire.Metallica / Fuel
for the win32 api, TransparentBlt() is the only function for transparency, but you could also use AlphaBlend() for translucency (i.e. 50% transparent)
A JPEG is worth a thousand and twenty four DWORD's. ;)
Note: Due to vacationing my website will not be updated till late-August. Of course I still have internet, but who wants to program during a vacation?
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.
Gimme fuel, gimme fire, gimme that which I desire.Metallica / Fuel

This topic is closed to new replies.

Advertisement