⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 drawobj.c

📁 Most code samples included on this CD were developed with Microsoft Visual C++ version 5.0 and the M
💻 C
📖 第 1 页 / 共 2 页
字号:
    return;

  // Retrieve the width and height of window's client area.
  GetClientRect (g_hwndMain, &rectClient);
  iWidth = rectClient.right - rectClient.left;
  iHeight = rectClient.bottom - rectClient.top;

  iStep = 2;
  
  for (iColor = 0; iColor <= 3; ++iColor)
  {
    // Create a logical palette.
    if ((hPal = CreateScalePalette (hDC, iColor)) == NULL)
      return;

    // Select the logical palette into the device context. 
    if (iColor ==0)
      // Save the system palette handle.
      hSysPal = SelectPalette (hDC, hPal, FALSE);
    else
      SelectPalette (hDC, hPal, FALSE);

    // Map the palette entries from the logical palette to the system 
    // palette.
    RealizePalette (hDC);
    
    for (index = 0; index < iWidth / iStep; index++)
    {
      hBrush = CreateSolidBrush (PALETTEINDEX (index % iPalSize));
      hBrushOld = SelectObject (hDC, hBrush);
      SetRect (&rect, index * iStep, 0, (index + 1) * iStep, iHeight);
      FillRect (hDC, &rect, hBrush);

      // Select the old brush into the device context.
      SelectObject (hDC, hBrushOld);
  
      // Delete the new brush.
      DeleteObject (hBrush);
    }
    
    // Pause for one second before displaying the next palette.
    Sleep (1000);

    DeleteObject (hPal);
  }

  // Select the system palette.
  SelectPalette (hDC, hSysPal, FALSE);
  RealizePalette (hDC);
    
  ReleaseDC (hwnd, hDC);
  return;
}

/***********************************************************************

FUNCTION: 
  BitmapDemo

PURPOSE: 
  Shows how to create a bitmap compatible with the device that is
  associated with the memory device context.

***********************************************************************/
VOID BitmapDemo (HWND hwnd)
{
  
  HDC hDC,                  // Handle to the display device context 
      hDCMem;               // Handle to the memory device context
  HBITMAP hBitmap,          // Handle to the new bitmap
          hOldBitmap;       // Handle to the old bitmap
  static int iCoordinate[200][4];  
  int i, j,                
      iXSrc, iYSrc,         // X-coordinate and y-coordinate of the  
                            // source rectangle's upper-left corner
      iXDest, iYDest,       // X-coordinate and y-coordinate of the  
                            // destination rectangle's upper-left corner
      iWidth, iHeight;      // Width and height of the bitmap

  // Retrieve the handle to the display device context for the client 
  // area of a window.
  if (!(hDC = GetDC (hwnd)))
    return;

  // Create a memory device context that is compatible with the device.
  hDCMem = CreateCompatibleDC (hDC);

  // Retrieve the width and height of the bitmap.
  iWidth = GetSystemMetrics (SM_CXSCREEN) / 10;
  iHeight = GetSystemMetrics (SM_CYSCREEN) / 10;

  // Create a bitmap that is compatible with the device associated with 
  // the memory device context.
  hBitmap = CreateCompatibleBitmap (hDC, iWidth, iHeight);

  // Select the bitmap object into the memory device context. 
  hOldBitmap = SelectObject (hDCMem, hBitmap);

  for (i = 0; i < 2; i++)
  {
    for (j = 0; j < 200; j++)
    {
      if (i == 0)
      {
        iCoordinate[j][0] = iXDest = iWidth * (rand () % 10);
        iCoordinate[j][1] = iYDest = iHeight * (rand () % 10);
        iCoordinate[j][2] = iXSrc = iWidth * (rand () % 10);
        iCoordinate[j][3] = iYSrc = iHeight * (rand () % 10);
      }
      else
      {
        iXDest = iCoordinate[200 - 1 - j][0];
        iYDest = iCoordinate[200 - 1 - j][1];
        iXSrc = iCoordinate[200 - 1 - j][2];
        iYSrc = iCoordinate[200 - 1 - j][3];
      }

      // Transfer pixels from the source rectangle to the destination
      // rectangle.
      BitBlt (hDCMem, 0, 0, iWidth, iHeight, hDC,  iXDest, iYDest, 
              SRCCOPY);
      BitBlt (hDC,  iXDest, iYDest, iWidth, iHeight, hDC,  iXSrc, iYSrc,
              SRCCOPY);
    }
  }

  // Select the old brush into the device context.
  SelectObject (hDC, hOldBitmap);

  // Delete the bitmap object. 
  DeleteObject (hBitmap);

  // Delete the memory device context and the display device context.
  DeleteDC (hDCMem);
  DeleteDC (hDC);

  return;  
}


/***********************************************************************

FUNCTION: 
  DisplayLine

PURPOSE: 
  Creates four regions, and paints them with different color and texture
  brushes. Draws two lines in the device content. One is a straight line
  that serves as the axis and the other is a sine wave.

***********************************************************************/
VOID DisplayLine (HWND hwnd)
{
  HDC hDC;            // Handle to the display device context 
  HRGN hRgn;          // Handle to a region object  
  HPEN hPen,          // Handle to the new pen object  
       hOldPen;       // Handle to the old pen object  
  HBRUSH hBrush;      // Handle to a brush object 
  RECT rect;          // RECT structure that contains the window抯  
                      // client area coordinates
  int index;          // Integer index
  POINT ptAxis[2],    // Two dimensional POINT structure array
        ptSine[NUMPT];// 200 dimensional POINT structure array

  // Retrieve the handle to the display device context for the client 
  // area of a window.
  if (!(hDC = GetDC (hwnd)))
    return;

  // Retrieve the coordinates of the window抯 client area. 
  GetClientRect (hwnd, &rect);

  // Create a rectangular region.
  hRgn = CreateRectRgn (0, 0, rect.right / 4, rect.bottom);

  // Create a solid brush.
  hBrush = CreateSolidBrush (g_crColor[0]); 

  // Fill the region by using the created brush.
  FillRgn (hDC, hRgn, hBrush);
  
  // Delete the rectangular region. 
  DeleteObject (hRgn);

  // Delete the brush object.
  DeleteObject (hBrush);

  // Create a rectangular region.
  hRgn = CreateRectRgn (rect.right / 4, 0, rect.right / 2, 
                        rect.bottom);
  
  // Create a solid brush.
  hBrush = CreateSolidBrush (g_crColor[1]); 
  
  // Fill the region by using the created brush.
  FillRgn (hDC, hRgn, hBrush);

  // Delete the rectangular region. 
  DeleteObject (hRgn);

  // Delete the brush object.
  DeleteObject (hBrush);

  // Create a rectangular region.
  hRgn = CreateRectRgn (rect.right / 2, 0, rect.right * 3 / 4, 
                        rect.bottom);
  
  // Create a solid brush.
  hBrush = CreatePatternBrush (LoadBitmap (
                                        g_hInst, 
                                        MAKEINTRESOURCE (IDB_BITMAP)));
  
  // Fill the region by using the created brush.
  FillRgn (hDC, hRgn, hBrush);
  
  // Delete the rectangular region. 
  DeleteObject (hRgn);

  // Delete the brush object.
  DeleteObject (hBrush);

  // Create a rectangular region.
  hRgn = CreateRectRgn (rect.right * 3 / 4, 0, rect.right, rect.bottom);
  
  // Create a solid brush.
  hBrush = CreateSolidBrush (g_crColor[7]); 
  
  // Fill the region by using the created brush.
  FillRgn (hDC, hRgn, hBrush);

  // Delete the rectangular region. 
  DeleteObject (hRgn);

  // Delete the brush object.
  DeleteObject (hBrush);
 
  // Assign the axis points coordinates in pixels.
  ptAxis[0].x = 0;
  ptAxis[0].y = g_iCBHeight + (rect.bottom - g_iCBHeight) / 2;
  ptAxis[1].x = rect.right - 1;
  ptAxis[1].y = ptAxis[0].y;

  // Assign the sine wave points coordinates in pixels.
  for (index = 0; index < NUMPT; ++index)
  {
    ptSine[index].x = index * rect.right / NUMPT;
    ptSine[index].y = (long) (g_iCBHeight + \
                              (rect.bottom - g_iCBHeight) / 2 * \
                              (1 - sin (8 * 3.14159 * index / NUMPT)));
  }
  
  // Create a dash pen object and select it.
  hPen = CreatePen (PS_DASH, 1, g_crColor[5]);
  hOldPen = SelectObject (hDC, hPen);

  // Draw a straight line that connects the two points.
  Polyline (hDC, ptAxis, 2);

  // Select the old brush into the device context.
  SelectObject (hDC, hOldPen);

  // Delete the pen object.
  DeleteObject (hPen);

  // Create a solid pen object and select it.
  hPen = CreatePen (PS_SOLID, 3, g_crColor[5]);
  hOldPen = SelectObject (hDC, hPen);

  // Draw a sine wave shaped polyline.
  Polyline (hDC, ptSine, NUMPT);

  // Select the old brush into the device context.
  SelectObject (hDC, hOldPen);

  // Delete the pen object.
  DeleteObject (hPen);

  // Release the device context.
  ReleaseDC (hwnd, hDC);

  return;
}
// END OF DRAWOBJ.C

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -