home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / pcmagazi / 1991 / 06 / icondraw.c < prev    next >
C/C++ Source or Header  |  1991-01-08  |  10KB  |  268 lines

  1. /*===========================================================================*/
  2. /*                                                                           */
  3. /* File    : ICODRAW.C                                                       */
  4. /*                                                                           */
  5. /* Purpose : Drawing routines for the IKE icon editor.                       */
  6. /*                                                                           */
  7. /* History :                                                                 */
  8. /*                                                                           */
  9. /* (C) Copyright 1991 Marc Adler/Magma Systems     All Rights Reserved       */
  10. /*===========================================================================*/
  11.  
  12. #include <memory.h>
  13. #include <string.h>
  14. #include <windows.h>
  15. #include "ico.h"
  16.  
  17. extern BYTE rgb[16][3];
  18.  
  19. HANDLE PASCAL AllocateDefaultBitmap(void);
  20. int  FAR PASCAL DrawEllipse(HDC hMemDC);
  21. int  FAR PASCAL DoFloodFill(HDC hMemDC);
  22. VOID PASCAL DrawOnBitmap(HDC hDC, HANDLE hBits, HPEN hPen, HBRUSH hBrush, FARPROC lpfnDraw);
  23.  
  24.  
  25. /****************************************************************************/
  26. /*                                                                          */
  27. /* Function : DisplayDIBitmap()                                             */
  28. /*                                                                          */
  29. /* Purpose  : Draws the bitmap which represents the icon                    */
  30. /*                                                                          */
  31. /* Returns  : Nothing.                                                      */
  32. /*                                                                          */
  33. /****************************************************************************/
  34. VOID PASCAL DisplayDIBitmap(HWND hWnd, HDC hDC, HANDLE hBits)
  35. {
  36.   HANDLE       hbmi;
  37.   LPBITMAPINFO lpbmi;
  38.   RECT         rClient;
  39.   LPSTR        lpBits;
  40.  
  41.   if ((lpBits = GlobalLock(hBits)) == NULL)
  42.   {
  43.     MessageBox(hWndMain, "Could not lock lpBits", "DisplayDIB", MB_OK);
  44.     return;
  45.   }
  46.  
  47.   hbmi = AllocateDefaultBitmap();
  48.   lpbmi = (LPBITMAPINFO) GlobalLock(hbmi);
  49.  
  50.   /*
  51.     Draw the bitmap on the screen
  52.   */
  53.   GetClientRect(hWnd, &rClient);
  54.   StretchDIBits(hDC, 0, 0, rClient.right+1, rClient.bottom+1,
  55.                 0, 0, 
  56.                 (WORD)lpbmi->bmiHeader.biWidth, (WORD)lpbmi->bmiHeader.biHeight,
  57.                 lpBits, (LPBITMAPINFO) lpbmi, DIB_RGB_COLORS, SRCCOPY);
  58.  
  59.   /*
  60.     Unlock all of the various memory handles
  61.   */
  62.   GlobalUnlock(hBits);
  63.   GlobalUnlock(hbmi);
  64.   GlobalFree(hbmi);
  65. }
  66.  
  67.  
  68. /****************************************************************************/
  69. /*                                                                          */
  70. /* Function : DrawCircleInBitmap()                                          */
  71. /*                                                                          */
  72. /* Purpose  : Draws a circle within the current pixmap.                     */
  73. /*                                                                          */
  74. /* Returns  : Nothing.                                                      */
  75. /*                                                                          */
  76. /****************************************************************************/
  77. static RECT rEllipse;
  78.  
  79. VOID PASCAL
  80. DrawCircleInBitmap(HWND hWnd,HDC hDC,HANDLE hBits,int x1,int y1,int x2,int y2)
  81. {
  82.   HPEN        hPen;
  83.   HBRUSH      hBrush;
  84.  
  85.   hPen = CreatePen(PS_SOLID, 1, 
  86.      RGB(rgb[CurrentColor][2], rgb[CurrentColor][1], rgb[CurrentColor][0]));
  87.  
  88.   hBrush = (iCurrentTool == ID_FILLCIRC) ? 
  89.    CreateSolidBrush(
  90.      RGB(rgb[CurrentColor][2], rgb[CurrentColor][1], rgb[CurrentColor][0])) :
  91.        GetStockObject(NULL_BRUSH);
  92.  
  93.   SetRect((LPRECT) &rEllipse, x1, y1, x2, y2);
  94.  
  95.   DrawOnBitmap(hDC, hBits, hPen, hBrush, DrawEllipse);
  96. }
  97.  
  98. int FAR PASCAL DrawEllipse(HDC hMemDC)
  99. {
  100.   return
  101.   Ellipse(hMemDC, rEllipse.left, rEllipse.top, rEllipse.right, rEllipse.bottom);
  102. }
  103.  
  104.  
  105. /****************************************************************************/
  106. /*                                                                          */
  107. /* Function : FloodFillBitmap()                                             */
  108. /*                                                                          */
  109. /* Purpose  : Does a flood-fill within the current bitmap. Uses BLACK as    */
  110. /*            the border color for the fill.                                */
  111. /*                                                                          */
  112. /* Returns  : Nothing.                                                      */
  113. /*                                                                          */
  114. /****************************************************************************/
  115. static POINT ptFlood;
  116.  
  117. VOID PASCAL
  118. FloodFillBitmap(HWND hWnd,HDC hDC,HANDLE hBits,int x1,int y1)
  119. {
  120.   HBRUSH      hBrush;
  121.  
  122.   hBrush = CreateSolidBrush(
  123.     RGB(rgb[CurrentColor][2], rgb[CurrentColor][1], rgb[CurrentColor][0]));
  124.  
  125.   ptFlood.x = x1;
  126.   ptFlood.y = y1;
  127.   DrawOnBitmap(hDC, hBits, (HPEN) 0, hBrush, DoFloodFill);
  128. }
  129.  
  130. int FAR PASCAL DoFloodFill(HDC hMemDC)
  131. {
  132.   return
  133.   FloodFill(hMemDC, ptFlood.x, ptFlood.y, RGB(0, 0, 0));
  134. }
  135.  
  136.  
  137.  
  138. /****************************************************************************/
  139. /*                                                                          */
  140. /* Function : AllocateDefaultBitmap()                                       */
  141. /*                                                                          */
  142. /* Purpose  : Creates an empty device independent bitmap.                   */
  143. /*                                                                          */
  144. /* Returns  : A handle to the bitmap.                                       */
  145. /*                                                                          */
  146. /****************************************************************************/
  147. HANDLE PASCAL AllocateDefaultBitmap(void)
  148. {
  149.   HANDLE       hbmi;
  150.   LPBITMAPINFO lpbmi;
  151.  
  152.   hbmi = GlobalAlloc(GMEM_FIXED | GMEM_ZEROINIT, 
  153.        (DWORD) sizeof(BITMAPINFOHEADER) + IconDir.ColorCount*sizeof(RGBQUAD));
  154.   lpbmi = (LPBITMAPINFO) GlobalLock(hbmi);
  155.  
  156.   lpbmi->bmiHeader.biSize          = (long) sizeof(BITMAPINFOHEADER);
  157.   lpbmi->bmiHeader.biWidth         = IconDir.Width;
  158.   lpbmi->bmiHeader.biHeight        = IconDir.Height;
  159.   lpbmi->bmiHeader.biPlanes        = 1;
  160.   lpbmi->bmiHeader.biBitCount      = ColorCountToPlanes(IconDir.ColorCount);
  161.   lpbmi->bmiHeader.biCompression   = BI_RGB;
  162.   lpbmi->bmiHeader.biSizeImage     = 512L;
  163.   lpbmi->bmiHeader.biXPelsPerMeter = 0;
  164.   lpbmi->bmiHeader.biYPelsPerMeter = 0;
  165.   lpbmi->bmiHeader.biClrUsed       = 0;
  166.   lpbmi->bmiHeader.biClrImportant  = 0;
  167.  
  168.   /*
  169.     Initialize the colors
  170.   */
  171.   if (IconDir.ColorCount == 2)
  172.   {
  173.     _fmemcpy(&lpbmi->bmiColors[0], rgb[0], 3);
  174.     _fmemcpy(&lpbmi->bmiColors[1], rgb[7], 3);
  175.   }
  176.   else
  177.   {
  178.     int i;
  179.     for (i = 0;  i < 16;  i++)
  180.       _fmemcpy(&lpbmi->bmiColors[i], rgb[i], 3);
  181.   }
  182.  
  183.   GlobalUnlock(hbmi);
  184.   return hbmi;
  185. }
  186.  
  187.  
  188. /****************************************************************************/
  189. /*                                                                          */
  190. /* Function : DrawOnBitmap()                                                */
  191. /*                                                                          */
  192. /* Purpose  : Takes a bitmap, a pen, a brush, and a callback function, and  */
  193. /*            does most of the grunt work for drawing a graphical object    */
  194. /*            on a bitmap.                                                  */
  195. /*                                                                          */
  196. /* Returns  : Nothing.                                                      */
  197. /*                                                                          */
  198. /****************************************************************************/
  199. VOID PASCAL DrawOnBitmap(HDC hDC, HANDLE hBits, HPEN hPen, HBRUSH hBrush, FARPROC lpfnDraw)
  200. {
  201.   HANDLE      hbmi;
  202.   LPBITMAPINFO lpbmi;
  203.   HBITMAP     hOldBitmap, hBitmap;
  204.   HPEN        hOldPen;
  205.   HBRUSH      hOldBrush;
  206.   LPSTR       lpBits;
  207.   HDC         hMemDC;
  208.  
  209.   hMemDC = CreateCompatibleDC(hDC);
  210.  
  211.   hbmi = AllocateDefaultBitmap();
  212.   lpbmi = (LPBITMAPINFO) GlobalLock(hbmi);
  213.  
  214.   if ((lpBits = GlobalLock(hBits)) == NULL)
  215.   {
  216.     MessageBox(hWndMain, "Could not lock lpBits", "DrawOnBitmap", MB_OK);
  217.     return;
  218.   }
  219.  
  220.   /*
  221.     Create the bitmap given the above structure.
  222.   */
  223.   hBitmap = CreateDIBitmap(hDC, (LPBITMAPINFOHEADER) &lpbmi->bmiHeader,
  224.                            CBM_INIT, (LPSTR) lpBits, (LPBITMAPINFO) lpbmi,
  225.                            DIB_RGB_COLORS);
  226.   if (hBitmap == NULL)
  227.   {
  228.     MessageBox(hWndMain, "Couldn't create the Bitmap", NULL, MB_OK);
  229.     return;
  230.   }
  231.  
  232.   /*
  233.     Save the old objects
  234.   */
  235.   hOldBitmap = SelectObject(hMemDC, hBitmap);
  236.   if (hPen)
  237.     hOldPen = SelectObject(hMemDC, hPen);
  238.   if (hBrush)
  239.     hOldBrush = SelectObject(hMemDC, hBrush);
  240.  
  241.   StretchDIBits(hMemDC, 0, 0, 32, 32,
  242.                         0, 0, 32, 32,
  243.                         lpBits, lpbmi,
  244.                         DIB_RGB_COLORS, SRCCOPY);
  245.  
  246.   (*lpfnDraw)(hMemDC);
  247.  
  248.   GetDIBits(hMemDC, hBitmap, 0,32, lpBits, lpbmi, DIB_RGB_COLORS);
  249.  
  250.   SelectObject(hMemDC, hOldBitmap);
  251.   if (hPen)
  252.     SelectObject(hMemDC, hOldPen);
  253.   if (hBrush)
  254.     SelectObject(hMemDC, hOldBrush);
  255.  
  256.   GlobalUnlock(hBits);
  257.   DeleteDC(hMemDC);
  258.   DeleteObject(hBitmap);
  259.   if (hPen)
  260.     DeleteObject(hPen);
  261.   if (hBrush)
  262.     DeleteObject(hBrush);
  263.  
  264.   GlobalUnlock(hbmi);
  265.   GlobalFree(hbmi);
  266. }
  267.  
  268.