home *** CD-ROM | disk | FTP | other *** search
- /*
- * PALANIM.C
- *
- * (C) Copyright Microsoft Corp. 1994. All rights reserved.
- *
- * You have a royalty-free right to use, modify, reproduce and
- * distribute the Sample Files (and/or any modified version) in
- * any way you find useful, provided that you agree that
- * Microsoft has no warranty obligations or liability for any
- * Sample Application Files which are modified.
- */
-
- #include <windows.h>
- #include <commdlg.h>
- #include <wing.h>
-
- #include "dib.h"
- #include "palanim.h"
-
- /*----------------------------------------------------------------------------*\
- | |
- | g l o b a l v a r i a b l e s |
- | |
- \*----------------------------------------------------------------------------*/
- static char szAppName[]="WinG Palette Animation App";
-
- static HINSTANCE hInstApp;
- static HWND hwndApp;
- static HPALETTE hpalApp;
- static BOOL fAppActive;
-
- static HDC hdcOffscreen;
- void far *gpBits;
- struct {
- BITMAPINFOHEADER InfoHeader;
- RGBQUAD ColorTable[256];
- } gInfo;
-
- int fAnimatePalette = 0; // Don't animate
- int fIncludeStatic = 0; // Use the static color entries
- enum {Red, Green, Blue} gWashColor = Red;
-
- PALETTEENTRY aPalette[256];
-
- extern HBITMAP ghBitmapMonochrome;
-
- #ifdef WIN32
- #define _export
- #endif
-
- /*----------------------------------------------------------------------------*\
- | |
- | f u n c t i o n d e f i n i t i o n s |
- | |
- \*----------------------------------------------------------------------------*/
-
- LONG FAR PASCAL _export AppWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);
- int ErrMsg (LPSTR sz,...);
- LONG AppCommand (HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);
-
- void AppExit(void);
- BOOL AppIdle(void);
-
- /***************************************************************************
- Internal functions for the animation
- */
-
- void CreateWashPalette(void);
- void DibCreateWash(BITMAPINFOHEADER far *Info, void far *pBits);
- void DibHorizontalLine(BITMAPINFOHEADER far *Info, void far *pBits,
- int y, char unsigned color);
-
- /***************************************************************************
- Sample functions from wing.hlp
- */
-
- HDC Create100x100WinGDC(void);
- void Destroy100x100WinGDC(HDC hWinGDC);
-
- void AppActivate(BOOL fActive);
-
- void ClearSystemPalette(void);
- HPALETTE CreateIdentityPalette(RGBQUAD aRGB[], int nColors);
-
- /*----------------------------------------------------------------------------*\
- | AppAbout( hDlg, uiMessage, wParam, lParam ) |
- | |
- | Description: |
- | This function handles messages belonging to the "About" dialog box. |
- | The only message that it looks for is WM_COMMAND, indicating the use |
- | has pressed the "OK" button. When this happens, it takes down |
- | the dialog box. |
- | |
- | Arguments: |
- | hDlg window handle of about dialog window |
- | uiMessage message number |
- | wParam message-dependent |
- | lParam message-dependent |
- | |
- | Returns: |
- | TRUE if message has been processed, else FALSE |
- | |
- \*----------------------------------------------------------------------------*/
- BOOL FAR PASCAL _export AppAbout(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
- {
- switch (msg)
- {
- case WM_COMMAND:
- if (LOWORD(wParam) == IDOK)
- {
- EndDialog(hwnd,TRUE);
- }
- break;
-
- case WM_INITDIALOG:
- return TRUE;
- }
- return FALSE;
- }
-
- /*----------------------------------------------------------------------------*\
- | AppInit( hInst, hPrev) |
- | |
- | Description: |
- | This is called when the application is first loaded into |
- | memory. It performs all initialization that doesn't need to be done |
- | once per instance. |
- | |
- | Arguments: |
- | hInstance instance handle of current instance |
- | hPrev instance handle of previous instance |
- | |
- | Returns: |
- | TRUE if successful, FALSE if not |
- | |
- \*----------------------------------------------------------------------------*/
- BOOL AppInit(HINSTANCE hInst,HINSTANCE hPrev,int sw,LPSTR szCmdLine)
- {
- WNDCLASS cls;
- int dx,dy;
- HBITMAP hbmOffscreen;
- HMENU hMenu;
- HDC Screen;
-
- /* Save instance handle for DialogBoxs */
- hInstApp = hInst;
-
- /* Refuse to run if this is a non-palettized device */
- Screen = GetDC(0);
- if (Screen)
- {
- int PaletteDevice = GetDeviceCaps(Screen, RASTERCAPS) & RC_PALETTE;
- ReleaseDC(0, Screen);
- if (!PaletteDevice)
- {
- MessageBox(0,
- "Palette animation requires a palettized display device!",
- "Non-palettized Display",
- MB_OK);
- return FALSE;
- }
- }
-
- if (!hPrev)
- {
- /*
- * Register a class for the main application window
- */
- cls.hCursor = LoadCursor(NULL,IDC_ARROW);
- cls.hIcon = LoadIcon(hInst,"AppIcon");
- cls.lpszMenuName = "AppMenu";
- cls.lpszClassName = szAppName;
- cls.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
- cls.hInstance = hInst;
- cls.style = CS_BYTEALIGNCLIENT | CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS;
- cls.lpfnWndProc = (WNDPROC)AppWndProc;
- cls.cbWndExtra = 0;
- cls.cbClsExtra = 0;
-
- if (!RegisterClass(&cls))
- return FALSE;
- }
-
- dx = GetSystemMetrics (SM_CXSCREEN) / 2;
- dy = GetSystemMetrics (SM_CYSCREEN) / 2;
-
- hwndApp = CreateWindow (szAppName, // Class name
- szAppName, // Caption
- WS_OVERLAPPEDWINDOW, // Style bits
- CW_USEDEFAULT, 0, // Position
- dx,dy, // Size
- (HWND)NULL, // Parent window (no parent)
- (HMENU)NULL, // use class menu
- hInst, // handle to window instance
- (LPSTR)NULL // no params to pass on
- );
- ShowWindow(hwndApp,sw);
-
- //*** Create the WinGDC (actually 256x256)
- hdcOffscreen = Create100x100WinGDC();
-
- //*** Check the menu to reflect initial palette (red)
- hMenu = GetMenu(hwndApp);
- CheckMenuItem(hMenu, MENU_STATIC, fIncludeStatic ? MF_CHECKED : MF_UNCHECKED);
- CheckMenuItem(hMenu, MENU_RED, MF_CHECKED);
- CheckMenuItem(hMenu, MENU_GREEN, MF_UNCHECKED);
- CheckMenuItem(hMenu, MENU_BLUE, MF_UNCHECKED);
-
- //*** Hack to get the offscreen HBITMAP
- hbmOffscreen = (HBITMAP)SelectObject(hdcOffscreen, ghBitmapMonochrome);
- SelectObject(hdcOffscreen, hbmOffscreen);
-
- //*** Initialize the DIB to a wash from top to bottom
- gpBits = WinGGetDIBPointer(hbmOffscreen, (BITMAPINFO far *)&gInfo);
- DibCreateWash(&gInfo.InfoHeader, gpBits);
-
- //*** Reset the color palette
- ClearSystemPalette();
- CreateWashPalette();
-
- return TRUE;
- }
-
-
- /*----------------------------------------------------------------------------*\
- | AppExit() |
- | |
- | Description: |
- | app is just about to exit, cleanup |
- | |
- \*----------------------------------------------------------------------------*/
- void AppExit()
- {
- if (hdcOffscreen)
- Destroy100x100WinGDC(hdcOffscreen);
-
- if (hpalApp)
- DeleteObject(hpalApp);
-
- //*** Be sure to restore the state on exit!
- if (fIncludeStatic)
- AppActivate(FALSE);
- }
-
- /*----------------------------------------------------------------------------*\
- | WinMain( hInst, hPrev, lpszCmdLine, cmdShow ) |
- | |
- | Description: |
- | The main procedure for the App. After initializing, it just goes |
- | into a message-processing loop until it gets a WM_QUIT message |
- | (meaning the app was closed). |
- | |
- | Arguments: |
- | hInst instance handle of this instance of the app |
- | hPrev instance handle of previous instance, NULL if first |
- | szCmdLine ->null-terminated command line |
- | cmdShow specifies how the window is initially displayed |
- | |
- | Returns: |
- | The exit code as specified in the WM_QUIT message. |
- | |
- \*----------------------------------------------------------------------------*/
- int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
- {
- MSG msg;
-
- /* Call initialization procedure */
- if (!AppInit(hInst,hPrev,sw,szCmdLine))
- return FALSE;
-
- /*
- * Polling messages from event queue
- */
- for (;;)
- {
- if (PeekMessage(&msg, NULL, 0, 0,PM_REMOVE))
- {
- if (msg.message == WM_QUIT)
- break;
-
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- else
- {
- if (AppIdle())
- WaitMessage();
- }
- }
-
- AppExit();
- return msg.wParam;
- }
-
- /*----------------------------------------------------------------------------*\
- | AppIdle() |
- | |
- | Description: |
- | place to do idle time stuff. |
- | |
- | Returns: |
- | RETURN TRUE IF YOU HAVE NOTHING TO DO OTHERWISE YOUR APP WILL BE A |
- | CPU PIG! |
- \*----------------------------------------------------------------------------*/
- BOOL AppIdle()
- {
- if (fAppActive)
- {
- return TRUE;
- }
- else
- {
- //
- // we are a background app.
- //
- return TRUE; // nothing to do.
- }
- }
-
- /*----------------------------------------------------------------------------*\
- | AppPaint(hwnd, hdc) |
- | |
- | Description: |
- | The paint function. Right now this does nothing. |
- | |
- | Arguments: |
- | hwnd window painting into |
- | hdc display context to paint to |
- | |
- | Returns: |
- | nothing |
- | |
- \*----------------------------------------------------------------------------*/
- AppPaint (HWND hwnd, HDC hdc)
- {
- RECT rc;
-
- GetClientRect(hwnd,&rc);
-
- SetTextColor(hdc,GetSysColor(COLOR_WINDOWTEXT));
- SetBkColor(hdc,GetSysColor(COLOR_WINDOW));
-
- if (hdcOffscreen)
- {
- int i;
- PALETTEENTRY aPalette[256];
- RGBQUAD aPaletteRGB[256];
-
- //*** BEFORE BLTTING, match the DIB color table to the
- //*** current palette to match the animated palette
- GetPaletteEntries(hpalApp, 0, 256, aPalette);
- //*** Alas, palette entries are r-g-b, rgbquads are b-g-r
- for (i=0; i<256; ++i)
- {
- aPaletteRGB[i].rgbRed = aPalette[i].peRed;
- aPaletteRGB[i].rgbGreen = aPalette[i].peGreen;
- aPaletteRGB[i].rgbBlue = aPalette[i].peBlue;
- aPaletteRGB[i].rgbReserved = 0;
- }
- WinGSetDIBColorTable(hdcOffscreen, 0, 256, aPaletteRGB);
-
- WinGBitBlt(hdc,0,0,256,256,hdcOffscreen,0,0);
- }
-
- return TRUE;
- }
-
- /*----------------------------------------------------------------------------*\
- | AppWndProc( hwnd, uiMessage, wParam, lParam ) |
- | |
- | Description: |
- | The window proc for the app's main (tiled) window. This processes all |
- | of the parent window's messages. |
- | |
- \*----------------------------------------------------------------------------*/
-
- LONG FAR PASCAL _export AppWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
- {
- PAINTSTRUCT ps;
- HDC hdc;
- BOOL f;
- static int CurtainY = 0;
-
- switch (msg)
- {
- case WM_CREATE:
- break;
-
- case WM_ACTIVATEAPP:
- fAppActive = (BOOL)wParam;
-
- //*** Remap the system colors and deal with the palette
- if (fIncludeStatic == 0)
- {
- AppActivate(fAppActive);
-
- if (hpalApp)
- {
- HDC hdc = GetDC(hwnd);
-
- UnrealizeObject(hpalApp);
- SelectPalette(hdc, hpalApp, FALSE);
- RealizePalette(hdc);
-
- ReleaseDC(hwnd, hdc);
- }
- }
- break;
-
- case WM_TIMER:
- switch (wParam)
- {
- case 1:
- //*** Animate or terminate the "falling black curtain"
- {
- if (CurtainY < 256)
- {
- DibHorizontalLine(&gInfo.InfoHeader, gpBits, CurtainY, 0);
- CurtainY++;
- }
- else
- {
- CurtainY = 0;
- DibCreateWash(&gInfo.InfoHeader, gpBits);
- KillTimer(hwnd, wParam);
- }
- }
-
- //*** The DIB has changed - redisplay it
- InvalidateRect(hwnd, NULL, FALSE);
- UpdateWindow(hwnd);
-
- break;
-
- case 2:
- //*** Get the current palette
- GetPaletteEntries(hpalApp, 0, 256, aPalette);
-
- if (fIncludeStatic)
- {
- //*** We'll rotate the middle 236 entries, leaving
- //*** black and white at the top
- aPalette[245] = aPalette[10];
- AnimatePalette(hpalApp, 10, 236, &aPalette[11]);
- }
- else
- {
- //*** We'll rotate the middle 254 entries, leaving
- //*** black and white at the top
- aPalette[255] = aPalette[1];
- AnimatePalette(hpalApp, 1, 254, &aPalette[2]);
- }
- InvalidateRect(hwnd, NULL, FALSE);
- UpdateWindow(hwnd);
- break;
- }
- break;
-
- case WM_ERASEBKGND:
- break;
-
- case WM_INITMENU:
- break;
-
- case WM_COMMAND:
- return AppCommand(hwnd,msg,wParam,lParam);
-
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
-
- case WM_CLOSE:
- break;
-
- case WM_PALETTECHANGED:
- if ((HWND)wParam == hwnd)
- break;
-
- // fall through to WM_QUERYNEWPALETTE
-
- case WM_QUERYNEWPALETTE:
- hdc = GetDC(hwnd);
-
- if (hpalApp)
- SelectPalette(hdc, hpalApp, FALSE);
-
- f = RealizePalette(hdc);
- ReleaseDC(hwnd,hdc);
-
- if (f)
- InvalidateRect(hwnd,NULL,TRUE);
-
- return f;
-
- case WM_PAINT:
- hdc = BeginPaint(hwnd,&ps);
- if (hpalApp)
- {
- SelectPalette(hdc, hpalApp, FALSE);
- RealizePalette(hdc);
- }
- AppPaint (hwnd,hdc);
- EndPaint(hwnd,&ps);
- return 0L;
- }
- return DefWindowProc(hwnd,msg,wParam,lParam);
- }
-
- /*----------------------------------------------------------------------------*\
- | AppCommand(hwnd, msg, wParam, lParam ) |
- | |
- | Description: |
- | handles WM_COMMAND messages for the main window (hwndApp) |
- | of the parent window's messages. |
- | |
- \*----------------------------------------------------------------------------*/
- LONG AppCommand (HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
- {
- HMENU hMenu;
- HPALETTE hPal = GetStockObject(DEFAULT_PALETTE);
- HDC hdc = GetDC(hwnd);
-
- switch(wParam)
- {
- case MENU_ABOUT:
- DialogBox(hInstApp,"AppAbout",hwnd,AppAbout);
- break;
-
- case MENU_EXIT:
- PostMessage(hwnd,WM_CLOSE,0,0L);
- break;
-
- case MENU_RED:
- case MENU_GREEN:
- case MENU_BLUE:
- hMenu = GetMenu(hwndApp);
-
- CheckMenuItem(hMenu, MENU_RED, MF_UNCHECKED);
- CheckMenuItem(hMenu, MENU_GREEN, MF_UNCHECKED);
- CheckMenuItem(hMenu, MENU_BLUE, MF_UNCHECKED);
- CheckMenuItem(hMenu, wParam, MF_CHECKED);
-
- if (wParam == MENU_RED) gWashColor = Red;
- else if (wParam == MENU_GREEN) gWashColor = Green;
- else gWashColor = Blue;
-
- //*** Delete the old palette and create a new one
- hPal = SelectPalette(hdc, hPal, FALSE);
- DeleteObject(hPal);
-
- CreateWashPalette();
-
- InvalidateRect(hwnd, NULL, FALSE);
- UpdateWindow(hwnd);
- break;
-
- case MENU_PALETTE:
- hMenu = GetMenu(hwndApp);
- if (fAnimatePalette)
- {
- fAnimatePalette = 0;
- CheckMenuItem(hMenu, MENU_PALETTE, MF_UNCHECKED);
- KillTimer(hwnd, 2);
- }
- else
- {
- fAnimatePalette = 1;
- CheckMenuItem(hMenu, MENU_PALETTE, MF_CHECKED);
- SetTimer(hwnd, 2, 1, 0);
- }
- break;
-
- case MENU_CURTAIN:
- //*** Just start off the falling curtain
- SetTimer(hwnd, 1, 10, 0);
- break;
-
- case MENU_STATIC:
- hMenu = GetMenu(hwndApp);
-
- if (fIncludeStatic)
- {
- //*** Flag no static color use
- fIncludeStatic = 0;
- CheckMenuItem(hMenu, MENU_STATIC, MF_UNCHECKED);
-
- //*** Remap the system colors
- AppActivate(TRUE);
- }
- else
- {
- //*** Flag static color use
- fIncludeStatic = 1;
- CheckMenuItem(hMenu, MENU_STATIC, MF_CHECKED);
-
- //*** Remap the system colors to normal
- AppActivate(FALSE);
- }
-
- //*** Delete the old palette and create a new one
- hPal = SelectPalette(hdc, hPal, FALSE);
- DeleteObject(hPal);
-
- CreateWashPalette();
-
- InvalidateRect(hwnd, NULL, FALSE);
- UpdateWindow(hwnd);
- break;
- }
-
- ReleaseDC(hwnd, hdc);
-
- return 0L;
- }
-
- /*----------------------------------------------------------------------------*\
- | ErrMsg - Opens a Message box with a error message in it. The user can |
- | select the OK button to continue |
- \*----------------------------------------------------------------------------*/
- int ErrMsg (LPSTR sz,...)
- {
- char ach[128];
-
- wvsprintf (ach,sz,(LPSTR)(&sz+1)); /* Format the string */
- MessageBox(hwndApp,ach,szAppName,MB_OK|MB_ICONEXCLAMATION|MB_TASKMODAL);
- return FALSE;
- }
-
-
- /***************************************************************************
- Palette Creation
- Create three palettes of solid color washes, with or without statics
- */
-
- void CreateWashPalette(void)
- {
- RGBQUAD aWash[256];
- int i;
-
- //*** Fill in the palette with a 256-color ramp
- for (i=0; i<256; ++i)
- {
- aWash[i].rgbRed = aWash[i].rgbBlue = aWash[i].rgbGreen = 0;
-
- switch (gWashColor)
- {
- case Red:
- aWash[i].rgbRed = i;
- break;
-
- case Green:
- aWash[i].rgbGreen = i;
- break;
-
- case Blue:
- aWash[i].rgbBlue = i;
- break;
- }
-
- aWash[i].rgbReserved = 0;
- }
-
- //*** Turn the wash into a palette
- if (hpalApp)
- DeleteObject(hpalApp);
- hpalApp = CreateIdentityPalette(aWash, 256);
- }
-
- /***************************************************************************
- Dib drawing routines
-
- One creates a wash of color from left to right,
- the other draws a horizontal line at a given Y in the DIB
- */
-
- void DibCreateWash(BITMAPINFOHEADER far *Info, void far *pBits)
- {
- unsigned int dxBytes = DibWidthBytes(Info);
- int dxWidth = DibWidth(Info);
- int dyLines = (int)Info->biHeight;
- int i, j;
- char unsigned huge *pScanline = (char unsigned huge *)pBits;
- char unsigned huge *pPixel;
-
- if (dyLines < 0)
- {
- dyLines = -dyLines;
- }
-
- for (i=0; i<dyLines; ++i)
- {
- //*** Point to the beginning of this scan line in the DIB
- pPixel = pScanline;
-
- //*** Step through this scan line and fill it
- //*** Wash up on the evens, then down on the odds
- for (j=0; j<256; j+=2)
- {
- *pPixel = (char unsigned)(j % 256);
- pPixel++;
- }
- for (j=253; j>0; j-=2)
- {
- *pPixel = (char unsigned)(j % 256);
- pPixel++;
- }
- //*** Make the last column white
- *pPixel = 255;
-
- //*** Move pointer to the next scan line
- pScanline += dxBytes;
- }
- }
-
- void DibHorizontalLine(BITMAPINFOHEADER far *Info, void far *pBits,
- int y, char unsigned color)
- {
- unsigned int dxBytes = DibWidthBytes(Info);
- char unsigned huge *pPixel;
- int dxWidth = DibWidth(Info);
- int dyLines = (int)Info->biHeight;
- int i;
-
- //*** Account for top-down and bottom-up DIBs
- if (dyLines > 0)
- {
- pPixel = (char unsigned huge *)pBits +
- (long)(dyLines - 1 - y) * (long)dxBytes;
- }
- else
- {
- pPixel = (char unsigned huge *)pBits +
- (long)y * (long)dxBytes;
- }
-
- for (i=0; i<dxWidth; ++i)
- {
- *pPixel = color;
- pPixel++;
- }
- }
-
-
-