|
Hi, I am trying to write code in clear WinAPI, but I am doing some mistake (probably I wasn't able to understand the MFC code):
#ifndef WINVER
# define WINVER 0x0502
#endif
#ifndef _WIN32_WINNT
# define _WIN32_WINNT 0x0502
#endif
#ifndef _WIN32_WINDOWS
# define _WIN32_WINDOWS 0x0410
#endif
#ifndef _WIN32_IE
# define _WIN32_IE 0x0600
#endif
#include <windows.h>
#include "path_to_CxImage_library/ximage.h"
#define MAIN_CLASS_NAME L"AlphaBlendingWindowClass"
HWND mainWindow;
CxImage image;
LRESULT CALLBACK WindowProcMain(HWND hwnd, UINT uMsg, WPARAM wParam,
LPARAM lParam)
{
switch ( uMsg )
{
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
image.Draw(hdc, 0, 0, 200, 200, NULL, true);
EndPaint(hwnd, &ps);
} break;
case WM_LBUTTONDOWN:
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
void Update() {
POINT position = {100,100};
SIZE size = {200, 200};
POINT src = {0, 0};
HDC screenDC = GetDC(NULL);
HDC memDC = CreateCompatibleDC(NULL);
UINT32 *data = new UINT32[size.cx*size.cy];
ZeroMemory(data, sizeof(UINT32)*size.cx*size.cy);
HBITMAP newBitmap = CreateBitmap(size.cx, size.cy, 1, 32, data);
HBITMAP oldBitmap = (HBITMAP)SelectObject(memDC, newBitmap);
image.Draw(memDC, 0, 0, 200, 200, NULL, true);
BLENDFUNCTION bf = {AC_SRC_OVER, 0, 255, AC_SRC_ALPHA};
if (!UpdateLayeredWindow(mainWindow, screenDC, &position, &size, memDC, &src, 0, &bf, ULW_ALPHA)) {
MessageBox(NULL, L"Window wasn't updated!", L"Error", MB_OK | MB_ICONERROR);
}
delete[] data;
SelectObject(memDC, oldBitmap);
DeleteObject(newBitmap);
DeleteDC(memDC);
ReleaseDC(NULL, screenDC);
}
BOOL InitApp(HINSTANCE instance) {
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wc.hInstance = instance;
wc.lpfnWndProc = WindowProcMain;
wc.lpszClassName = MAIN_CLASS_NAME;
wc.lpszMenuName = NULL;
wc.style = CS_HREDRAW | CS_VREDRAW;
if ( !RegisterClassEx(&wc) )
return FALSE;
mainWindow = CreateWindowEx(WS_EX_LAYERED, MAIN_CLASS_NAME, L"AlphaBlendedWindow",
WS_VISIBLE | WS_POPUP, 100, 100, 200, 200, NULL, NULL, instance, NULL);
if ( mainWindow == NULL ) {
MessageBox(NULL, L"Cannot create window!", L"Error", MB_OK | MB_ICONERROR);
return FALSE;
}
if (!image.Load(L"../debug/image.png")) {
MessageBox(NULL, L"Cannot load image!", L"Error", MB_OK | MB_ICONERROR);
}
Update();
return TRUE;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst, LPSTR lpCmdLine,
int nShow) {
MSG msg;
InitApp(hInstance);
while ( GetMessage(&msg, NULL, 0, 0) ) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
</windows.h> Thanks for any help
|
|
|
|
|
me too, I write an app with clean api, but also something wrong...
|
|
|
|
|
How can I draw text on the alpha window? Do I have to draw the text into the bitmap and then premultiply it after ? anyone have an example of how to do this:?
|
|
|
|
|
Ok I figured out how to draw text and whatever I wanted on the window now my premultiplying everything, but now how can I put windows controls on the window?
|
|
|
|
|
Hi,
This is real world article. How to use controls (ie, button, editbox, ...) in layered windows. Any help?
Thanks,
Pherosiden
|
|
|
|
|
I need the UNICODE version of this Code and i need unicode version of imagdecoder.dll
So far, i have followed the instructions and have not been able to build a unicode version. Can anyone help me?
|
|
|
|
|
I have MS Visual Studio 2005 (no SP) and "anydec.h" is missing. I couldn't find it over internet, so please someone send me it, armaza@mail.ru. thancks.
|
|
|
|
|
This is same my issue,maybe i guess these header files,
#include "anydec.h"
#include "winbmp.h"
#include "dibsect.h"
must to be added the prefix "pl" to it.
#include "planydec.h"
#include "plwinbmp.h"
#include "pldibsect.h"
|
|
|
|
|
Hi man
Did u look at date posted? 2007 year
I use Graphics32 library with Delphi to work with translucent windows, also wrote an Aero Glass emulation under XP 4 fun
10x anyway 4 replay
|
|
|
|
|
so past year 2007 ^^ i knew,
VC++ also release new feature pack..
i found no comments and just reply
to help someone met this issue ,correct imgdecode project source
|
|
|
|
|
The exe file works great but when i open the dsw (project file) in visual c++ 6.0
and compile it i get about 5 compiler errors saying "identifier undeclared".
I tried everything but nothing works.
|
|
|
|
|
Hey,
gr8 work man... actually have implemented alpha compositions equations as well.. but i dont have enough test images, specially the ones with some meaningful per-pixel alpha.
I downloaded what is here on this page but would be gr8 if you could share more or give me pointer to get'em.
Regards,
Ninad
|
|
|
|
|
Dude
this is good... copied or not copied this was the only excellent working example of Pixel Blending of Dialog which I could find anywhere.
And after all we all learn from each other. But what counts is the effort put up by someone to actually post this on the internet is awesome. Remember, Man is a mimicking animal
Good going dude
Glax
|
|
|
|
|
I don't know how to add control to the dialog when i use UpdateLayeredWindow API.Because WM_PAINT message can not send.Can you give me some methods to solve it.Thank you!
|
|
|
|
|
Hi
I have an application that constantly is updating its image.
Now when I place a layered transparent window on the top of it -
I made one with your application using color keying instead of alpha -
then I observe with Windows Task Manager that the CPU time of my
application goes from 3 to 5% to 20 to 25%.
Now my question is, is Window really that bad in handling layered windows
or am I doing something wrong.
Can I make an overlay with markers/cursors for my application in another
way?
Jens M P
|
|
|
|
|
I've been messing with your code and first off thank you for enlightening me on the use of per-pixel alpha blending But when I try to write text on the window via Device Context I can't "erase" the old text. Is there any special way of resetting the picture back to its default or just a specific region? Or is there maybe a better way to write text on a layered window altogether?
Thank you in advance
-ObsidianX
|
|
|
|
|
Hello there,
I also like this soo much... but can u plese tell me that how did u write text on this Layered dialog using GDI.
PLease tell me !!
Thanks you.
|
|
|
|
|
This is a little late, but I just figured it out. You have to do your GDI drawing to an offscreen bitmap/DC and then when you are done drawing you must premultiply the bitmap pixels, then you must use the AlphaBlend function to finally blit it all to the orignal bitmap and call DoUpdateDummyDialog.
|
|
|
|
|
D:\perpixelalpha\perpixelalphaDlg.cpp(251) : error C2065: 'WS_EX_LAYERED' : undeclared identifier
D:\perpixelalpha\perpixelalphaDlg.cpp(283) : error C2065: 'AC_SRC_ALPHA' : undeclared identifier
D:\perpixelalpha\perpixelalphaDlg.cpp(287) : error C2039: 'UpdateLayeredWindow' : is not a member of '`global namespace''
D:\perpixelalpha\perpixelalphaDlg.cpp(287) : error C2065: 'UpdateLayeredWindow' : undeclared identifier
D:\perpixelalpha\perpixelalphaDlg.cpp(288) : error C2065: 'ULW_ALPHA' : undeclared identifier
I'm not too conversant with MFC, but I'm getting these all the time, no matter what. I do have the sdk, I try putting the winuser.h everywhere, it's not taken apparently. It's a mystery to me how this thing gets built.
|
|
|
|
|
put #define _WIN32_WINNT 0x0500 before #include "windows.h"
should work!!
|
|
|
|
|
This article is bad! Writer has stolen code he does not under stand! Article also does not show proper way to do this.
Code he use for premultiply alpha is stolen from Feng Yuans book. Image drawing is from Free Image library.
But even worse, here is proof that this article is broken. Try to draw an big line across image. Make one line of solid blue (rgb(0, 0, 255)), like this in CPerpixelalphaDlg::DoUpdateDummyDialog
BITMAP bmpInfo;<br />
bmp.GetBitmap(&bmpInfo);<br />
<br />
CPen kPen;<br />
kPen.CreatePen(PS_SOLID, 4, RGB(0, 0, 255));<br />
dcMemory.SelectObject(&kPen);<br />
dcMemory.MoveTo(0, 0);<br />
dcMemory.LineTo(bmpInfo.bmWidth, bmpInfo.bmHeight);<br />
<br />
CRect rectDlg;<br />
wnd.GetWindowRect(rectDlg);
You see what happen....
|
|
|
|
|
George987654321 wrote: Writer has stolen code
What? Why you say it was stolen?
George987654321 wrote: Article also does not show proper way to do this.
Care to show us the way?
George987654321 wrote: Code he use for premultiply alpha is stolen from Feng Yuans book.
Nonsense...
George987654321 wrote: Image drawing is from Free Image library.
More nonsense... the PSP loading code is mine; the other formats (PNG, etc) is from paintlib.
George987654321 wrote: But even worse, here is proof that this article is broken.
Oh, and why is that?
----
Rui Lopes
|
|
|
|
|
This is all I need say:
http://www.fengyuan.com/article/alphablend.html
Rui Godinho Lopes wrote: George987654321 wrote:
But even worse, here is proof that this article is broken.
Oh, and why is that?
Why? Add code I show in first message to draw line on image. It not work, no blue line acorss image. Why? Well I leave you to understand problem, if you really wrote any code and not copy pasted it, you should understand it.
Only thing your code show is how to use paintlib to load PNG.
|
|
|
|
|
You can say whatever you want... its all nonsenses.
George987654321 wrote: Why? Well I leave you to understand problem, if you really wrote any code and not copy pasted it, you should understand it.
I'll bite, its quite easy to explain... you are using a GDI Pen that doesn't have support for the alpha channel (it sets alpha to 0, which is transparent); you are then drawing it on a memory dc that is expected to have its RGB channels premultiplied with the alpha (again, the GDI Pen does not do that)... then when UpdateLayeredWindow uses it for alpha blending with the desktop, well, you see what you see, and it can be easily explained by looking at the composition algorithm...
----
Rui Lopes
|
|
|
|
|
This looks like the answer of a person who understands the code. I enjoyed the article. Keep on writing man.
|
|
|
|
|