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

📄 drawobj.c

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

THIS CODE AND INFORMATION IS PROVIDED AS IS WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
PURPOSE.

Copyright(c)  1999  Microsoft Corporation.  All Rights Reserved.

MODULE: 
  DrawObj.c

ABSTRACT: 
  This is a C file of the CeGDI Windows CE application. It contains
  several functions that show the GDI features such as palette, bitmap, 
  pen, brush, and line.

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

#include "CeGDI.h"
#include <math.h>

#define NUMPT  200   // Number of points in the sine wave polyline
#define PALETTEINDEX(i) ((COLORREF) (0x01000000 | (DWORD) (WORD) (i))) 
                     // Macro that accepts an index to a palette entry
                     // and returns a palette-entry specifier consisting
                     // of a 32-bit COLORREF value that specifies the 
                     // color associated with the index
int iPalSize = 0;    // Number of entries in the logical color palette

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

FUNCTION: 
  DrawRandomObjects

PURPOSE: 
  Draws random objects in the context device.

***********************************************************************/
VOID DrawRandomObjects (HWND hwnd)
{
  HDC hDC;                  // Handle to the display device context 
  RECT rect;                // RECT structure
  POINT pt[4];              // Four dimensional POINT structure array
  HBRUSH hBrush,            // Handle to the new brush object 
         hOldBrush;         // Handle to the old brush object 
  TCHAR szDebug[80];        // Debuging message string

  int x1, y1, x2, y2, x3, y3, x4, y4, 
                            // Coordinates of four points
      iRed, iGreen, iBlue,  // Indicate the Red, Green, Blue component 
                            // color of the brush
      iObject;              // Integer that indicates the type of 
                            // objects
  // Retrieve the handle to the display device context.
  if (!(hDC = GetDC (hwnd)))
    return;

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

  // Take the command bar height in consideration.
  rect.bottom -= g_iCBHeight;

  // Generate three random numbers.
  iRed = rand() % 255;
  iGreen = rand() % 255;
  iBlue = rand() % 255;

  // Create a solid brush object and selects it into the device context.
  hBrush = CreateSolidBrush (RGB(iRed, iGreen, iBlue));

  if (hOldBrush = SelectObject (hDC, hBrush))
  {
    // Randomly generate four points.
    x1 = rand() % rect.right;
    y1 = rand() % rect.bottom + g_iCBHeight;
    x2 = rand() % rect.right;
    y2 = rand() % rect.bottom + g_iCBHeight;
    x3 = rand() % rect.right;
    y3 = rand() % rect.bottom + g_iCBHeight;
    x4 = rand() % rect.right;
    y4 = rand() % rect.bottom + g_iCBHeight;

    // Randomly generate an integer that indicates the type of objects.
    iObject = rand() % 4;

    switch (iObject)
    {
      case 0:
        wsprintf (szDebug, TEXT("Rectangle(%d ,%d, %d, %d)\n"), 
                  x1, y1, x2, y2);

        // Draw a rectangle.
        Rectangle (hDC, x1, y1, x2, y2);

        break;

      case 1:
        wsprintf (szDebug, TEXT("Ellipse(%d, %d, %d, %d)\n"), 
                  x1, y1, x2, y2);

        // Draw an ellipse.
        Ellipse (hDC, x1, y1, x2, y2);

        break;

      case 2:
        wsprintf (szDebug, TEXT("RoundRect (%d, %d, %d, %d, %d, %d)\n"),
                  x1, y1, x2, y2, x3, y3);

        // Draw a rectangle with rounded corners. 
        RoundRect (hDC, x1, y1, x2, y2, x3, y3);

        break;

      case 3:
        pt[0].x = x1;
        pt[0].y = y1;
        pt[1].x = x2;
        pt[1].y = y2;
        pt[2].x = x3;
        pt[2].y = y3;
        pt[3].x = x4;
        pt[3].y = y4;

        wsprintf (szDebug, 
                  TEXT("Chord(%d, %d, %d, %d, %d, %d, %d, %d)\n"),
                  x1, y1, x2, y2, x3, y3, x4, y4);

        // Draw a polygon.
        Polygon(hDC, pt, 4);

        break;

      default:
        break;
    }

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

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

  ReleaseDC (hwnd, hDC);
  return;
}

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

FUNCTION: 
  CreateScalePalette

PURPOSE: 
  Creates a palette that represents the scale values of an specified
  RGB color. This function can also create a gray scale palette.

***********************************************************************/
HPALETTE CreateScalePalette (HDC hDC, int iColor)
{
  HPALETTE hPalette = NULL;   // Handle of the palette to be created       
  LPLOGPALETTE lpMem = NULL;  // Buffer for the LOGPALETTE structure 
                              // which defines the palette
  int index,                  // Integer index
      iReserved,              // Number of reserved entries in the 
                              // system palette
      iRasterCaps;            // Raster capabilities of the display 
                              // device context
  // Retrieve the raster capabilities of the display device context.
  // Check if it is capble of specifing a palette-based device, then 
  // determine the number of entries in the logical color palette. 
  
  iRasterCaps = GetDeviceCaps (hDC, RASTERCAPS); 
  iRasterCaps = (iRasterCaps & RC_PALETTE) ? TRUE : FALSE;  

  if (iRasterCaps) 
  {
    iReserved = GetDeviceCaps (hDC, NUMRESERVED);
    iPalSize = GetDeviceCaps (hDC, SIZEPALETTE) - iReserved;
  }
  else 
    iPalSize = GetDeviceCaps (hDC, NUMCOLORS); 

  // If there can not be any entries in the logical color palette, exit.
  if (iPalSize <= 0)
  {
    MessageBox (g_hwndMain, 
                TEXT("Palette can not be created, there can not be ")
                TEXT("any entries in it."),
                TEXT("Info"), 
                MB_OK);
    goto exit;
  }

  // Allocate a buffer for the LOGPALETTE structure.
  if (!(lpMem = (LOGPALETTE *) LocalAlloc (LMEM_FIXED, 
                sizeof(LOGPALETTE) + sizeof(PALETTEENTRY) * iPalSize)))
    goto exit;
            
  lpMem->palNumEntries = (WORD) iPalSize;
  lpMem->palVersion = (WORD) 0x0300;

  switch(iColor)
  {
    case 0:            // Red color component only
      for (index = 0; index < iPalSize; index++)
      {
        lpMem->palPalEntry[index].peRed   = (BYTE) index;
        lpMem->palPalEntry[index].peGreen = 0;
        lpMem->palPalEntry[index].peBlue  = 0;
        lpMem->palPalEntry[index].peFlags = 0;
      }
      break;

    case 1:            // Green color component only
      for (index = 0; index < iPalSize; index++)
      {
        lpMem->palPalEntry[index].peRed   = 0;
        lpMem->palPalEntry[index].peGreen = (BYTE) index;
        lpMem->palPalEntry[index].peBlue  = 0;
        lpMem->palPalEntry[index].peFlags = 0;
      }
      break;

    case 2:            // Blue color component only
      for (index = 0; index < iPalSize; index++)
      {
        lpMem->palPalEntry[index].peRed   = 0;
        lpMem->palPalEntry[index].peGreen = 0;
        lpMem->palPalEntry[index].peBlue  = (BYTE) index;
        lpMem->palPalEntry[index].peFlags = 0;
      }
      break;

    case 3:            // Gray scale palette
    default:  
      for (index = 0; index < iPalSize; index++)
      {
        lpMem->palPalEntry[index].peRed   = (BYTE) index;
        lpMem->palPalEntry[index].peGreen = (BYTE) index;
        lpMem->palPalEntry[index].peBlue  = (BYTE) index;
        lpMem->palPalEntry[index].peFlags = 0;
      }
      break;
  }

  // Create the palette.
  hPalette = CreatePalette (lpMem);

  // Free the memory object lpMem. 
  LocalFree ((HLOCAL) lpMem);

exit:
  return hPalette;
}

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

FUNCTION: 
  DisplayPalette

PURPOSE: 
  Shows how to select a logical palette and map the palette entries from
  the logical palette to the system palette.

***********************************************************************/
VOID DisplayPalette (HWND hwnd)
{
  HDC hDC;              // Handle to a display device context  
  RECT rect,            // RECT structure for the small rectangles
       rectClient;      // RECT structure for the window's client area
  HBRUSH hBrush,        // Handle to the new brush object  
         hBrushOld;     // Handle to the old brush object  
  HPALETTE hPal,        // Handle to a palette object  
           hSysPal;     // Handle to the system palette  
  int index,            // Integer index
      iColor,           // Indicates the color of the palette
      iStep,            // Width of the small rectangles
      iWidth, iHeight;  // Width and height of window's client area
  
  // Retrieve the handle to the display device context for the client 
  // area of a window. 
  if (!(hDC = GetDC (hwnd)))

⌨️ 快捷键说明

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