Jump to content
  • Advertisement
Sign in to follow this  
Treiber

TransparentBlt

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

If you intended to correct an error in the post then please contact us.

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
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?

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!