#include #include #include #include using namespace Framework; struct Image { HDC hdc; Bild *bild; HBITMAP hBitmap; BITMAPINFO info; int pitch; unsigned char *pixels; }; Image icon; bool exitB = 0; void ImageDestroy( Image *image ) { if( !image ) return; image->pitch = 0; if( image->hBitmap ) { DeleteObject( image->hBitmap ); image->hBitmap = NULL; } if( image->hdc ) { DeleteDC( image->hdc ); image->hdc = NULL; } memset( &image->info, 0, sizeof( image->info ) ); if( image->bild ) image->bild->release(); } bool ImageCreate( Image *image, const char *pfad, const char *name ) { if( !image ) return 0; LTDBDatei *bilder = new LTDBDatei(); bilder->setDatei( new Text( pfad ) ); bilder->leseDaten( 0 ); image->bild = bilder->laden( 0, new Text( name ) ); bilder->release(); if( !image->bild ) return 0; image->pixels = 0; image->pitch = ( ( image->bild->getBreite() * 32 + 31 ) & ~31 ) >> 3; image->hdc = CreateCompatibleDC( 0 ); if( !image->hdc ) return 0; memset( &image->info, 0, sizeof( image->info ) ); image->info.bmiHeader.biSize = sizeof( image->info.bmiHeader ); image->info.bmiHeader.biBitCount = 32; image->info.bmiHeader.biWidth = image->bild->getBreite(); image->info.bmiHeader.biHeight = -image->bild->getHeight(); image->info.bmiHeader.biCompression = BI_RGB; image->info.bmiHeader.biPlanes = 1; image->hBitmap = CreateDIBSection( image->hdc, &image->info, DIB_RGB_COLORS, (void**)&image->pixels, NULL, 0 ); if( !image->hBitmap ) { ImageDestroy( image ); return 0; } GdiFlush(); return 1; } void ImagePreMultAlpha( Image *image ) { unsigned char *pPixel = 0; if( image->bild->getBreite() * 4 == image->pitch ) { int i = 0; int totalBytes = image->bild->getBreite() * image->bild->getHeight() * 4; for( i = 0; i < totalBytes; i += 4 ) { pPixel = &image->pixels[ i ]; pPixel[ 0 ] = (unsigned char)( pPixel[ 0 ] * ( (float)pPixel[ 3 ] / 255.0f ) ); pPixel[ 1 ] = (unsigned char)( pPixel[ 1 ] * ( (float)pPixel[ 3 ] / 255.0f ) ); pPixel[ 2 ] = (unsigned char)( pPixel[ 2 ] * ( (float)pPixel[ 3 ] / 255.0f ) ); } } else { int x = 0; int y = 0; for( y = 0; y < image->bild->getHeight(); ++y ) { for( x = 0; x < image->bild->getBreite(); ++x ) { pPixel = &image->pixels[ ( y * image->pitch ) + ( x * 4 ) ]; pPixel[ 0 ] = (unsigned char)( pPixel[ 0 ] * ( (float)pPixel[ 3 ] / 255.0f ) ); pPixel[ 1 ] = (unsigned char)( pPixel[ 1 ] * ( (float)pPixel[ 3 ] / 255.0f ) ); pPixel[ 2 ] = (unsigned char)( pPixel[ 2 ] * ( (float)pPixel[ 3 ] / 255.0f ) ); } } } } bool ImageImport( Image *image ) { BYTE *pRow = NULL; if( !image ) return 0; if( !ImageCreate( image, "data/bilder/game.ltdb", "starticon.png" ) ) return 0; int *buffer = image->bild->getBuffer(); for( int i = 0; i < image->bild->getHeight(); i++ ) { pRow = &image->pixels[ i * image->pitch ]; for( int i2 = 0; i2 < image->bild->getBreite(); i2++ ) { pRow[ i2 * 4 ] = (unsigned char)( ( buffer[ i2 + i * image->bild->getBreite() ] ) & 0xFF ); pRow[ i2 * 4 + 1 ] = (unsigned char)( ( buffer[ i2 + i * image->bild->getBreite() ] >> 8 ) & 0xFF ); pRow[ i2 * 4 + 2 ] = (unsigned char)( ( buffer[ i2 + i * image->bild->getBreite() ] >> 16 ) & 0xFF ); pRow[ i2 * 4 + 3 ] = (unsigned char)( ( buffer[ i2 + i * image->bild->getBreite() ] >> 24 ) & 0xFF ); } } ImagePreMultAlpha( image ); return 1; } void InitLayeredWindow( HWND hWnd ) { HDC hdc = NULL; if( hdc = GetDC( hWnd ) ) { HGDIOBJ hPrevObj = NULL; POINT ptDest = { 0, 0 }; POINT ptSrc = { 0, 0 }; SIZE client = { icon.bild->getBreite(), icon.bild->getHeight() }; BLENDFUNCTION blendFunc = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA }; hPrevObj = SelectObject( icon.hdc, icon.hBitmap ); ClientToScreen( hWnd, &ptDest ); UpdateLayeredWindow( hWnd, hdc, &ptDest, &client, icon.hdc, &ptSrc, 0, &blendFunc, ULW_ALPHA ); SelectObject( icon.hdc, hPrevObj ); ReleaseDC( hWnd, hdc ); } } LRESULT CALLBACK Proc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ) { return DefWindowProc( hWnd, msg, wParam, lParam ); } int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd ) { WNDCLASSEX wcl = { 0 }; wcl.cbSize = sizeof( wcl ); wcl.style = CS_HREDRAW | CS_VREDRAW; wcl.lpfnWndProc = Proc; wcl.cbClsExtra = 0; wcl.cbWndExtra = 0; wcl.hInstance = hInstance; wcl.hIcon = 0; wcl.hCursor = 0; wcl.hbrBackground = 0; wcl.lpszMenuName = 0; wcl.lpszClassName = TEXT( "LayeredWindowClass" ); wcl.hIconSm = 0; if( !ImageImport( &icon ) ) { WMessageBox( 0, new Text( "Fehler" ), new Text( "data/bilder/game.ltdb/starticon.png konnte nicht geladen werden!" ), MB_ICONERROR ); return 0; } if( RegisterClassEx( &wcl ) ) { HWND hWnd = CreateWindowEx( WS_EX_LAYERED | WS_EX_TOOLWINDOW, wcl.lpszClassName, TEXT( "Layered Window Demo" ), WS_POPUP, 0, 0, icon.bild->getBreite(), icon.bild->getHeight(), 0, 0, wcl.hInstance, 0 ); if( hWnd ) { BOOL bRet = 0; MSG msg = { 0 }; InitLayeredWindow( hWnd ); RECT r; GetWindowRect( GetDesktopWindow(), &r ); r.top = ( r.bottom / 2 ) - ( icon.bild->getHeight() / 2 ); r.left = ( r.right / 2 ) - ( icon.bild->getBreite() / 2 ); SetWindowPos( hWnd, 0, r.left, r.top, icon.bild->getBreite(), icon.bild->getHeight(), 0 ); ShowWindow( hWnd, nShowCmd ); UpdateWindow( hWnd ); Sleep( 5000 ); DestroyWindow( hWnd ); } UnregisterClass( wcl.lpszClassName, hInstance ); } ImageDestroy( &icon ); return 0; }