main.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #include <Bild.h>
  2. #include <DateiSystem.h>
  3. #include <Text.h>
  4. #include <Fenster.h>
  5. using namespace Framework;
  6. struct Image
  7. {
  8. HDC hdc;
  9. Bild *bild;
  10. HBITMAP hBitmap;
  11. BITMAPINFO info;
  12. int pitch;
  13. unsigned char *pixels;
  14. };
  15. Image icon;
  16. bool exitB = 0;
  17. void ImageDestroy( Image *image )
  18. {
  19. if( !image )
  20. return;
  21. image->pitch = 0;
  22. if( image->hBitmap )
  23. {
  24. DeleteObject( image->hBitmap );
  25. image->hBitmap = NULL;
  26. }
  27. if( image->hdc )
  28. {
  29. DeleteDC( image->hdc );
  30. image->hdc = NULL;
  31. }
  32. memset( &image->info, 0, sizeof( image->info ) );
  33. if( image->bild )
  34. image->bild->release();
  35. }
  36. bool ImageCreate( Image *image, const char *pfad, const char *name )
  37. {
  38. if( !image )
  39. return 0;
  40. LTDBDatei *bilder = new LTDBDatei();
  41. bilder->setDatei( new Text( pfad ) );
  42. bilder->leseDaten( 0 );
  43. image->bild = bilder->laden( 0, new Text( name ) );
  44. bilder->release();
  45. if( !image->bild )
  46. return 0;
  47. image->pixels = 0;
  48. image->pitch = ( ( image->bild->getBreite() * 32 + 31 ) & ~31 ) >> 3;
  49. image->hdc = CreateCompatibleDC( 0 );
  50. if( !image->hdc )
  51. return 0;
  52. memset( &image->info, 0, sizeof( image->info ) );
  53. image->info.bmiHeader.biSize = sizeof( image->info.bmiHeader );
  54. image->info.bmiHeader.biBitCount = 32;
  55. image->info.bmiHeader.biWidth = image->bild->getBreite();
  56. image->info.bmiHeader.biHeight = -image->bild->getHeight();
  57. image->info.bmiHeader.biCompression = BI_RGB;
  58. image->info.bmiHeader.biPlanes = 1;
  59. image->hBitmap = CreateDIBSection( image->hdc, &image->info, DIB_RGB_COLORS, (void**)&image->pixels, NULL, 0 );
  60. if( !image->hBitmap )
  61. {
  62. ImageDestroy( image );
  63. return 0;
  64. }
  65. GdiFlush();
  66. return 1;
  67. }
  68. void ImagePreMultAlpha( Image *image )
  69. {
  70. unsigned char *pPixel = 0;
  71. if( image->bild->getBreite() * 4 == image->pitch )
  72. {
  73. int i = 0;
  74. int totalBytes = image->bild->getBreite() * image->bild->getHeight() * 4;
  75. for( i = 0; i < totalBytes; i += 4 )
  76. {
  77. pPixel = &image->pixels[ i ];
  78. pPixel[ 0 ] = (unsigned char)( pPixel[ 0 ] * ( (float)pPixel[ 3 ] / 255.0f ) );
  79. pPixel[ 1 ] = (unsigned char)( pPixel[ 1 ] * ( (float)pPixel[ 3 ] / 255.0f ) );
  80. pPixel[ 2 ] = (unsigned char)( pPixel[ 2 ] * ( (float)pPixel[ 3 ] / 255.0f ) );
  81. }
  82. }
  83. else
  84. {
  85. int x = 0;
  86. int y = 0;
  87. for( y = 0; y < image->bild->getHeight(); ++y )
  88. {
  89. for( x = 0; x < image->bild->getBreite(); ++x )
  90. {
  91. pPixel = &image->pixels[ ( y * image->pitch ) + ( x * 4 ) ];
  92. pPixel[ 0 ] = (unsigned char)( pPixel[ 0 ] * ( (float)pPixel[ 3 ] / 255.0f ) );
  93. pPixel[ 1 ] = (unsigned char)( pPixel[ 1 ] * ( (float)pPixel[ 3 ] / 255.0f ) );
  94. pPixel[ 2 ] = (unsigned char)( pPixel[ 2 ] * ( (float)pPixel[ 3 ] / 255.0f ) );
  95. }
  96. }
  97. }
  98. }
  99. bool ImageImport( Image *image )
  100. {
  101. BYTE *pRow = NULL;
  102. if( !image )
  103. return 0;
  104. if( !ImageCreate( image, "data/bilder/game.ltdb", "starticon.png" ) )
  105. return 0;
  106. int *buffer = image->bild->getBuffer();
  107. for( int i = 0; i < image->bild->getHeight(); i++ )
  108. {
  109. pRow = &image->pixels[ i * image->pitch ];
  110. for( int i2 = 0; i2 < image->bild->getBreite(); i2++ )
  111. {
  112. pRow[ i2 * 4 ] = (unsigned char)( ( buffer[ i2 + i * image->bild->getBreite() ] ) & 0xFF );
  113. pRow[ i2 * 4 + 1 ] = (unsigned char)( ( buffer[ i2 + i * image->bild->getBreite() ] >> 8 ) & 0xFF );
  114. pRow[ i2 * 4 + 2 ] = (unsigned char)( ( buffer[ i2 + i * image->bild->getBreite() ] >> 16 ) & 0xFF );
  115. pRow[ i2 * 4 + 3 ] = (unsigned char)( ( buffer[ i2 + i * image->bild->getBreite() ] >> 24 ) & 0xFF );
  116. }
  117. }
  118. ImagePreMultAlpha( image );
  119. return 1;
  120. }
  121. void InitLayeredWindow( HWND hWnd )
  122. {
  123. HDC hdc = NULL;
  124. if( hdc = GetDC( hWnd ) )
  125. {
  126. HGDIOBJ hPrevObj = NULL;
  127. POINT ptDest = { 0, 0 };
  128. POINT ptSrc = { 0, 0 };
  129. SIZE client = { icon.bild->getBreite(), icon.bild->getHeight() };
  130. BLENDFUNCTION blendFunc = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
  131. hPrevObj = SelectObject( icon.hdc, icon.hBitmap );
  132. ClientToScreen( hWnd, &ptDest );
  133. UpdateLayeredWindow( hWnd, hdc, &ptDest, &client, icon.hdc, &ptSrc, 0, &blendFunc, ULW_ALPHA );
  134. SelectObject( icon.hdc, hPrevObj );
  135. ReleaseDC( hWnd, hdc );
  136. }
  137. }
  138. LRESULT CALLBACK Proc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
  139. {
  140. return DefWindowProc( hWnd, msg, wParam, lParam );
  141. }
  142. int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
  143. {
  144. WNDCLASSEX wcl = { 0 };
  145. wcl.cbSize = sizeof( wcl );
  146. wcl.style = CS_HREDRAW | CS_VREDRAW;
  147. wcl.lpfnWndProc = Proc;
  148. wcl.cbClsExtra = 0;
  149. wcl.cbWndExtra = 0;
  150. wcl.hInstance = hInstance;
  151. wcl.hIcon = 0;
  152. wcl.hCursor = 0;
  153. wcl.hbrBackground = 0;
  154. wcl.lpszMenuName = 0;
  155. wcl.lpszClassName = TEXT( "LayeredWindowClass" );
  156. wcl.hIconSm = 0;
  157. if( !ImageImport( &icon ) )
  158. {
  159. WMessageBox( 0, new Text( "Fehler" ), new Text( "data/bilder/game.ltdb/starticon.png konnte nicht geladen werden!" ), MB_ICONERROR );
  160. return 0;
  161. }
  162. if( RegisterClassEx( &wcl ) )
  163. {
  164. 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 );
  165. if( hWnd )
  166. {
  167. BOOL bRet = 0;
  168. MSG msg = { 0 };
  169. InitLayeredWindow( hWnd );
  170. RECT r;
  171. GetWindowRect( GetDesktopWindow(), &r );
  172. r.top = ( r.bottom / 2 ) - ( icon.bild->getHeight() / 2 );
  173. r.left = ( r.right / 2 ) - ( icon.bild->getBreite() / 2 );
  174. SetWindowPos( hWnd, 0, r.left, r.top, icon.bild->getBreite(), icon.bild->getHeight(), 0 );
  175. ShowWindow( hWnd, nShowCmd );
  176. UpdateWindow( hWnd );
  177. Sleep( 5000 );
  178. DestroyWindow( hWnd );
  179. }
  180. UnregisterClass( wcl.lpszClassName, hInstance );
  181. }
  182. ImageDestroy( &icon );
  183. return 0;
  184. }