/****(AnimView)************************************************************* LEAD Functions Used. L_FileInfo L_InitBitmap L_LoadBitmap L_AllocateBitmap L_AccessBitmap L_ReleaseBitmap L_FreeBitmap L_CreatePaintPalette L_PaintDC L_LoadBitmapList L_GetBitmapListItem L_GetBitmapListCount L_SetBitmapListItem L_DlgColorRes L_ColorResBitmapList L_GetBitmapColors L_CreateBitmap L_TranslateBitmapColor L_DestroyBitmapList L_CreatePlayback L_ProcessPlayback L_CancelPlaybackWait L_GetPlaybackUpdateRect L_DestroyPlayback This example will: 1.Loading file information. 2.load an image whose name is sent through command line. 3.This example will be display animation for images whose extention ("*.AVI" or "*.GIF"). *****************************************************************************/ #include #include #include #include "TCHAR.H" #include "AnimView.h" #include "resource.h" static LPCOLORPARM pParm; static COLORLAYOUT colorlayout; static COLORREF crColor; L_BOOL ExtractCommandData ( ) ; /*---[WinMain]--------------------------------------------------------------- Syntax: int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow ) Parameters: hInstance Current instance. hPrevInstance previous instance. lpCmdLine command line. nCmdShow show-window type. Prototype: Windows.h Notes: Windows main function, calls initialization function and processes message loop. ----------------------------------------------------------------------------*/ int PASCAL WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MSG msg; UNLOCKSUPPORT(); if (!hPrevInstance) if (!InitApplication (hInstance)) return (FALSE); ExtractCommandData ( ) ; L_DlgInit(DLG_INIT_COLOR); if (!InitInstance (hInstance, nCmdShow)) return (FALSE); while (GetMessage (&msg, NULL, 0, 0)) { TranslateMessage (&msg); DispatchMessage (&msg); } L_DlgFree(); return (msg.wParam); } L_BOOL InitApplication (HINSTANCE hInstance) { WNDCLASS wcWindowClass; wcWindowClass.style = 0; wcWindowClass.lpfnWndProc = MainWndProc; wcWindowClass.cbClsExtra = 0; wcWindowClass.cbWndExtra = 0; wcWindowClass.hInstance = hInstance; wcWindowClass.hIcon = LoadIcon (hInstance, MAKEINTRESOURCE (IDR_ICONE)); wcWindowClass.hCursor = LoadCursor (NULL, IDC_ARROW); wcWindowClass.hbrBackground = (HBRUSH)GetStockObject (GRAY_BRUSH); wcWindowClass.lpszMenuName = TEXT("MAIN_MENU"); wcWindowClass.lpszClassName = TEXT("LEADWClass"); return (RegisterClass (&wcWindowClass)); } L_BOOL InitInstance (HINSTANCE hInstance, L_INT nCmdShow) { HWND hWnd; L_INT nRet; L_TCHAR buf[1024]; Data.FileInfo.uStructSize = sizeof(FILEINFO); L_FileInfo(Data.szFilename, &Data.FileInfo, sizeof(FILEINFO), 0, NULL); wsprintf(buf, TEXT("File Name:\t%s\n\n") TEXT("Format:\t\t%d\n\n") TEXT("Width:\t\t%d\n\n") TEXT("Height:\t\t%d\n\n") TEXT("BitsPerPixel:\t%d\n\n") TEXT("Size On Disk:\t%ld\n\n") TEXT("Size In Memory:\t%ld\n\n") TEXT("Compression:\t%s\n"), (L_TCHAR L_FAR *) Data.FileInfo.Name, Data.FileInfo.Format, Data.FileInfo.Width, Data.FileInfo.Height, Data.FileInfo.BitsPerPixel, Data.FileInfo.SizeDisk, Data.FileInfo.SizeMem, (L_TCHAR L_FAR *) Data.FileInfo.Compression), MessageBox( NULL, buf, TEXT("File Information"), MB_OK | MB_ICONINFORMATION); Data.FileInfo.uStructSize = sizeof(FILEINFO); if ((nRet = L_FileInfo (Data.szFilename, &Data.FileInfo, sizeof(FILEINFO), 0, NULL)) != SUCCESS) { wsprintf (buf, TEXT("ERROR %d from L_FileInfo on file %s"), nRet, (LPTSTR) Data.szFilename); MessageBox (NULL, buf, TEXT("ERROR"), MB_OK | MB_ICONERROR); return (FALSE); } Data.hList = NULL; Data.hPlayback = NULL; Data.hPalette = NULL; Data.nBitmap = 0; Data.uLastFrame= 0; L_InitBitmap(&Data.BitmapHandle, sizeof(BITMAPHANDLE), 0, 0, 0); rWndSize.top = 0; rWndSize.left = 0; rWndSize.bottom = Data.FileInfo.Height; rWndSize.right = Data.FileInfo.Width; AdjustWindowRect (&rWndSize, WS_OVERLAPPEDWINDOW, FALSE); hWnd = CreateWindow (TEXT("LEADWClass"), TEXT("Animation viewer demo"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, rWndSize.right - rWndSize.left, rWndSize.bottom - rWndSize.top, NULL, NULL, hInstance, NULL); if (hWnd == NULL) return (FALSE); ShowWindow (hWnd, nCmdShow); UpdateWindow (hWnd); return (TRUE); } L_INT32 EXT_FUNCTION MainWndProc (HWND hWnd, L_UINT Message, WPARAM wParam, LPARAM lParam) { switch (Message) { HANDLE_MSG (hWnd, WM_CREATE, Window_OnCreate); HANDLE_MSG (hWnd, WM_COMMAND, Window_OnCommand); HANDLE_MSG (hWnd, WM_QUERYNEWPALETTE, Window_OnQueryNewPalette); HANDLE_MSG (hWnd, WM_PALETTECHANGED, Window_OnPaletteChanged); HANDLE_MSG (hWnd, WM_PALETTEISCHANGING, Window_OnPaletteChanging); HANDLE_MSG (hWnd, WM_SYSCOLORCHANGE, Window_SysColorChange); HANDLE_MSG (hWnd, WM_ACTIVATE, Window_OnActivate); HANDLE_MSG (hWnd, WM_PAINT, Window_OnPaint); HANDLE_MSG (hWnd, WM_DESTROY, Window_OnDestroy); HANDLE_MSG (hWnd, WM_KEYDOWN, Window_OnKeyDown); HANDLE_MSG (hWnd, WM_INITMENUPOPUP, Window_OnInitMenuPopup); HANDLE_MSG (hWnd, WM_SIZE, Window_OnSize); HANDLE_MSG (hWnd, WM_ERASEBKGND, Window_OnEraseBkGnd); HANDLE_MSG (hWnd, WM_CLOSE, Window_OnClose); } return DefWindowProc (hWnd, Message, wParam, lParam); } BOOL Window_OnEraseBkGnd(HWND hWnd, HDC hdc) { HBRUSH hBrush; HRGN hRgn; HRGN hInvalidRgn; RECT rcUpdate; SetRect (&rcUpdate, 0, 0,rcDisplay.right , rcDisplay.bottom); hRgn = CreateRectRgnIndirect(&rcDisplay); hInvalidRgn = CreateRectRgnIndirect(&rcUpdate); CombineRgn(hInvalidRgn,hInvalidRgn,hRgn,RGN_XOR); hBrush = (HBRUSH)GetClassLong(hWnd,GCL_HBRBACKGROUND); FillRgn (hdc, hInvalidRgn, hBrush); DeleteObject(hRgn); DeleteObject(hInvalidRgn); return TRUE; } BOOL Window_OnCreate (HWND hWnd, CREATESTRUCT FAR * lpCreateStruct) { L_UINT nBit; L_INT nRet; L_TCHAR buf[MAX_PATH]; L_BOOL bAnimated; UNREFERENCED_PARAMETER (lpCreateStruct); memset(buf, 0, MAX_PATH); bAnimated = (Data.FileInfo.Format == FILE_GIF || Data.FileInfo.Format == FILE_AVI); if (bAnimated) { nRet = L_LoadBitmapList(Data.szFilename, &Data.hList, Data.BitmapHandle.BitsPerPixel,ORDER_BGRORGRAY, NULL,&Data.FileInfo); if (nRet == SUCCESS) nRet = L_GetBitmapListItem(Data.hList, 0, &Data.BitmapHandle, sizeof(BITMAPHANDLE)); L_GetBitmapListCount(Data.hList,&nBit); if(nBit==1) wsprintf(buf, TEXT("The loaded file is not animated")); else wsprintf(buf, TEXT("The loaded file is animated")); } else { nRet = L_LoadBitmap (Data.szFilename, &Data.BitmapHandle, sizeof(BITMAPHANDLE), Data.BitmapHandle.BitsPerPixel, ORDER_BGRORGRAY,NULL, &Data.FileInfo); wsprintf(buf, TEXT("The loaded file is not animated")); } if (nRet != SUCCESS) { wsprintf (buf, TEXT("Error %d Loading %s file"), nRet, (LPTSTR) Data.szFilename); MessageBox (hWnd, buf, TEXT("Error"), MB_OK | MB_ICONERROR); return FALSE; } MessageBox (hWnd, buf, TEXT("About File"), MB_OK | MB_ICONINFORMATION); FORWARD_WM_QUERYNEWPALETTE (hWnd, SendMessage); return TRUE; } VOID Window_OnInitMenuPopup (HWND hWnd, HMENU hMenu, L_INT nItem, BOOL fSystemMenu) { CheckMenuItem (hMenu,IDM_ANILOOP, MF_BYCOMMAND |(bLoop) ? MF_CHECKED : MF_UNCHECKED); EnableMenuItems(hWnd, hMenu); EnableMenuItem(hMenu,IDM_EXIT,(!bPlay) ? MF_ENABLED : MF_GRAYED); } VOID Window_OnCommand (HWND hWnd, L_INT id, HWND hwndCtl, UINT codeNotify) { L_UINT i; L_INT nRet; L_BOOL bRet = FALSE; HMENU hMenu; HCURSOR hCursor; L_INT nValue; BOOL bImageChanged = FALSE; HBRUSH hBrush; FRAMESETTINGSPARM FrameSettingsParm; UNREFERENCED_PARAMETER (hwndCtl); UNREFERENCED_PARAMETER (codeNotify); hMenu=GetMenu(hWnd); switch(id) { case IDM_HELP_ABOUT: DoDialogBoxParam(IDD_ABOUTBOX, hWnd, (DLGPROC) AboutDlgProc, 0); return; case ID_ANIMATION_STOPPLAY: bKillPlay = TRUE; bPlay = FALSE; bLoop = FALSE; SetWindowText (hWnd, TEXT("Animation viewer demo")); L_GetBitmapListCount(Data.hList, &Data.nBitmap); if (Data.nBitmap == Data.uLastFrame) Data.nBitmap = 0; EnableMenuItems(hWnd, hMenu); return; case IDM_EXIT: bKillPlay = TRUE; bPlay = FALSE; bBackground = FALSE; bLoop = FALSE; bSet = FALSE; FORWARD_WM_DESTROY (hWnd,SendMessage); return; case IDM_PLAYANI: bKillPlay = FALSE; bPlay = TRUE; EnableMenuItems(hWnd,hMenu); if(bPlay) { if (Data.FileInfo.Format == FILE_GIF || Data.FileInfo.Format == FILE_AVI) { SetWindowText (hWnd, TEXT("Animation is now playing...")); TestAnimate(Data.szFilename, hWnd); SetWindowText (hWnd, TEXT("Animation viewer demo")); while (bLoop) TestAnimate(Data.szFilename, hWnd); } } bKillPlay = TRUE; bPlay = FALSE; EnableMenuItems(hWnd,hMenu); break; case IDM_ANILOOP: bLoop = !bLoop; CheckMenuItem (hMenu,IDM_ANILOOP,MF_BYCOMMAND |(bLoop) ? MF_CHECKED : MF_UNCHECKED); return; case IDM_ANIFRAMESETTINGS: bSet = TRUE; L_GetBitmapListItem(Data.hList, Data.nBitmap, &Data.BitmapHandle, sizeof(BITMAPHANDLE)); L_GetBitmapListCount(Data.hList, &Data.uLastFrame); FrameSettingsParm.fTransparency = Data.BitmapHandle.Flags.Transparency; FrameSettingsParm.fWaitUserInput = Data.BitmapHandle.Flags.WaitUserInput; FrameSettingsParm.crTransparency = Data.BitmapHandle.Transparency; FrameSettingsParm.uDelay = Data.BitmapHandle.Delay; FrameSettingsParm.uDisposalMethod = Data.BitmapHandle.DisposalMethod; FrameSettingsParm.nLeft = Data.BitmapHandle.Left; FrameSettingsParm.nTop = Data.BitmapHandle.Top; FrameSettingsParm.fApplytoall = FALSE; FrameSettingsParm.pBitmap = &Data.BitmapHandle; if(GetFrameSettingsParm(hWnd, &FrameSettingsParm)) { for(i = (FrameSettingsParm.fApplytoall ? 0 : Data.uFrame); i <= (FrameSettingsParm.fApplytoall ? Data.uLastFrame : Data.uFrame); i++) { L_GetBitmapListItem(Data.hList, i, &Data.BitmapHandle, sizeof(BITMAPHANDLE)); Data.BitmapHandle.Flags.Transparency = FrameSettingsParm.fTransparency; Data.BitmapHandle.Flags.WaitUserInput = FrameSettingsParm.fWaitUserInput; Data.BitmapHandle.Transparency = FrameSettingsParm.crTransparency; Data.BitmapHandle.Delay = FrameSettingsParm.uDelay; Data.BitmapHandle.DisposalMethod = FrameSettingsParm.uDisposalMethod; Data.BitmapHandle.Left = FrameSettingsParm.nLeft;; Data.BitmapHandle.Top = FrameSettingsParm.nTop; L_SetBitmapListItem(Data.hList, i, &Data.BitmapHandle); } } break; case IDM_ANICOLORRES: { COLORRESDLGPARAMS DlgParams ; memset(&DlgParams, 0, sizeof(COLORRESDLGPARAMS)); DlgParams.uStructSize = sizeof(COLORRESDLGPARAMS); DlgParams.pBitmap = &Data.BitmapHandle; DlgParams.nBitsPerPixel = Data.BitmapHandle.BitsPerPixel ; DlgParams.uColorResFlags = CRF_SLOWMATCH; DlgParams.uDlgFlags = DLG_COLORRES_SHOW_PREVIEW| DLG_COLORRES_SHOW_ORDER | DLG_COLORRES_SHOW_TOOL_ZOOMLEVEL | DLG_COLORRES_SHOW_OPENPALFILE ; DlgParams.uDlgFlagsEx = DLG_COLORRES_SHOW_BITALL | DLG_COLORRES_SHOW_DITHER_ALL | DLG_COLORRES_SHOW_PAL_ALL ; if(L_DlgColorRes(hWnd, &DlgParams) == SUCCESS_DLG_OK) { bImageChanged=TRUE; nValue = 0; nRet = L_ColorResBitmapList ( Data.hList, DlgParams.nBitsPerPixel, DlgParams.uColorResFlags, NULL, DlgParams.hpalCustom, 0); if(nRet == SUCCESS) { L_GetBitmapListItem(Data.hList, Data.nBitmap, &Data.BitmapHandle, sizeof(BITMAPHANDLE)); InvalidateRect (hWnd, NULL, TRUE); FORWARD_WM_QUERYNEWPALETTE (hWnd,SendMessage); } if(DlgParams.hpalCustom) DeleteObject(DlgParams.hpalCustom); } break; } case IDM_ANIBACKCOLOR: bBackground = TRUE; GetColor(hWnd, &Data.BitmapHandle, &crColor); hBrush=CreateSolidBrush(crColor); SetClassLong(hWnd,GCL_HBRBACKGROUND,(LONG)hBrush); break; case IDM_ANIFIRSTFRAME: Data.nBitmap = 0; nRet = L_GetBitmapListItem(Data.hList, Data.nBitmap, &Data.BitmapHandle, sizeof(BITMAPHANDLE)); if(nRet != SUCCESS) { MessageBox(hWnd,TEXT("Error Getting Items"),TEXT("Error Message"),MB_OK | MB_ICONERROR); return; } InvalidateRect (hWnd, NULL, TRUE); break; case IDM_ANIPREVFRAME: if(Data.nBitmap > 0) { Data.nBitmap--; L_GetBitmapListItem(Data.hList, Data.nBitmap, &Data.BitmapHandle, sizeof(BITMAPHANDLE)); InvalidateRect (hWnd, NULL, TRUE); } break; case IDM_ANINEXTFRAME: if(Data.nBitmap < Data.uLastFrame - 1) { Data.nBitmap++; L_GetBitmapListItem(Data.hList, Data.nBitmap, &Data.BitmapHandle, sizeof(BITMAPHANDLE)); InvalidateRect (hWnd, NULL, TRUE); } break; case IDM_ANILASTFRAME: L_GetBitmapListCount(Data.hList, &Data.nBitmap); Data.nBitmap--; L_GetBitmapListItem(Data.hList, Data.nBitmap, &Data.BitmapHandle, sizeof(BITMAPHANDLE)); InvalidateRect (hWnd, NULL, TRUE); break; } hCursor = SetCursor (LoadCursor (NULL, IDC_WAIT)); SetCursor (hCursor); return; } VOID Window_OnPaletteChanged (HWND hWnd, HWND hWndPaletteChange) { HDC hDC; HPALETTE hPalette; if (hWnd == hWndPaletteChange) return; if (Data.hPalette) { DeleteObject (Data.hPalette); Data.hPalette = NULL; } if (Data.BitmapHandle.Flags.Allocated) { hDC = GetDC (hWnd); Data.hPalette = L_CreatePaintPalette (hDC, &Data.BitmapHandle); hPalette = SelectPalette (hDC, Data.hPalette, TRUE); RealizePalette (hDC); InvalidateRect (hWnd, NULL, FALSE); SelectPalette (hDC, hPalette, TRUE); ReleaseDC (hWnd, hDC); } } VOID Window_OnPaletteChanging (HWND hWnd, HWND hWndPaletteChange) { Window_OnPaletteChanged (hWnd, hWndPaletteChange); } VOID Window_SysColorChange(HWND hwnd) { Window_OnQueryNewPalette (hwnd); } BOOL Window_OnQueryNewPalette (HWND hWnd) { HDC hDC; HPALETTE hPalette; L_INT nNoColors = 0; if (Data.hPalette) { DeleteObject (Data.hPalette); Data.hPalette = NULL; } if (Data.BitmapHandle.Flags.Allocated) { hDC = GetDC (hWnd); Data.hPalette = L_CreatePaintPalette (hDC, &Data.BitmapHandle); if (Data.hPalette) { hPalette = SelectPalette (hDC, Data.hPalette, FALSE); nNoColors = RealizePalette (hDC); if (nNoColors) InvalidateRect (hWnd, NULL, FALSE); SelectPalette (hDC, hPalette, TRUE); } ReleaseDC (hWnd, hDC); } return (nNoColors); } VOID Window_OnActivate(HWND hwnd, UINT state, HWND hwndActDeact, BOOL fMinimized) { if(state!=WA_INACTIVE) Window_OnQueryNewPalette (hwnd); } VOID Window_OnPaint (HWND hWnd) { HDC hdc = GetDC(hWnd); PAINTSTRUCT ps; HPALETTE hOldPal = NULL; RECT rcUpdate; hdc = BeginPaint (hWnd, &ps); GetClientRect(hWnd,&rcDisplay); GetClientRect(hWnd,&rcUpdate); if (Data.BitmapHandle.Flags.Allocated) { if (Data.hPalette) hOldPal = SelectPalette (hdc, Data.hPalette, TRUE); SetRect (&rcUpdate, Data.BitmapHandle.Top, Data.BitmapHandle.Left,rcDisplay.right , rcDisplay.bottom); L_PaintDC(hdc, &Data.BitmapHandle, NULL, NULL, &rcDisplay, &rcDisplay, SRCCOPY); if (Data.hPalette) SelectPalette (hdc, hOldPal, TRUE); } ReleaseDC(hWnd,hdc); EndPaint (hWnd, &ps); } VOID Window_OnDestroy (HWND hWnd) { UNREFERENCED_PARAMETER (hWnd); bKillPlay = TRUE; bPlay = FALSE; bLoop = FALSE; bBackground = FALSE; if (Data.hList) L_DestroyBitmapList(Data.hList); if (Data.BitmapHandle.Flags.Allocated) L_FreeBitmap (&Data.BitmapHandle); if (Data.hPalette) DeleteObject (Data.hPalette); PostQuitMessage (0); return; } VOID TestAnimate(L_TCHAR L_FAR * pszFilename, HWND hWnd) { MSG msg; HDC hdc; L_UINT uState; L_UINT uCount=0; HPALETTE hPalette = NULL; L_INT nRet; L_TCHAR buf[1024]; RECT rcUpdate; BITMAPHANDLE NBitmap; nRet = L_GetBitmapListItem(Data.hList, 0, &NBitmap, sizeof(BITMAPHANDLE)); if (nRet != SUCCESS) { wsprintf (buf, TEXT("ERROR %d from L_FileInfo on file %s"), nRet, (LPTSTR) Data.szFilename); MessageBox (NULL, buf, TEXT("ERROR"), MB_OK | MB_ICONERROR); return; } hdc = GetDC (hWnd); GetClientRect(hWnd,&rcDisplay); Data.hPalette = L_CreatePaintPalette(hdc, &Data.BitmapHandle); nRet = L_CreateBitmap(&Data.BitmapHandle, sizeof(BITMAPHANDLE), TYPE_CONV, NBitmap.Width, NBitmap.Height, NBitmap.BitsPerPixel, NBitmap.Order, NULL, NBitmap.ViewPerspective, NULL, 0); if (nRet != SUCCESS) { wsprintf (buf, TEXT("ERROR %d from L_FileInfo on file %s"), nRet, (LPTSTR) Data.szFilename); MessageBox (NULL, buf, TEXT("ERROR"), MB_OK | MB_ICONERROR); return; } nRet = L_CopyBitmap(&Data.BitmapHandle, &NBitmap, sizeof(BITMAPHANDLE)); if (nRet != SUCCESS) { wsprintf (buf, TEXT("ERROR %d from L_FileInfo on file %s"), nRet, (LPTSTR) Data.szFilename); MessageBox (NULL, buf, TEXT("ERROR"), MB_OK | MB_ICONERROR); return; } Data.BitmapHandle.Background = crColor; if (Data.hList) { L_CreatePlayback(&Data.hPlayback, &Data.BitmapHandle, Data.hList); do { L_ProcessPlayback(Data.hPlayback, &uState); switch(uState) { case PLAYSTATE_WAITINPUT: L_CancelPlaybackWait(Data.hPlayback); break; case PLAYSTATE_POSTCLEAR: case PLAYSTATE_POSTRENDER: L_GetPlaybackUpdateRect(Data.hPlayback, &rcUpdate, TRUE); hdc = GetDC(hWnd); if (Data.hPalette) { hPalette = SelectPalette (hdc, Data.hPalette, TRUE); RealizePalette(hdc); } L_PaintDC(hdc, &Data.BitmapHandle, NULL, NULL, &rcDisplay, &rcDisplay, SRCCOPY); if (Data.hPalette) SelectPalette (hdc, hPalette, TRUE); ReleaseDC(hWnd, hdc); break; } if (Data.hList) { nRet = L_GetBitmapListCount(Data.hList,&uCount); if (nRet != SUCCESS) { wsprintf (buf, TEXT("ERROR %d from L_FileInfo on file %s"), nRet, (LPTSTR) Data.szFilename); MessageBox (NULL, buf, TEXT("ERROR"), MB_OK | MB_ICONERROR); return; } } SetFocus(hWnd); while (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) { TranslateMessage (&msg); DispatchMessage (&msg); } if ((uCount > 1) && (uState == PLAYSTATE_END)) { if (bLoop) { Data.nBitmap =0; uState = PLAYSTATE_PRERENDER; } else Data.nBitmap++; } }while(uState != PLAYSTATE_END && !bKillPlay); L_DestroyPlayback(Data.hPlayback, NULL); } } VOID Window_OnKeyDown (HWND hWnd, UINT vk, BOOL fDown, int cRepeat, UINT flags) { switch (vk) { case VK_ESCAPE: bKillPlay = TRUE; bPlay = FALSE; bLoop = FALSE; SetWindowText (hWnd, TEXT("Animation viewer demo")); EnableMenuItems(hWnd, GetMenu(hWnd)); break; } } L_INT DoDialogBoxParam(L_INT nDialog, HWND hWnd, DLGPROC pfnDialog, LPARAM lParam) { DLGPROC pfn; L_INT nRet; pfn = (DLGPROC) MakeProcInstance((FARPROC) pfnDialog, hInstMain); nRet = DialogBoxParam (hInstMain, MAKEINTRESOURCE (nDialog), hWnd, pfn, lParam); FreeProcInstance((FARPROC) pfn); return(nRet); } L_BOOL CALLBACK AboutDlgProc (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { UNREFERENCED_PARAMETER(lParam); switch (message) { case WM_INITDIALOG: return TRUE; case WM_CLOSE: EndDialog (hDlg, 0); return TRUE; case WM_COMMAND: switch (LOWORD(wParam)) { case IDOK: EndDialog (hDlg, 0); return TRUE; } } return FALSE; } L_BOOL GetColor(HWND hWnd, pBITMAPHANDLE pBitmap, COLORREF FAR *pColor) { static COLORREF CustomColors[16] = { RGB(0, 0, 0), RGB(128, 0, 0), RGB(0, 128, 0), RGB(128, 128, 0), RGB(0, 0, 128), RGB(128, 0, 128), RGB(0, 128, 128), RGB(128, 128, 128), RGB(192, 192, 192), RGB(255, 0, 0), RGB(0, 255, 0), RGB(255, 255, 0), RGB(0, 0, 255), RGB(255, 0, 255), RGB(0, 255, 255), RGB(255, 255, 255), }; static COLORREF rgbColor = RGB(0,0, 0); /* I want to use this function to diplay the current palette (pColor is NULL) and to pick a transparent color (pColor != NULL) */ if(!pColor) pColor = &rgbColor; if( pBitmap->BitsPerPixel > 8 ) { CHOOSECOLOR color; _fmemset(&color, 0, sizeof(CHOOSECOLOR)); color.lStructSize = sizeof(CHOOSECOLOR); color.hwndOwner = hWnd; color.rgbResult = *pColor; color.lpCustColors = CustomColors; color.Flags = CC_FULLOPEN | CC_RGBINIT; if( ChooseColor(&color) ) { *pColor = color.rgbResult; return TRUE; } else return FALSE; } else { COLORPARM colorparm; colorparm.nColors = pBitmap->nColors; colorparm.hPal = L_DupBitmapPalette(pBitmap); L_GetBitmapColors(pBitmap, 0, pBitmap->nColors, colorparm.palColors); if( colorparm.nColors <= 0 ) return 0; /* some error occurred */ if( *pColor & 0xFF000000 ) colorparm.nColorIndex = (L_INT)(*pColor & 0x00FFFFFF); else colorparm.nColorIndex = GetNearestPaletteIndex(colorparm.hPal, *pColor); colorparm.nColorIndex = max(0, min(colorparm.nColorIndex,colorparm.nColors-1)); switch( DoDialogBoxParam(IDD_BITMAPCOLORS, hWnd, (DLGPROC)BitmapColorsDlgProc, (LPARAM)(LPCOLORPARM)&colorparm) ) { case IDOK: *pColor = RGB(colorparm.palColors[colorparm.nColorIndex].rgbRed, colorparm.palColors[colorparm.nColorIndex].rgbGreen, colorparm.palColors[colorparm.nColorIndex].rgbBlue); DeleteObject(colorparm.hPal); return TRUE; default: DeleteObject(colorparm.hPal); return FALSE; } } } L_VOID SetCurrentColor( HWND hDlg, LPRGBQUAD palColors, L_INT nColorIndex ) { InvalidateRect( GetDlgItem(hDlg,IDC_CURRENTCOLOR), NULL, FALSE); SetDlgItemInt( hDlg, IDC_CURRENTRED, palColors[nColorIndex].rgbRed, FALSE); SetDlgItemInt( hDlg, IDC_CURRENTGREEN, palColors[nColorIndex].rgbGreen, FALSE); SetDlgItemInt( hDlg, IDC_CURRENTBLUE, palColors[nColorIndex].rgbBlue, FALSE); } L_VOID GenerateColorLayout(HWND hDlg, LPCOLORLAYOUT pColorLayout, L_INT nColors, L_INT L_FAR *deltaX, L_INT L_FAR *deltaY) { RECT rcControl; pColorLayout->nTotalColors = nColors; pColorLayout->nCols = 16; /* the number of rows. The last row might not be full */ pColorLayout->nRows = (nColors + pColorLayout->nCols - 1) / pColorLayout->nCols; /* the size in rcControl is in screen coordinates */ GetWindowRect( GetDlgItem(hDlg,IDC_COLORARRAY), &rcControl ); /* pick some arbitrary values for the gaps */ /* make sure the gaps are at least 3 to be able to draw the focus rectangle around the selected color */ pColorLayout->nDeltaWidth = 4; pColorLayout->nDeltaHeight = 4; /* calculate the width and height of the boxes as if you had 16x16 rows */ /* substract 4 so that nOffsetX is at least 2 to be able to draw the focus rectangle around the very first box */ pColorLayout->nBoxWidth = ((rcControl.right - rcControl.left) - 4 - pColorLayout->nDeltaWidth * (16 - 1)) / 16; pColorLayout->nBoxHeight = ((rcControl.bottom - rcControl.top) - 4 - pColorLayout->nDeltaHeight * (16 - 1)) / 16; /* determine the required width for the button */ if( pColorLayout->nRows == 1 ) pColorLayout->nCols = nColors; /* I won't be using deltaX, but calculate it anyway */ *deltaX = pColorLayout->nCols * (pColorLayout->nBoxWidth + pColorLayout->nDeltaWidth) - pColorLayout->nDeltaWidth; *deltaX -= (rcControl.right - rcControl.left); /* this is the value I really need */ *deltaY = pColorLayout->nRows * (pColorLayout->nBoxHeight + pColorLayout->nDeltaHeight) - pColorLayout->nDeltaHeight; *deltaY -= (rcControl.bottom - rcControl.top); /* for an aesthetic effect, split what's left in two */ pColorLayout->nOffsetX = (rcControl.right - rcControl.left) - (pColorLayout->nBoxWidth + pColorLayout->nDeltaWidth) * (16 - 1) - pColorLayout->nBoxWidth; pColorLayout->nOffsetX /= 2; pColorLayout->nOffsetY = 0; } L_VOID ResizeDlgItem(HWND hDlg, L_INT nID, L_INT dx, L_INT dy) { RECT rc; HWND hwndItem; hwndItem = (nID) ? GetDlgItem(hDlg, nID) : hDlg; GetWindowRect(hwndItem, &rc); if(nID) { ScreenToClient(hDlg, (LPPOINT) &rc.left); ScreenToClient(hDlg, (LPPOINT) &rc.right); } MoveWindow(hwndItem, rc.left, rc.top, rc.right - rc.left + dx, rc.bottom - rc.top + dy, FALSE); } L_VOID OffsetDlgItem(HWND hDlg, L_INT nID, L_INT dx, L_INT dy) { RECT rc; HWND hwndItem; hwndItem = GetDlgItem(hDlg, nID); GetWindowRect(hwndItem, &rc); ScreenToClient(hDlg, (LPPOINT) &rc.left); ScreenToClient(hDlg, (LPPOINT) &rc.right); MoveWindow(hwndItem, rc.left + dx, rc.top + dy, rc.right - rc.left, rc.bottom - rc.top, FALSE); } LRESULT CALLBACK ColorArrayWindowProc(HWND hWnd, L_UINT Msg, WPARAM wParam, LPARAM lParam) { L_INT nNewIndex; L_INT row, col; HDC hDC; RECT rect; switch (Msg) { case WM_LBUTTONDOWN: { nNewIndex = GetColorIndex( &colorlayout, LOWORD(lParam), HIWORD(lParam) ); if( nNewIndex != nIndex && nNewIndex >= 0 && nNewIndex < colorlayout.nTotalColors ) { hDC = GetDC(hWnd); /* get rid of the old focus rectangle */ row = nIndex / colorlayout.nCols; col = nIndex % colorlayout.nCols; SetRect( &rect, 1, 1, colorlayout.nBoxWidth - 1, colorlayout.nBoxHeight - 1 ); OffsetRect( &rect, colorlayout.nOffsetX + col *(colorlayout.nBoxWidth+colorlayout.nDeltaWidth), colorlayout.nOffsetY + row *(colorlayout.nBoxHeight+colorlayout.nDeltaHeight) ); DrawFocusRect( hDC, &rect ); /* set the new index */ nIndex = nNewIndex; /* paint the new focus rectangle */ row = nIndex / colorlayout.nCols; col = nIndex % colorlayout.nCols; SetRect( &rect, 1, 1, colorlayout.nBoxWidth - 1, colorlayout.nBoxHeight - 1 ); OffsetRect( &rect, colorlayout.nOffsetX + col *(colorlayout.nBoxWidth+colorlayout.nDeltaWidth), colorlayout.nOffsetY + row *(colorlayout.nBoxHeight+colorlayout.nDeltaHeight) ); DrawFocusRect( hDC, &rect ); ReleaseDC( hWnd, hDC ); } SetCurrentColor(GetParent(hWnd), pParm->palColors, nIndex); break; } case WM_PALETTECHANGED: if( (HWND)wParam != hWnd ) { InvalidateRect(hWnd,NULL,FALSE); UpdateWindow(hWnd); } return 0; case WM_QUERYNEWPALETTE: return 1; } #if _MSC_VER <= 1100 return CallWindowProc((FARPROC)pfnDefaultButtonProc, hWnd, Msg, wParam, lParam); #else return CallWindowProc((WNDPROC)pfnDefaultButtonProc, hWnd, Msg, wParam, lParam); #endif } L_VOID DrawColoredRectangle(HDC hDC, L_INT left, L_INT top, L_INT width, L_INT height, COLORREF color) { LOGBRUSH logbrush; HBRUSH hBrush, hOldBrush; HPEN hPen, hOldPen; logbrush.lbStyle = BS_SOLID; logbrush.lbColor = color; hBrush = CreateBrushIndirect(&logbrush); hOldBrush = (HBRUSH)SelectObject(hDC, hBrush); hPen = CreatePen(PS_INSIDEFRAME, 3, RGB(0, 0, 0)); hOldPen = (HPEN)SelectObject(hDC, hPen); Rectangle(hDC, left, top, left + width, top + height); SelectObject(hDC, hOldBrush); DeleteObject(hBrush); SelectObject(hDC, hOldPen); DeleteObject(hPen); } L_VOID DrawColorPalette( HDC hDC, HPALETTE hPal, LPCOLORLAYOUT colorlayout, LPRGBQUAD pal, L_INT nIndex ) { L_INT row, col; L_INT index; RECT rect; HPALETTE hOldPalette; hOldPalette = SelectPalette( hDC, hPal, FALSE ); RealizePalette( hDC ); for( row = 0; row < colorlayout->nRows; row++ ) for( col = 0; col < colorlayout->nCols; col++ ) { index = row * colorlayout->nCols + col; if( index >= colorlayout->nTotalColors ) break; DrawColoredRectangle( hDC, colorlayout->nOffsetX + col *(colorlayout->nBoxWidth+colorlayout->nDeltaWidth), colorlayout->nOffsetY + row *(colorlayout->nBoxHeight+colorlayout->nDeltaHeight), colorlayout->nBoxWidth, colorlayout->nBoxHeight, PALETTEINDEX(index) ); } SelectPalette( hDC, hOldPalette, TRUE ); RealizePalette( hDC ); row = nIndex / colorlayout->nCols; col = nIndex % colorlayout->nCols; SetRect( &rect, 1, 1, colorlayout->nBoxWidth - 1, colorlayout->nBoxHeight - 1 ); OffsetRect( &rect, colorlayout->nOffsetX + col *(colorlayout->nBoxWidth+colorlayout->nDeltaWidth), colorlayout->nOffsetY + row *(colorlayout->nBoxHeight+colorlayout->nDeltaHeight) ); DrawFocusRect( hDC, &rect ); } L_BOOL CALLBACK L_EXPORT BitmapColorsDlgProc (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { LPDRAWITEMSTRUCT lpDraw; L_INT deltaX, deltaY; L_INT i; switch (message) { case WM_INITDIALOG: pParm = (LPCOLORPARM) lParam; SetCurrentColor(hDlg, pParm->palColors, pParm->nColorIndex); nIndex = pParm->nColorIndex; /* get the number of rows and columns with colored boxes, the width and height of a box and the width and height of the space between boxes */ GenerateColorLayout(hDlg, &colorlayout, pParm->nColors, &deltaX, &deltaY); if( deltaY ) { ResizeDlgItem( hDlg, IDC_COLORARRAY, 0, deltaY ); ResizeDlgItem( hDlg, IDC_COLORARRAYFRAME, 0, deltaY ); for( i = IDC_FIRSTCOLORITEM; i <= IDC_LASTCOLORITEM; i++ ) OffsetDlgItem( hDlg, i, 0, deltaY + 5); OffsetDlgItem( hDlg, IDOK, 0, deltaY + 5); OffsetDlgItem( hDlg, IDCANCEL, 0, deltaY + 5); /* specify 0 to resize the dialog box */ ResizeDlgItem( hDlg, 0, 0, deltaY ); } /* subclass the button so that we can intercept the clicking */ pfnDefaultButtonProc = (WNDPROC)SetWindowLong(GetDlgItem(hDlg,IDC_COLORARRAY), GWL_WNDPROC, (LONG)ColorArrayWindowProc); return (TRUE); case WM_DRAWITEM: lpDraw = (LPDRAWITEMSTRUCT)lParam; if( wParam == IDC_CURRENTCOLOR && lpDraw->itemAction == ODA_DRAWENTIRE ) { HPALETTE hOldPalette = SelectPalette(lpDraw->hDC,pParm->hPal,FALSE); RealizePalette(lpDraw->hDC); DrawColoredRectangle( lpDraw->hDC, 0, 0, (lpDraw->rcItem.right - lpDraw->rcItem.left), (lpDraw->rcItem.bottom - lpDraw->rcItem.top), PALETTEINDEX(nIndex) ); SelectPalette(lpDraw->hDC,hOldPalette,TRUE); } else if( wParam == IDC_COLORARRAY && lpDraw->itemAction == ODA_DRAWENTIRE ) DrawColorPalette( lpDraw->hDC, pParm->hPal, &colorlayout, pParm->palColors, nIndex ); break; case WM_COMMAND: switch (CTLID(wParam, lParam)) { case IDOK: pParm->nColorIndex = nIndex; SetWindowLong(GetDlgItem(hDlg,IDC_COLORARRAY), GWL_WNDPROC, (LONG)pfnDefaultButtonProc); EndDialog (hDlg, wParam); return (TRUE); case IDCANCEL: SetWindowLong(GetDlgItem(hDlg,IDC_COLORARRAY), GWL_WNDPROC, (LONG)pfnDefaultButtonProc); EndDialog (hDlg, wParam); return (TRUE); } } return (FALSE); } L_INT GetColorIndex( LPCOLORLAYOUT colorlayout, L_INT x, L_INT y ) { L_INT row, col; x -= colorlayout->nOffsetX; y -= colorlayout->nOffsetY; if( x < 0 || y < 0 ) return -1; col = x / (colorlayout->nBoxWidth + colorlayout->nDeltaWidth); row = y / (colorlayout->nBoxHeight + colorlayout->nDeltaHeight); x %= colorlayout->nBoxWidth + colorlayout->nDeltaWidth; y %= colorlayout->nBoxHeight + colorlayout->nDeltaHeight; if( x >= colorlayout->nBoxWidth || y >= colorlayout->nBoxHeight ) return -1; return row * colorlayout->nCols + col; } L_BOOL CALLBACK L_EXPORT FrameSettingsDlgProc (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { static LPFRAMESETTINGSPARM pParm; BOOL fTranslated; L_INT nLeft=0; L_INT nTop =0; L_TCHAR szText[256]; COLORREF crColor; HWND hWnd = GetParent(hDlg); HDC hdc = GetDC(hWnd); switch (message) { case WM_INITDIALOG: pParm = (LPFRAMESETTINGSPARM) lParam; CheckDlgButton (hDlg, IDC_ANIFRAMECOLOR, pParm->fTransparency); CheckDlgButton (hDlg, IDC_ANIFRAMEINPUT, pParm->fWaitUserInput); wsprintf(szText, TEXT("%lu"), (L_UINT32) pParm->uDelay); SetDlgItemText(hDlg, IDC_ANIFRAMEDELAY, szText); wsprintf(szText, TEXT("0x%.8lX"), (L_UINT32) pParm->crTransparency); SetDlgItemText(hDlg, IDC_ANIFRAMECOLORBOX, szText); SendDlgItemMessage(hDlg, IDC_ANIFRAMEDISPOSAL, CB_ADDSTRING, 0, (LPARAM) (LPTSTR) TEXT("None")); SendDlgItemMessage(hDlg, IDC_ANIFRAMEDISPOSAL, CB_ADDSTRING, 0, (LPARAM) (LPTSTR) TEXT("Leave")); SendDlgItemMessage(hDlg, IDC_ANIFRAMEDISPOSAL, CB_ADDSTRING, 0, (LPARAM) (LPTSTR) TEXT("Restore Background")); SendDlgItemMessage(hDlg, IDC_ANIFRAMEDISPOSAL, CB_ADDSTRING, 0, (LPARAM) (LPTSTR) TEXT("Restore Previous")); SendDlgItemMessage(hDlg, IDC_ANIFRAMEDISPOSAL, CB_SETCURSEL, (WPARAM) pParm->uDisposalMethod, 0); SetDlgItemInt(hDlg, IDC_ANIFRAMELEFT, pParm->nLeft, TRUE); SetDlgItemInt(hDlg, IDC_ANIFRAMETOP, pParm->nTop, TRUE); CheckDlgButton (hDlg, IDC_ANIFRAMEALL, pParm->fApplytoall); return (TRUE); case WM_COMMAND: switch (CTLID(wParam, lParam)) { case IDC_CHOOSECOLOR: GetColor(hWnd, &Data.BitmapHandle, &crColor); break; case IDOK: nLeft = (L_INT) GetDlgItemInt (hDlg, IDC_ANIFRAMELEFT, &fTranslated, TRUE); if(!fTranslated) { MessageBeep (-1); SetFocus (GetDlgItem (hDlg, IDC_ANIFRAMELEFT)); return(FALSE); } nTop = (L_INT) GetDlgItemInt (hDlg, IDC_ANIFRAMETOP, &fTranslated, TRUE); if(!fTranslated) { MessageBeep (0); SetFocus (GetDlgItem (hDlg, IDC_ANIFRAMETOP)); return(FALSE); } GetDlgItemText(hDlg, IDC_ANIFRAMEDELAY, szText, sizeof(szText)); pParm->uDelay = _tcstoul(szText, NULL, 10); GetDlgItemText(hDlg, IDC_ANIFRAMECOLORBOX, szText, sizeof(szText)); pParm->crTransparency = _tcstoul(szText, NULL, 0); pParm->nLeft = nLeft; pParm->nTop = nTop; pParm->fTransparency = IsDlgButtonChecked (hDlg, IDC_ANIFRAMECOLOR); pParm->fWaitUserInput = IsDlgButtonChecked (hDlg, IDC_ANIFRAMEINPUT); pParm->fApplytoall = IsDlgButtonChecked (hDlg, IDC_ANIFRAMEALL); pParm->uDelay = (L_UINT32) GetDlgItemInt (hDlg, IDC_ANIFRAMEDELAY, NULL, FALSE); pParm->uDisposalMethod = (L_UINT) SendDlgItemMessage(hDlg, IDC_ANIFRAMEDISPOSAL, CB_GETCURSEL, 0, 0); EndDialog (hDlg, wParam); return (TRUE); case IDCANCEL: EndDialog (hDlg, wParam); return (TRUE); } } return (FALSE); } L_BOOL GetFrameSettingsParm(HWND hWnd, LPFRAMESETTINGSPARM pParm) { L_INT nRet; nRet = DoDialogBoxParam (IDD_ANIFRAME, hWnd, (DLGPROC) FrameSettingsDlgProc, (LPARAM) pParm); return ((nRet == IDOK) ? TRUE: FALSE); } VOID EnableMenuItems(HWND hWnd, HMENU hMenu) { L_BOOL bFrame; UINT uEnable = MF_BYCOMMAND; HMENU hSysMenu= GetSystemMenu(hWnd, FALSE); if (Data.hList) L_GetBitmapListCount(Data.hList, &Data.uLastFrame); EnableMenuItem(hSysMenu, SC_CLOSE, MF_BYCOMMAND | ((Data.hList) ? bPlay : MF_ENABLED)); uEnable |= (Data.hList) ? bPlay : MF_GRAYED; EnableMenuItem(hMenu, IDM_ANIFRAMESETTINGS, uEnable); EnableMenuItem(hMenu, IDM_ANIBACKCOLOR, uEnable); EnableMenuItem(hMenu, IDM_ANICOLORRES, uEnable); EnableMenuItem(hMenu, IDM_ANILOOP, uEnable); EnableMenuItem(hMenu, IDM_PLAYANI, uEnable); EnableMenuItem(hMenu, IDM_EXIT, uEnable); EnableMenuItem(hMenu, IDM_HELP_ABOUT, MF_BYCOMMAND | ((bPlay) ? MF_GRAYED : MF_ENABLED)); bFrame = ((Data.hList) && (Data.nBitmap > 0) && (Data.nBitmap <= (Data.uLastFrame - 1))); uEnable |= (bPlay) ? MF_GRAYED : ((bFrame) ? MF_ENABLED : MF_GRAYED); EnableMenuItem(hMenu, IDM_ANIPREVFRAME, uEnable); EnableMenuItem(hMenu, IDM_ANIFIRSTFRAME, uEnable); uEnable = MF_BYCOMMAND; bFrame = ((Data.hList) && (Data.nBitmap >= 0) && (Data.nBitmap < (Data.uLastFrame - 1))); uEnable |= (bPlay) ? MF_GRAYED : ((bFrame) ? MF_ENABLED : MF_GRAYED); EnableMenuItem(hMenu, IDM_ANINEXTFRAME, uEnable); EnableMenuItem(hMenu, IDM_ANILASTFRAME, uEnable); EnableMenuItem(hMenu, ID_ANIMATION_STOPPLAY, MF_BYCOMMAND | ((Data.hList && bPlay) ? MF_ENABLED: MF_GRAYED)); } L_VOID Window_OnSize (HWND hWnd, L_UINT nState, L_INT nCx, L_INT nCy) { InvalidateRect(hWnd,NULL,TRUE); } L_VOID Window_OnClose(HWND hWnd) { if(!bPlay) PostQuitMessage (0); return; } /*---[ExtractCommandData]---------------------------------------------------- Syntax: L_BOOL ExtractCommandData( ); Parameters: None. ProtoType: AnimView.c Notes: - This procedure is responsible for extracting the file name passed through the command line. - You should call this function only in WinMain function. --------------------------------------------------------------------------*/ L_BOOL ExtractCommandData ( ) { LPTSTR pszFirst = NULL; LPTSTR pszCmdLine = NULL; LPTSTR psz = NULL; L_INT nFirstPos = 0 ; L_INT nStringLen = 0 ; pszCmdLine = GetCommandLine(); nStringLen = lstrlen ( pszCmdLine ) + 1 ; // To specify that it is command line or not. if(( pszCmdLine[1] == ':' ) || // Shortcut case ( pszCmdLine[2] == ':' )) //VS case { // 1- TRY TO EXPOSE THE EXE NAME FROM THE COMMAND LINE psz = _tcschr ( pszCmdLine, ':' ) ; if ( NULL == psz ) { return FALSE ; } } else { psz = pszCmdLine ; } pszFirst = _tcschr ( psz + 1, ':' ) ; if ( NULL == pszFirst ) { return FALSE ; } // Calc the char number to the image file name. nFirstPos = pszFirst - pszCmdLine - 1 ; if ( 0 > nFirstPos ) { return FALSE ; } memset (Data.szFilename, 0, sizeof(Data.szFilename)); lstrcpyn ( Data.szFilename, pszFirst - 1, ( nStringLen - nFirstPos ) ) ; return TRUE; }