Sign in to follow this  

[.net] Using UpdateLayeredWindows and GDI for per pixel alpha

This topic is 4303 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

Hi, I am trying to draw png files so I can see my desktop through them, and am using UpdateLayeredWindow to do this from a basic, non-altered png, using this code: Note, TheBitmap is a GDI+ bitmap loaded from a png file a little earlier.
	HDC screenDC( GetDC(0) );
	POINT sourcePos = {0};
	HDC sourceDC( CreateCompatibleDC(screenDC) );

	HBITMAP bufferBitmap = {0};
	TheBitmap->GetHBITMAP(0, &bufferBitmap);

	HBITMAP oldBmpSelInDC;
	
	oldBmpSelInDC = (HBITMAP)SelectObject(sourceDC, bufferBitmap);

	POINT pos = {100, 100};
	SIZE size = {TheBitmap->GetWidth(), TheBitmap->GetHeight()};

	BLENDFUNCTION blendFunction = {0};
	blendFunction.BlendOp = AC_SRC_OVER;
	blendFunction.SourceConstantAlpha = 255;
	blendFunction.AlphaFormat = AC_SRC_ALPHA;

	COLORREF colorKey( RGB(255,0,255) );
	DWORD flags( ULW_ALPHA);

	UpdateLayeredWindow(hwnd, screenDC, &pos, & size, sourceDC, &sourcePos,
	colorKey, &blendFunction, flags);

	SelectObject(sourceDC, oldBmpSelInDC);
	DeleteObject(bufferBitmap);
	DeleteDC(sourceDC);
	
	ReleaseDC(0, screenDC);
Now, I want to alter the bitmap by stretching it, etc, which I have done before using gdi commands and the WM_PAINT prompt. But I don't know how to do this now I am using UpdateLayeredWindow. Should I create a hdc and paint on it using gdi? Then what? How do I use that hdc in the above code? I would really appreciate any help, I have been struggling with this for quite a while. I do realize that this is a fairly tricky subject though, my research has taken a week, and i am still only at the stage where I can do per pixel alhpa with a non-altered image, but not with an altered png.

Share this post


Link to post
Share on other sites
You don't use WM_PAINT now (just ignore it).
Your window is cached somewhere in memory, so you can move it around and cover/uncover it with other windows, without redrawing contents.

If you wish to update your window (that is, alter your PNG) just call once again UpdateLayeredWindow with a new bitmap. If you stretch it, don't forget to resize the window.

Share this post


Link to post
Share on other sites
Thanks for the reply.

But what Iam wondering is, how do I stretch that png? Before I was using layeredwindows, I would use gdi calls, which worked great.

But now, how do I do it. Do I still use gdi+ calls? If so, where do I get the bitmap from to set updatelayeredwindow with? My png file held in an image source is no longer what I want to display, instead it is an altered version of it. So the image I want dispalyed is no longer held in an image or bitmap class. I dont know where it is stored.

I have searched and searched, but this topic is really not talked about much. Or rather, it is discussed alot if you're a delphi, or vb programmer, but there is so little c++ discussion on it. And what there is, does not cover my problem exactly.

I'll keep struggling on with it, and hope something fits into place in my brain :). thanks for your reply.

Share this post


Link to post
Share on other sites
No one can helpw ith this?

Can't say I am surprised, the amount of useful information on this is ridiculously small, and generally for vb and delphi.

If anyone has seen a book using layered windows and gdi together for this sort of effect, I would love to hear about it, and the title of the book so I can buy the thing :)

Share this post


Link to post
Share on other sites
There isn't much information because it isn't really useful.
Not only you must have Win2000/XP to use layers, but also you can't have controls on such windows (you have them, but they aren't drawn).

Were you using GDI or GDI+?

If you were using GDI to stretch your picture, that's definitely not the way to do it. Better manually implement bilinear filtering or something.

If it's GDI+, then it's stretching is quite OK. What do you mean you don't know where it's stored? You should have some object.
Basically, if you managed to use an unaltered PNG, I don't see any reason for not being to use an altered one.

I'm doing some stuff with GDI and layered windows, but I'm working in VC6 (and use a lot of pointers for GDI). No books, just a lot of MSDN :-)

Share this post


Link to post
Share on other sites

This topic is 4303 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.

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