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
|