荔园在线
荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀
[回到开始]
[上一篇][下一篇]
发信人: Peter (小飞侠), 信区: Program
标 题: 一个调色板动画程序
发信站: BBS 荔园晨风站 (Fri Jan 22 13:45:33 1999), 转信
// Copyright (c) 1990, 1991 Microsoft Corporation. All rights reserved.
#include "windows.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define PALETTESIZE 256 /* Number of entries in the system palette
*/
HWND hWnd;
HDC hDCGlobal;
HANDLE hPal, hOldPallete; /* Handle to the application's logical
palette */
NPLOGPALETTE pLogPal; /* Pointer to program's logical palette
*/
int iNumColors; /* Number of colors supported by device
*/
int iPalSize,iRasterCaps; /* Size of Physical palette
*/
PALETTEENTRY palentries[210]; /* The animation pallete
*/
BOOL CreateColors(HWND);
long FAR PASCAL SimpleWinProc(HWND, unsigned, WORD, LONG);
/******************************************************************
* *
* WinMain *
* *
******************************************************************/
int PASCAL WinMain(hInstance, hPrevInstance, lpszCmdLine, cmdShow)
HANDLE hInstance, hPrevInstance;
LPSTR lpszCmdLine;
int cmdShow;
{
MSG msg;
static WNDCLASS wndclass;
if (!hPrevInstance)
{ /* Main Window */
wndclass.lpszClassName = (LPSTR)"ColorWheel";
wndclass.hInstance = hInstance;
wndclass.lpfnWndProc = SimpleWinProc;
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hIcon = NULL;
wndclass.lpszMenuName = NULL;
wndclass.hbrBackground = GetStockObject(GRAY_BRUSH);
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
if (!RegisterClass((LPWNDCLASS)&wndclass))
return FALSE;
}
else
{
/* return FALSE; */
}
hWnd = CreateWindow((LPSTR) "ColorWheel", /* Window class name */
(LPSTR) "Animated Color Wheel", /* Window title */
WS_OVERLAPPED | WS_MAXIMIZEBOX | WS_MINIMIZEBOX |
WS_THICKFRAME | WS_SYSMENU | WS_MAXIMIZE,
CW_USEDEFAULT, /* x */
0, /* y */
CW_USEDEFAULT, /* cx */
0, /* cy */
(HWND)NULL, /* No parent */
(HMENU)NULL, /* Use the class menu. */
(HANDLE)hInstance, /* task queue for window */
(LPSTR)NULL /* No params */
);
ShowWindow(hWnd, cmdShow); /* Get windows to make room for this
window */
UpdateWindow(hWnd); /* Paint the client area. */
while (GetMessage((LPMSG)&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
exit (msg.wParam);
return(msg.wParam);
}
/******************************************************************
* *
* SIMPLEWINPROC: Receives Main Window Message *
* *
******************************************************************/
long FAR PASCAL SimpleWinProc(hWnd, identifier, wParam, lParam)
HWND hWnd; /* Intended window */
unsigned identifier; /* Message Number */
WORD wParam; /* 16 bit param */
LONG lParam; /* 32 bit param */
{
PAINTSTRUCT ps;
HDC hDC;
HPEN hOldPen, hPen;
HANDLE hOldPal;
int i;
int cx, cy;
int sx, sy;
int radius;
float x, y;
float degrees, radians;
RECT rRect;
int PenWidth;
PALETTEENTRY palentry;
static BOOL b256Colors;
switch (identifier)
{
case WM_CREATE:
b256Colors = CreateColors(hWnd);
SetTimer(hWnd, 1, 10, NULL);
break;
case WM_TIMER:
if (!b256Colors)
break;
// Step 1: Rotate the colors in the array of palette entries
// by brute force (for the example)
palentry.peRed = palentries[209].peRed;
palentry.peGreen = palentries[209].peGreen;
palentry.peBlue = palentries[209].peBlue;
for (i = 209; i > 0; i--)
{
palentries[i].peRed = palentries[i-1].peRed;
palentries[i].peGreen = palentries[i-1].peGreen;
palentries[i].peBlue = palentries[i-1].peBlue;
}
palentries[0].peRed = palentry.peRed;
palentries[0].peGreen = palentry.peGreen;
palentries[0].peBlue = palentry.peBlue;
// Step 2: Bring in the Palette
hDC = GetDC(hWnd);
hOldPal = SelectPalette(hDC, hPal, FALSE);
// Call Animate Palette, and realize the colors
AnimatePalette(hPal, 20, 210, (LPPALETTEENTRY)palentries);
RealizePalette(hDC);
// Housekeep
SelectPalette(hDC, hOldPal, TRUE);
ReleaseDC(hWnd, hDC);
break;
case WM_PAINT:
if (!b256Colors)
return (DefWindowProc(hWnd, identifier, wParam, lParam));
// Figure centerpoint and radius
GetClientRect(hWnd, (LPRECT)&rRect);
cx = rRect.right / 2;
cy = rRect.bottom / 2;
radius = min(cx, cy) - 10;
PenWidth = radius / 20;
hDC = BeginPaint(hWnd, (LPPAINTSTRUCT)&ps);
// Bring in the pallete
hOldPal = SelectPalette(hDC, hPal, FALSE);
// Synchronize the system pallete and app's logical pallete
RealizePalette(hDC);
// Save the default pen
hOldPen = SelectObject(hDC, GetStockObject(NULL_PEN));
for (i = 0; i < 210; i++)
{
// Step 1: turn 0-210 into 0-360
degrees = ((float)i) * (float)(360.0 / 210.0);
// Step 2: turn degrees into radians
radians = degrees * (float)0.017453292;
// Step 3: Figure outer edge of line in floating pt coords
x = (float)cos(radians);
y = (float)sin(radians);
// Step 4: Convert these into screen coords
sx = cx + (int)(x * radius);
sy = cy + (int)(y * radius);
// Instead of RGB, we use PALETTEINDEX to access the palette
hPen = CreatePen(0, PenWidth, PALETTEINDEX(i + 20));
SelectObject(hDC, hPen);
MoveTo(hDC, cx, cy);
LineTo(hDC, sx, sy);
SelectObject(hDC, hOldPen);
DeleteObject(hPen);
}
// Housekeep
SelectObject(hDC, hOldPen);
SelectPalette(hDC, hOldPal, TRUE);
EndPaint(hWnd, (LPPAINTSTRUCT)&ps);
break;
case WM_DESTROY:
KillTimer(hWnd, 1);
PostQuitMessage(0);
break;
default:
return(DefWindowProc(hWnd, identifier, wParam, lParam));
break;
} /* switch identifier */
return (0L);
} /* window procedure */
/******************************************************************/
//
// Create a logical pallete, called hPal
//
/******************************************************************/
BOOL CreateColors(HWND hWnd)
{
unsigned char iLoop;
unsigned char Saturation;
int Satincrease;
// Step 1: Find out about the system
hDCGlobal = GetDC(NULL);
iPalSize = GetDeviceCaps(hDCGlobal, SIZEPALETTE);
iRasterCaps = GetDeviceCaps(hDCGlobal, RASTERCAPS);
iRasterCaps = (iRasterCaps & RC_PALETTE) ? TRUE : FALSE;
ReleaseDC(NULL, hDCGlobal);
if (iRasterCaps)
iNumColors = GetDeviceCaps(hDCGlobal, SIZEPALETTE);
else
iNumColors = GetDeviceCaps(hDCGlobal, NUMCOLORS);
if (iNumColors != 256)
{
MessageBox (NULL, "System needs 256 color pallete to show this demo",
"ColorWheel", MB_OK);
return FALSE;
}
// Step 2: Create a pallete structure in memory (all zeroes right now)
pLogPal = (NPLOGPALETTE)LocalAlloc(LMEM_FIXED,
(sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * (PALETTESIZE))));
pLogPal->palVersion = 0x300;
pLogPal->palNumEntries = PALETTESIZE;
// Step 3: Fill in the colors
//
// This app uses the palette index range from 20 through 230,
// for a total of 210 colors. For each primary color, the app will
// gradually increase from low saturation, to a high midpoint,
// then back to low saturation. Staggering these "peaks" of color
// will cause a gradual color change from one color to another.
// ------------------------ Fill in red -----------------------
Satincrease = 3;
Saturation = 40;
for (iLoop = 20; iLoop < 160; iLoop++)
{
palentries[iLoop-20].peRed = pLogPal->palPalEntry[iLoop].peRed
= Saturation;
if (90 == iLoop)
Satincrease *= -1;
Saturation += Satincrease;
}
// ---------------------- Fill in green -----------------------
Saturation = 40;
Satincrease = 3;
for (iLoop = 90; iLoop < 230; iLoop++)
{
palentries[iLoop-20].peGreen = pLogPal->palPalEntry[iLoop].peGreen
= Saturation;
if (160 == iLoop)
Satincrease *= -1;
Saturation += Satincrease;
}
// ----------------------- Fill in Blue -----------------------
Saturation = 40;
Satincrease = 3;
for (iLoop = 160; iLoop < 230; iLoop++)
{
palentries[iLoop-20].peBlue = pLogPal->palPalEntry[iLoop].peBlue
= Saturation;
Saturation += Satincrease;
}
Satincrease = -3;
for (iLoop = 20; iLoop < 90; iLoop++)
{
palentries[iLoop-20].peBlue = pLogPal->palPalEntry[iLoop].peBlue
= Saturation;
Saturation += Satincrease;
}
// -------- Flag all colors to have no special attributes -----
for (iLoop = 20; iLoop < 230; iLoop++)
palentries[iLoop-20].peFlags = pLogPal->palPalEntry[iLoop].peFlags
= PC_RESERVED;
// Now that the memory pallete structure has been filled with
// values, create a GDI object and assign a handle to it.
hPal = CreatePalette((LPLOGPALETTE)pLogPal);
}
}
--
※ 来源:.BBS 荔园晨风站 bbs.szu.edu.cn.[FROM: 192.168.1.3]
[回到开始]
[上一篇][下一篇]
荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店