Emboss.txt
Upload User: zhanwei8
Upload Date: 2007-01-03
Package Size: 2k
Code Size: 5k
Category:

OpenGL program

Development Platform:

Visual C++

  1. //prototype for default arguments - include this in your header file
  2. HBITMAP Emboss( HBITMAP hBitmap, HBITMAP hbmBackGnd, HPALETTE hPal, BOOL bRaised = TRUE,
  3.    int xDest = 0, int yDest = 0, 
  4.    COLORREF clrHighlight = GetSysColor( COLOR_BTNHIGHLIGHT ), 
  5.    COLORREF clrShadow = GetSysColor( COLOR_BTNSHADOW ));
  6. ///////////////////////////////////////////////////////////////////////////////////
  7. // Emboss - Creates a 3D embossed effect
  8. // Returns - A new bitmap containing the resulting effect
  9. // hBitmap - Bitmap that contains the basic text & shapes
  10. // hbmBackGnd - Contains the color image 
  11. // hPal - Handle of palette associated with hbmBackGnd
  12. // bRaised - True if raised effect is desired. False for sunken effect
  13. // xDest - x coordinate - used to offset hBitmap
  14. // yDest - y coordinate - used to offset hBitmap
  15. // clrHightlight - Color used for the highlight edge
  16. // clrShadow - Color used for the shadow//
  17. // Note - 1. Neither of the bitmap handles passed in should be selected 
  18. //   in a device context.
  19. //   2. The pixel at 0,0 in hBitmap is considered the background color//
  20. HBITMAP Emboss( HBITMAP hBitmap, HBITMAP hbmBackGnd, HPALETTE hPal, 
  21.    BOOL bRaised, int xDest, int yDest, 
  22.    COLORREF clrHighlight, COLORREF clrShadow ){
  23. const DWORD PSDPxax = 0x00B8074A; BITMAP   bmInfo ;
  24. HBITMAP  hbmOld, hbmShadow, hbmHighlight, hbmResult, hbmOldMem ;
  25. HBRUSH   hbrPat ; HDC      hDC, hColorDC, hMonoDC, hMemDC ; if( !bRaised ) {
  26. // Swap the highlight and shadow color COLORREF clrTemp = clrShadow;
  27. clrShadow = clrHighlight; clrHighlight = clrTemp; }
  28. // We create two monochrome bitmaps. One of them will contain the
  29. // highlighted edge and the other will contain the shadow. These
  30. // bitmaps are then used to paint the highlight and shadow on the
  31. // background image. hbmResult = NULL ; hDC = GetDC( NULL ) ;
  32. // Create a compatible DCs hMemDC = ::CreateCompatibleDC( hDC );
  33. hMonoDC = CreateCompatibleDC( hDC ); hColorDC = CreateCompatibleDC( hDC );
  34. if( hMemDC == NULL || hMonoDC == NULL || hColorDC == NULL ) {
  35. if( hMemDC ) DeleteDC( hMemDC ); if( hMonoDC ) DeleteDC( hMonoDC );
  36. if( hColorDC ) DeleteDC( hColorDC ); return NULL; }
  37. // Select the background image into memory DC so that we can draw it
  38. hbmOldMem = (HBITMAP)::SelectObject( hMemDC, hbmBackGnd );
  39. // Get dimensions of the background image BITMAP bm;
  40. ::GetObject( hbmBackGnd, sizeof( bm ), &bm );
  41. // Create the monochrome and compatible color bitmaps 
  42. GetObject( hBitmap, sizeof( BITMAP ), (LPSTR) &bmInfo ) ; hbmShadow =
  43. CreateBitmap( bmInfo.bmWidth, bmInfo.bmHeight, 1, 1, NULL ) ; hbmHighlight =
  44. CreateBitmap( bmInfo.bmWidth, bmInfo.bmHeight, 1, 1, NULL ) ; hbmResult =
  45. CreateCompatibleBitmap( hDC, bm.bmWidth, bm.bmHeight ) ;
  46. hbmOld = (HBITMAP)SelectObject( hColorDC, hBitmap ) ;
  47. // Set background color of bitmap for mono conversion
  48. // We assume that the pixel in the top left corner has the background color
  49. SetBkColor( hColorDC, GetPixel( hColorDC, 0, 0 ) ) ;
  50. // Create the highlight bitmap.
  51. hbmHighlight = (HBITMAP)SelectObject( hMonoDC, (HGDIOBJ) hbmHighlight ) ;
  52. PatBlt( hMonoDC, 0, 0, bmInfo.bmWidth, bmInfo.bmHeight, WHITENESS ) ;
  53. BitBlt( hMonoDC, 0, 0, bmInfo.bmWidth - 1, bmInfo.bmHeight - 1,
  54. hColorDC, 1, 1, SRCCOPY ) ;
  55. BitBlt( hMonoDC, 0, 0, bmInfo.bmWidth, bmInfo.bmHeight,
  56. hColorDC, 0, 0, MERGEPAINT ) ;
  57. hbmHighlight = (HBITMAP)SelectObject( hMonoDC, (HGDIOBJ) hbmHighlight ) ;
  58. // create the shadow bitmap
  59. hbmShadow = (HBITMAP)SelectObject( hMonoDC, (HGDIOBJ) hbmShadow ) ;
  60. PatBlt( hMonoDC, 0, 0, bmInfo.bmWidth, bmInfo.bmHeight, WHITENESS ) ;
  61. BitBlt( hMonoDC, 1, 1, bmInfo.bmWidth-1, bmInfo.bmHeight-1,
  62. hColorDC, 0, 0, SRCCOPY ) ;
  63. BitBlt( hMonoDC, 0, 0, bmInfo.bmWidth, bmInfo.bmHeight,
  64. hColorDC, 0, 0, MERGEPAINT ) ;
  65. hbmShadow = (HBITMAP)SelectObject( hMonoDC, (HGDIOBJ) hbmShadow ) ;
  66. // Now let's start working on the final image
  67. SelectObject( hColorDC, hbmResult ) ;
  68. // Select and realize the palette if one is supplied
  69. if( hPal && GetDeviceCaps(hDC, RASTERCAPS) & RC_PALETTE ) {
  70. ::SelectPalette( hColorDC, hPal, FALSE ); ::RealizePalette(hColorDC); }
  71. // Draw the background image
  72. BitBlt(hColorDC, 0, 0, bm.bmWidth, bm.bmHeight, hMemDC, 0, 0,SRCCOPY);
  73. // Restore the old bitmap in the hMemDC ::SelectObject( hMemDC, hbmOldMem );
  74. // Set the background and foreground color for the raster operations
  75. SetBkColor( hColorDC, RGB(255,255,255) ) ;
  76. SetTextColor( hColorDC, RGB(0,0,0) ) ; // blt the highlight edge
  77. hbrPat = CreateSolidBrush( clrHighlight ) ;
  78. hbrPat = (HBRUSH)SelectObject( hColorDC, hbrPat ) ;
  79. hbmHighlight = (HBITMAP)SelectObject( hMonoDC, (HGDIOBJ) hbmHighlight ) ;
  80. BitBlt( hColorDC, xDest, yDest, bmInfo.bmWidth, bmInfo.bmHeight,
  81. hMonoDC, 0, 0, PSDPxax ) ; DeleteObject( SelectObject( hColorDC, hbrPat ) ) ;
  82. hbmHighlight = (HBITMAP)SelectObject( hMonoDC, (HGDIOBJ) hbmHighlight ) ;
  83. // blt the shadow edge hbrPat = CreateSolidBrush( clrShadow ) ;
  84. hbrPat = (HBRUSH)SelectObject( hColorDC, hbrPat ) ;
  85. hbmShadow = (HBITMAP)SelectObject( hMonoDC, (HGDIOBJ) hbmShadow ) ;
  86. BitBlt( hColorDC, xDest, yDest, bmInfo.bmWidth, bmInfo.bmHeight,
  87. hMonoDC, 0, 0, PSDPxax ) ; DeleteObject( SelectObject( hColorDC, hbrPat ) ) ;
  88. hbmShadow = (HBITMAP)SelectObject( hMonoDC, (HGDIOBJ) hbmShadow ) ;
  89. // select old bitmap into color DC  SelectObject( hColorDC, hbmOld ) ;
  90. DeleteObject( (HGDIOBJ) hbmShadow ) ; DeleteObject( (HGDIOBJ) hbmHighlight ) ;
  91. ReleaseDC( NULL, hDC ) ; return ( hbmResult ) ;}