2004-10-21 04:48:46 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#ifndef AC_SRC_ALPHA
|
|
|
|
#define AC_SRC_ALPHA (0x1)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
HINSTANCE HInst;
|
|
|
|
const char* WndClassName = "GMainWnd";
|
2005-05-07 21:24:31 +00:00
|
|
|
LRESULT CALLBACK MainWndProc(HWND HWnd, UINT Msg, WPARAM WParam,
|
2004-10-21 04:48:46 +00:00
|
|
|
LPARAM LParam);
|
2005-05-07 21:24:31 +00:00
|
|
|
|
2004-10-21 04:48:46 +00:00
|
|
|
int APIENTRY WinMain(HINSTANCE HInstance, HINSTANCE HPrevInstance,
|
|
|
|
LPTSTR lpCmdLine, int nCmdShow)
|
|
|
|
{
|
|
|
|
WNDCLASS wc;
|
|
|
|
MSG msg;
|
|
|
|
|
|
|
|
HInst = HInstance;
|
|
|
|
|
|
|
|
memset(&wc, 0, sizeof(WNDCLASS));
|
2005-05-07 21:24:31 +00:00
|
|
|
|
2004-10-21 04:48:46 +00:00
|
|
|
wc.style = CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS;
|
|
|
|
wc.lpfnWndProc = MainWndProc;
|
|
|
|
wc.hInstance = HInstance;
|
|
|
|
wc.hCursor = LoadCursor(NULL, (LPCTSTR)IDC_ARROW);
|
|
|
|
/* wc.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_BTNFACE + 1); */
|
|
|
|
wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
|
|
|
|
wc.lpszClassName = WndClassName;
|
|
|
|
|
|
|
|
if (RegisterClass(&wc))
|
|
|
|
{
|
2005-05-07 21:24:31 +00:00
|
|
|
HWND HWnd =
|
2004-10-21 04:48:46 +00:00
|
|
|
CreateWindow(
|
|
|
|
WndClassName, TEXT("AlphaBlend Rendering Demo"),
|
2005-05-07 21:24:31 +00:00
|
|
|
WS_OVERLAPPED | WS_SYSMENU | WS_CAPTION |
|
2004-10-21 04:48:46 +00:00
|
|
|
WS_VISIBLE | WS_CLIPSIBLINGS,
|
|
|
|
0, 0, 320, 430,
|
|
|
|
NULL, NULL, HInst, NULL
|
|
|
|
);
|
2005-05-07 21:24:31 +00:00
|
|
|
|
2004-10-21 04:48:46 +00:00
|
|
|
if (HWnd)
|
|
|
|
{
|
|
|
|
ShowWindow(HWnd, nCmdShow);
|
|
|
|
UpdateWindow(HWnd);
|
|
|
|
|
|
|
|
while (GetMessage(&msg, NULL, 0, 0))
|
|
|
|
{
|
|
|
|
TranslateMessage(&msg);
|
|
|
|
DispatchMessage(&msg);
|
2005-05-07 21:24:31 +00:00
|
|
|
}
|
2004-10-21 04:48:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* image related */
|
|
|
|
BITMAP bmp;
|
|
|
|
LPCSTR filename = TEXT("lena.bmp");
|
|
|
|
HDC HMemDC = NULL, HMemDC2 = NULL;
|
|
|
|
HBITMAP HOldBmp = NULL;
|
|
|
|
PVOID pBmpBits = NULL;
|
|
|
|
HBITMAP H32BppBitmap = NULL;
|
|
|
|
BITMAPINFO bmpi;
|
|
|
|
|
|
|
|
BOOL ConvertBitmapTo32Bpp(HDC hDC, BITMAP *bmp)
|
|
|
|
{
|
|
|
|
ZeroMemory(&bmpi, sizeof(BITMAPINFO));
|
|
|
|
bmpi.bmiHeader.biSize = sizeof(BITMAPINFO);
|
|
|
|
bmpi.bmiHeader.biWidth = bmp->bmWidth;
|
|
|
|
bmpi.bmiHeader.biHeight = bmp->bmHeight;
|
|
|
|
bmpi.bmiHeader.biPlanes = 1;
|
|
|
|
bmpi.bmiHeader.biBitCount = 32;
|
|
|
|
bmpi.bmiHeader.biCompression = BI_RGB;
|
|
|
|
bmpi.bmiHeader.biSizeImage = 4 * bmpi.bmiHeader.biWidth * bmpi.bmiHeader.biHeight;
|
|
|
|
H32BppBitmap = CreateDIBSection(hDC, &bmpi, DIB_RGB_COLORS, &pBmpBits, 0, 0);
|
|
|
|
if(H32BppBitmap)
|
|
|
|
{
|
|
|
|
HBITMAP bmpalpha;
|
|
|
|
SelectObject(hDC, H32BppBitmap);
|
|
|
|
BitBlt(hDC, 0, 0, bmp->bmWidth, bmp->bmHeight, HMemDC, 0, 0, SRCCOPY);
|
2005-05-07 21:24:31 +00:00
|
|
|
|
2004-10-21 04:48:46 +00:00
|
|
|
/* load and apply alpha channel */
|
2005-05-07 21:24:31 +00:00
|
|
|
bmpalpha = LoadImage(HInst, TEXT("lenaalpha.bmp"), IMAGE_BITMAP,
|
2004-10-21 04:48:46 +00:00
|
|
|
0, 0, LR_LOADFROMFILE);
|
|
|
|
if(bmpalpha)
|
|
|
|
{
|
|
|
|
COLORREF *col = pBmpBits;
|
|
|
|
int x, y;
|
|
|
|
HDC hdcTemp = CreateCompatibleDC(NULL);
|
|
|
|
if(!hdcTemp)
|
|
|
|
{
|
|
|
|
DeleteObject(bmpalpha);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
SelectObject(hdcTemp, bmpalpha);
|
2005-05-07 21:24:31 +00:00
|
|
|
|
2004-10-21 04:48:46 +00:00
|
|
|
for(y = 0; y < bmp->bmHeight; y++)
|
|
|
|
{
|
|
|
|
for(x = 0; x < bmp->bmWidth; x++)
|
|
|
|
{
|
|
|
|
COLORREF Color = (COLORREF)GetRValue(GetPixel(hdcTemp, x, y)) << 24;
|
|
|
|
*col++ |= Color;
|
|
|
|
}
|
|
|
|
}
|
2005-05-07 21:24:31 +00:00
|
|
|
|
2004-10-21 04:48:46 +00:00
|
|
|
DeleteObject(bmpalpha);
|
|
|
|
DeleteDC(hdcTemp);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2005-05-07 21:24:31 +00:00
|
|
|
LRESULT CALLBACK MainWndProc(HWND HWnd, UINT Msg, WPARAM WParam,
|
2004-10-21 04:48:46 +00:00
|
|
|
LPARAM LParam)
|
|
|
|
{
|
|
|
|
switch (Msg)
|
|
|
|
{
|
|
|
|
case WM_CREATE:
|
2005-05-07 21:24:31 +00:00
|
|
|
{
|
2004-10-21 04:48:46 +00:00
|
|
|
/* create a memory DC */
|
|
|
|
HMemDC = CreateCompatibleDC(NULL);
|
|
|
|
if (HMemDC)
|
|
|
|
{
|
|
|
|
/* load a bitmap from file */
|
2005-05-07 21:24:31 +00:00
|
|
|
HBITMAP HBmp =
|
2004-10-21 04:48:46 +00:00
|
|
|
/* static_cast<HBITMAP> */(
|
2005-05-07 21:24:31 +00:00
|
|
|
LoadImage(HInst, filename, IMAGE_BITMAP,
|
2004-10-21 04:48:46 +00:00
|
|
|
0, 0, LR_LOADFROMFILE)
|
2005-05-07 21:24:31 +00:00
|
|
|
);
|
2004-10-21 04:48:46 +00:00
|
|
|
if (HBmp)
|
2005-05-07 21:24:31 +00:00
|
|
|
{
|
2004-10-21 04:48:46 +00:00
|
|
|
/* extract dimensions of the bitmap */
|
|
|
|
GetObject(HBmp, sizeof(BITMAP), &bmp);
|
|
|
|
|
|
|
|
/* associate the bitmap with the memory DC */
|
|
|
|
/* HOldBmp = static_cast<HBITMAP> */
|
|
|
|
(SelectObject(HMemDC, HBmp)
|
|
|
|
);
|
|
|
|
HMemDC2 = CreateCompatibleDC(NULL);
|
|
|
|
if(!ConvertBitmapTo32Bpp(HMemDC2, &bmp))
|
|
|
|
{
|
|
|
|
PostQuitMessage(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2005-05-07 21:24:31 +00:00
|
|
|
}
|
2004-10-21 04:48:46 +00:00
|
|
|
}
|
|
|
|
case WM_PAINT:
|
|
|
|
{
|
|
|
|
PAINTSTRUCT ps;
|
|
|
|
BLENDFUNCTION BlendFunc;
|
|
|
|
HDC Hdc = BeginPaint(HWnd, &ps);
|
|
|
|
#if 0
|
|
|
|
try
|
|
|
|
#endif
|
|
|
|
{
|
2005-05-07 21:24:31 +00:00
|
|
|
|
2004-10-21 04:48:46 +00:00
|
|
|
BlendFunc.BlendOp = AC_SRC_OVER;
|
|
|
|
BlendFunc.BlendFlags = 0;
|
|
|
|
BlendFunc.SourceConstantAlpha = 128;
|
|
|
|
BlendFunc.AlphaFormat = 0;
|
2005-05-07 21:24:31 +00:00
|
|
|
|
|
|
|
BitBlt(Hdc, 100, 90,
|
2004-10-21 04:48:46 +00:00
|
|
|
bmp.bmWidth, bmp.bmHeight,
|
2005-05-07 21:24:31 +00:00
|
|
|
HMemDC2, 0, 0,
|
|
|
|
SRCCOPY);
|
2004-10-21 04:48:46 +00:00
|
|
|
GdiAlphaBlend(Hdc, 0, 0, bmp.bmWidth, bmp.bmHeight,
|
|
|
|
HMemDC2, 0, 0, bmp.bmWidth, bmp.bmHeight,
|
|
|
|
BlendFunc);
|
|
|
|
GdiAlphaBlend(Hdc, bmp.bmWidth - 15, 10, bmp.bmWidth / 2, bmp.bmHeight / 2,
|
|
|
|
HMemDC2, 0, 0, bmp.bmWidth, bmp.bmHeight,
|
|
|
|
BlendFunc);
|
2005-05-07 21:24:31 +00:00
|
|
|
|
2004-10-21 04:48:46 +00:00
|
|
|
BlendFunc.SourceConstantAlpha = 255;
|
|
|
|
BlendFunc.AlphaFormat = AC_SRC_ALPHA;
|
2005-05-07 21:24:31 +00:00
|
|
|
|
2004-10-21 04:48:46 +00:00
|
|
|
GdiAlphaBlend(Hdc, 140, 200, bmp.bmWidth, bmp.bmHeight,
|
|
|
|
HMemDC2, 0, 0, bmp.bmWidth, bmp.bmHeight,
|
|
|
|
BlendFunc);
|
|
|
|
GdiAlphaBlend(Hdc, 20, 210, (bmp.bmWidth / 3) * 2, (bmp.bmHeight / 3) * 2,
|
|
|
|
HMemDC2, 0, 0, bmp.bmWidth, bmp.bmHeight,
|
|
|
|
BlendFunc);
|
2005-05-07 21:24:31 +00:00
|
|
|
}
|
2004-10-21 04:48:46 +00:00
|
|
|
#if 0
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
EndPaint(HWnd, &ps);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
EndPaint(HWnd, &ps);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case WM_DESTROY:
|
|
|
|
{
|
|
|
|
/* clean up */
|
|
|
|
DeleteObject(SelectObject(HMemDC, HOldBmp));
|
|
|
|
DeleteDC(HMemDC);
|
|
|
|
DeleteDC(HMemDC2);
|
|
|
|
|
|
|
|
PostQuitMessage(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return DefWindowProc(HWnd, Msg, WParam, LParam);
|
|
|
|
}
|