/*[]=====================================================================[]*/ /*[] LEADTOOLS for Windows - []*/ /*[] []*/ /*[] []*/ /*[] Copyright (c) 1991-2000 LEAD Technologies, Inc. []*/ /*[] All Rights Reserved. []*/ /*[]=====================================================================[]*/ /*---(Painteff)------------------------------------------------------------ LEAD Functions Used. L_CreatePaintPalette L_EfxPaintBitmap L_FreeBitmap L_FileInfo L_InitBitmap L_LoadFile We have made the assumption that the user has the knowledge of programing in C and Windows. This example is a menu driven program that can load a an image from a file and display the image using any of the display effects supported in the LEADTOOLS DLL. Usage: PAINTEFF --------------------------------------------------------------------------*/ #include /* Needed for message crackers. */ #include /* Required for all Windows applications. */ #include #include #include #include #include "tchar.h" #include "..\\..\\..\\include\\l_bitmap.h" /* LEADTOOLS main header file. */ #include "..\\..\\..\\include\\l_error.h" /* LEADTOOLS error definition header file. */ #include "Painteff.h" /* Application specific header file. */ L_BOOL ExtractCommandData ( ); /*---[WinMain]--------------------------------------------------------------- Syntax: int PASCAL WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR 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; /* Message structure, See Windows SDK for more information. */ UNLOCKSUPPORT(); if (!hPrevInstance) /* Other instances of app running? */ if (!InitApplication (hInstance)) /* Initialize shared things. */ return (FALSE); /* Exits if unable to initialize. */ ExtractCommandData ( ); if (!InitInstance (hInstance, nCmdShow)) /* Do instance initializations. */ return (FALSE); /* Acquire and dispatch messages until a WM_QUIT message is received. */ while (GetMessage (&msg, NULL, 0, 0)) { TranslateMessage (&msg); /* Translates virtual key codes. */ DispatchMessage (&msg); /* Dispatches message to window. */ } return (msg.wParam); /* Returns the value from PostQuitMessage. */ } /*---[InitApplication]------------------------------------------------------ Syntax: L_BOOL InitApplication( HANDLE hInstance ) Parameters: hInstance Current instance. Prototype: Painteff.h Notes: Initializes window class structure and registers window class. --------------------------------------------------------------------------*/ L_BOOL InitApplication (HANDLE hInstance) { WNDCLASS wcWindowClass; wcWindowClass.style = CS_HREDRAW | CS_VREDRAW; /* Class style(s). */ wcWindowClass.lpfnWndProc = MainWndProc; /* Function to retrieve messages */ /* for windows of this class. */ wcWindowClass.cbClsExtra = 0;/* No per-class extra data. */ wcWindowClass.cbWndExtra = 0;/* No per-window extra data. */ wcWindowClass.hInstance = hInstance; /* Owner. */ wcWindowClass.hIcon = LoadIcon (hInstance, TEXT("LEAD")); wcWindowClass.hCursor = LoadCursor (NULL, IDC_ARROW); wcWindowClass.hbrBackground = GetStockObject (BLACK_BRUSH); wcWindowClass.lpszMenuName = TEXT("MAIN_MENU"); /* Menu. */ wcWindowClass.lpszClassName = TEXT("LEADDLL Class"); /* Name. */ /* Register the window class and return the result code. */ return (RegisterClass (&wcWindowClass)); } /*---[InitInstance]---------------------------------------------------------- Syntax: L_BOOL InitInstance( HANDLE hInstance, L_INT nCmdShow ) Parameters: hInstance Current instance. nCmdShow Param for first ShowWindow() call. Prototype: Painteff.h Notes: Saves instance handle and creates main window. --------------------------------------------------------------------------*/ L_BOOL InitInstance (HANDLE hInstance, L_INT nCmdShow) { HDC hDC; HMENU hMainMenu; L_INT i; /* Create a main window for this application instance. */ hMainWnd = CreateWindow ( TEXT("LEADDLL Class"), TEXT("LEADTOOLS Paint Effect Demo"), /* Window title */ WS_OVERLAPPEDWINDOW, /* Window style. */ CW_USEDEFAULT, /* Default horizontal position. */ CW_USEDEFAULT, /* Default vertical position. */ 710,/* Window width. */ 400,/* Window height. */ NULL, /* Overlapped windows have no parent. */ NULL, /* Use the window class menu. */ hInstance, /* This instance owns this window. */ NULL/* Pointer not needed. */ ); if (hMainWnd == NULL) return (FALSE); /* If window could not be created, return "failure". */ nOpenTypeIdx = 1; /* Default file type index */ hMainMenu = GetMenu (hMainWnd); for (i = 0; i < 15; i++) { EnableMenuItem (hMainMenu, i+1, MF_BYPOSITION | MF_GRAYED); } /* Compute Device Bits -- Device color mode */ hDC = GetDC (hMainWnd); Data.nBitsPerPixel = GetDeviceCaps (hDC, BITSPIXEL) * GetDeviceCaps (hDC, PLANES); /* If Device bits is > 8, set device bits to 24 */ if (Data.nBitsPerPixel > 8) Data.nBitsPerPixel = 24; ReleaseDC (hMainWnd, hDC); ShowWindow (hMainWnd, nCmdShow); /* Show the window. */ UpdateWindow (hMainWnd); /* Send WM_PAINT message. */ return (TRUE); } /*---[MainWndProc]----------------------------------------------------------- Syntax: L_INT32 EXT_FUNCTION MainWndProc( HWND hWnd, L_UINT message, WPARAM wParam, LPARAM lParam ) Parameters: hWnd Window handle. message Type of message. wParam Additional information. lParam Additional information. Prototype: Painteff.h Notes: This procedure is responsible for handling window messages. --------------------------------------------------------------------------*/ L_INT32 EXT_FUNCTION MainWndProc (HWND hWnd, L_UINT Message, WPARAM wParam, LPARAM lParam) { switch (Message) { HANDLE_MSG (hWnd, WM_INITMENUPOPUP, Window_OnInitMenuPopup); HANDLE_MSG (hWnd, WM_COMMAND, Window_OnCommand); HANDLE_MSG (hWnd, WM_PALETTECHANGED, Window_OnPaletteChanged); HANDLE_MSG (hWnd, WM_QUERYNEWPALETTE, Window_OnQueryNewPalette); 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); } return DefWindowProc (hWnd, Message, wParam, lParam); } /*----(Window_OnInitMenuPopup)------------------------------------------------- Description: This function handles the WM_INITMENUPOPUP message. Syntax : VOID Window_OnInitMenuPopup(HWND hwnd, HMENU hMenu, L_INT item, BOOL fSystemMenu) Prototype : Painteff.h Parameters : hWnd Handle to the window. hMenu Handle to the menu selected. item Menu item selected. fSystemMenu Is the menu the system menu? Return Value: None. --------------------------------------------------------------------------*/ VOID Window_OnInitMenuPopup (HWND hWnd, HMENU hMenu, L_INT item, BOOL fSystemMenu) { L_INT i; if (!fSystemMenu && GetSubMenu (GetMenu (hWnd), item) == hMenu) { switch (item) { case MENU_EFFECT_POS: case MENU_EFFECT_POS+1: case MENU_EFFECT_POS+2: case MENU_EFFECT_POS+3: case MENU_EFFECT_POS+4: case MENU_EFFECT_POS+5: case MENU_EFFECT_POS+6: case MENU_EFFECT_POS+7: case MENU_EFFECT_POS+8: case MENU_EFFECT_POS+9: case MENU_EFFECT_POS+10: case MENU_EFFECT_POS+11: case MENU_EFFECT_POS+12: case MENU_EFFECT_POS+13: case MENU_EFFECT_POS+14: case MENU_EFFECT_POS+15: for (i = EFX_EFFECT_WIPE_CLASS; i <= EFX_EFFECT_ELLIPSE_MAX; i++) { CHECKMENUITEM (hMenu, i, i == nEffect); } break; } } return; } /*---[Window_OnActivate]----------------------------------------------------- Syntax: VOID Window_OnActivate(HWND hwnd, UINT state, HWND hwndActDeact, BOOL fMinimized) Parameters: hWnd Window handle. state WA_ACTIVE | WA_CLICKACTIVE | WA_INACTIVE hwndActDeact the winodw habdle that deactivate fMinimized Window is minimized Prototype: Color.h Notes: This procedure is responsible for handling WM_ONCOMMAND. --------------------------------------------------------------------------*/ void Window_OnActivate(HWND hwnd, UINT state, HWND hwndActDeact, BOOL fMinimized) { if(state!=WA_INACTIVE) Window_OnQueryNewPalette (hwnd); } /*----(Window_OnCommand)--------------------------------------------------- Description: This function handles the WM_COMMAND message. Syntax : VOID Window_OnCommand (HWND hWnd, L_INT id, HWND hwndCtl, UINT codeNotify) Prototype : Painteff.h Parameters : hWnd Handle to the window. id Control or menu item identifier. hwndCtl Handle of control. codeNotify Notification message. Return Value: None. --------------------------------------------------------------------------*/ VOID Window_OnCommand (HWND hWnd, L_INT id, HWND hwndCtl, UINT codeNotify) { L_TCHAR szFileName[MAXFILENAME]; switch (id) { case IDM_OPEN: if (GetOpenBitmapFile (hWnd, (LPTSTR) szFileName)) { if (ImageLoad (hWnd, (LPTSTR) szFileName) == SUCCESS) InvalidateRect (hWnd, NULL, TRUE); else MessageBox (hWnd, TEXT("ImageLoad Error"), TEXT("ERROR"), MB_OK); } break; case IDM_EXIT: FORWARD_WM_CLOSE (hWnd, SendMessage); break; default: if ((id >= EFX_EFFECT_WIPE_CLASS) && (id <= EFX_EFFECT_ELLIPSE_MAX)) { nEffect = id; InvalidateRect (hWnd, NULL, TRUE); } else { DefFrameProc (hWnd, hMainWnd, WM_COMMAND, (WPARAM) (int) (id), MAKELPARAM ((UINT) (hwndCtl), (codeNotify))); } } return; } /*----(Window_OnPaletteChanged)-------------------------------------------- Syntax: VOID Window_OnPaletteChanged( HWND hWnd, HWND hWndPaletteChange ) Parameters: hwnd Handle to a window. hWndPaletteChange Handle to a window that has the palette realized. Prototype: Painteff.h Notes: This procedure is responsible for handling WM_PALETTECHANGED. --------------------------------------------------------------------------*/ VOID Window_OnPaletteChanged (HWND hWnd, HWND hWndPaletteChange) { HDC hDC; HPALETTE hPalette; /* If this window initiated the palette change, do nothing. */ if (hWnd == hWndPaletteChange) return; /* Delete the previous palette, if there is one. */ if (Data.hPalette) { DeleteObject (Data.hPalette); Data.hPalette = NULL; } /* Does this window have a bitmap and a palette? */ if (Data.Bitmap.Flags.Allocated) { hDC = GetDC (hWnd); /* Generate a new logical palette (if needed) for painting. */ Data.hPalette = L_CreatePaintPalette (hDC, &Data.Bitmap); /* Select and Realize the palette. */ hPalette = SelectPalette (hDC, Data.hPalette, TRUE); RealizePalette (hDC); /* Return the old palette. */ SelectPalette (hDC, hPalette, TRUE); ReleaseDC (hWnd, hDC); /* Force a repaint. */ InvalidateRect (hWnd, NULL, FALSE); } return; } /*====(Window_OnPaletteChanging)========================================== Description: Enumerates all child windows and asks them to realize their logical palettes beside the physical palette. Syntax : VOID Window_OnPaletteChanging(HWND hwnd, HWND hWndPaletteChange) Parameters : hwnd Handle to a window. hWndPaletteChange Handle to a window that has the palette realized. Return Value: None. ==========================================================================*/ VOID Window_OnPaletteChanging(HWND hWnd, HWND hWndPaletteChange) { Window_OnPaletteChanged (hWnd, hWndPaletteChange); } /*====(Window_SysColorChange)========================================== Syntax: VOID Window_SysColorChange( HWND hWnd ) Parameters: hWnd Handle to a window. Prototype: Loadsave.h Notes: This procedure is responsible for handling WM_SYSCOLORCHANGE. --------------------------------------------------------------------------*/ VOID Window_SysColorChange(HWND hwnd) { Window_OnQueryNewPalette (hwnd); } /*----(Window_OnQueryNewPalette)-------------------------------------------- Syntax: BOOL Window_OnQueryNewPalette( HWND hWnd ) Parameters: hWnd Handle to a window. Prototype: Painteff.h Notes: This procedure is responsible for handling WM_QUERYNEWPALETTE. --------------------------------------------------------------------------*/ BOOL Window_OnQueryNewPalette (HWND hWnd) { HDC hDC; HPALETTE hPalette; L_INT nNoColors = 0; /* Delete the previous palette, if there is one. */ if (Data.hPalette) { DeleteObject (Data.hPalette); Data.hPalette = NULL; } if (Data.Bitmap.Flags.Allocated) { hDC = GetDC (hWnd); /* Generate a new logical palette (if needed) for painting. */ Data.hPalette = L_CreatePaintPalette (hDC, &Data.Bitmap); if (Data.hPalette) /* Is a palette needed? */ { hPalette = SelectPalette (hDC, Data.hPalette, FALSE); nNoColors = RealizePalette (hDC); /* Restore the old palette. */ SelectPalette (hDC, hPalette, TRUE); } ReleaseDC (hWnd, hDC); if (nNoColors) /* If the palette changed, force a WM_PAINT */ InvalidateRect (hWnd, NULL, FALSE); } return (nNoColors); } /*----(Window_OnPaint)----------------------------------------------------- Description: This function handles the WM_PAINT message. Syntax : VOID Window_OnPaint (HWND hWnd) Prototype : Painteff.h Parameters : hWnd Handle to the window. Return Value: None. --------------------------------------------------------------------------*/ VOID Window_OnPaint (HWND hWnd) { RECT rc; PAINTSTRUCT ps; /* Paint structure data */ HPALETTE hPalette = 0; /* Palette Handle */ BeginPaint (hWnd, &ps); if (Data.Bitmap.Flags.Allocated) { if (Data.hPalette) hPalette = SelectPalette (ps.hdc, Data.hPalette, TRUE); /* Setup the Destination Rectangle. */ SetRect (&rc, 0, 0, Data.Bitmap.Width, Data.Bitmap.Height); L_EfxPaintBitmap (ps.hdc, /* handle to the target device context */ &Data.Bitmap, /* pointer to the bitmap handle */ NULL, /* pointer to the display source rectangle */ NULL, /* pointer to the display source clipping rectangle */ &rc, /* pointer to the display rectangle */ &ps.rcPaint, /* pointer to the display destination clipping rectangle */ nEffect, /* effect to apply when painting */ 5, /* graining size */ 0, /* delay between graining steps */ 0, 0, 1, /* pass number when using a pattern brush */ 1, /* maximum passes for a pattern brush */ FALSE, /* transparency paint flag */ 0, /* transparency paint color */ 3, /* wand width */ RGB(255,0,0), /* wand color */ SRCCOPY); /* Windows ROP code for display */ if (Data.hPalette) SelectPalette (ps.hdc, hPalette, TRUE); } EndPaint (hWnd, &ps); return; } /*----(Window_OnDestroy)--------------------------------------------------- Description: This function handles the WM_DESTROY message. Syntax : VOID Window_OnCommand (HWND hWnd) Prototype : Painteff.h Parameters : hWnd Handle to the window. Return Value: None. --------------------------------------------------------------------------*/ VOID Window_OnDestroy (HWND hWnd) { UNREFERENCED_PARAMETER (hWnd); if (Data.Bitmap.Flags.Allocated) /* Do we have an image? */ L_FreeBitmap (&Data.Bitmap); if (Data.hPalette) /* Delete palette if there is one. */ DeleteObject (Data.hPalette); PostQuitMessage (0); return; } /*----(GetOpenBitmapFile)------------------------------------------------- Syntax: L_BOOL GetOpenBitmapFile (HWND hWnd, LPSTR pFilename) Parameters: hWnd Handle to the window. pFilename Name of the file to open and get info from. Prototype: Painteff.h Notes: This function will open a file and get image info. from it. --------------------------------------------------------------------------*/ L_BOOL GetOpenBitmapFile (HWND hWnd, LPTSTR pFilename) { OPENFILENAME ofn; L_TCHAR szFileTitle[256]; L_TCHAR szFilter[1024]; L_INT nRet; L_INT i; static L_BOOL fInit = FALSE; *pFilename = '\0'; /* Image File Formats */ lstrcpy (szFilter, TEXT("ALL|*.*|") TEXT("LEAD|*.cmp|") TEXT("JPEG JFIF|*.JPG|") TEXT("JPEG JTIF|*.jtf|") TEXT("TGA|*.tga|") TEXT("TIF|*.tif|") TEXT("CALS 1|*.cal|Macintosh 1|*.mac|GEM image 1|*.img|MS Paint 1|*.msp|") TEXT("WordPerfect|*.wpg|") TEXT("SUN Raster|*.ras|") TEXT("Mac PICT 1|*.PCT|") TEXT("Windows BMP|*.bmp|") TEXT("OS/2 BMP|*.bmp|") TEXT("PCX|*.pcx|") TEXT("GIF|*.gif|") TEXT("WMF|*.wmf|") TEXT("EPS|*.eps|")); for (i = 0; szFilter[i] != '\0'; i++) { if (szFilter[i] == '|') szFilter[i] = '\0'; } _fmemset (&ofn, 0, sizeof (OPENFILENAME)); ofn.lStructSize = sizeof (OPENFILENAME); ofn.hwndOwner = hWnd; ofn.lpstrFilter = szFilter; ofn.nFilterIndex = nOpenTypeIdx; ofn.lpstrFile = pFilename; ofn.nMaxFile = MAXFILENAME; ofn.lpstrFileTitle = szFileTitle; ofn.nMaxFileTitle = sizeof (szFileTitle)/sizeof(L_TCHAR); if (!fInit) { ofn.lpstrInitialDir = Data.szImageDir; fInit = TRUE; } ofn.lpstrTitle = TEXT("Open Bitmap File"); ofn.Flags = (OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY); if (GetOpenFileName (&ofn)) { nOpenTypeIdx = (L_INT) ofn.nFilterIndex; Data.FileInfo.uStructSize = sizeof(FILEINFO); nRet = L_FileInfo (pFilename, &Data.FileInfo, sizeof(FILEINFO), 0, NULL); if (nRet != SUCCESS) MessageBox (hWnd, TEXT("File Error"), TEXT("Open Bitmap File"), MB_ICONEXCLAMATION | MB_OK); else return (TRUE); } return (FALSE); } /*----[ImageLoad]----------------------------------------------------------- Syntax: L_BOOL ImageLoad(HWND hWnd, LPSTR lpFilename) Parameters: hWnd Handle to the window. lpFilename Pointer to the filename of the file containing the image to be loaded. Prototype: Scrlzoom.h Notes: This function will load an image from a file. --------------------------------------------------------------------------*/ L_BOOL ImageLoad (HWND hWnd, LPTSTR lpFileName) { HMENU hMainMenu; HCURSOR hCursor; RECT rcClient; RECT rcWindow; L_INT nRet, i; L_INT nFlags = LOADFILE_ALLOCATE | LOADFILE_STORE; /* Reset to Default */ nEffect = EFX_EFFECT_WIPE_L_TO_R; /* Free Bitmap and hPalette if already loaded */ if (Data.Bitmap.Flags.Allocated) L_FreeBitmap (&Data.Bitmap); if (Data.hPalette) { DeleteObject (Data.hPalette); Data.hPalette = NULL; } /* Get Image file information and load it to FileInfo structure */ Data.FileInfo.uStructSize = sizeof(FILEINFO); if ((nRet = L_FileInfo (lpFileName, &Data.FileInfo, sizeof(FILEINFO), 0, NULL)) != SUCCESS) return (nRet); hCursor = SetCursor (LoadCursor (NULL, IDC_WAIT)); /* Change to WAIT */ /* Snap the window to image */ ShowWindow (hWnd, SW_NORMAL); GetClientRect (hWnd, &rcClient); GetWindowRect (hWnd, &rcWindow); MoveWindow (hWnd, rcWindow.left, rcWindow.top, RECTWIDTH (&rcWindow) + Data.FileInfo.Width - RECTWIDTH (&rcClient), RECTHEIGHT (&rcWindow) + Data.FileInfo.Height - RECTHEIGHT (&rcClient), TRUE); /* Initialize Bitmap */ L_InitBitmap (&Data.Bitmap, sizeof(BITMAPHANDLE), Data.FileInfo.Width, Data.FileInfo.Height, Data.nBitsPerPixel); if (Data.nBitsPerPixel <= 4) nFlags |= LOADFILE_FIXEDPALETTE; /* Load the file to the bitmap */ nRet = L_LoadFile (lpFileName, &Data.Bitmap, sizeof(BITMAPHANDLE), Data.nBitsPerPixel, ORDER_BGR, nFlags, NULL, NULL, NULL, NULL); if (nRet == SUCCESS) { /* The following is for speed. */ if (Data.Bitmap.ViewPerspective == TOP_LEFT) { L_FlipBitmap (&Data.Bitmap); Data.Bitmap.ViewPerspective = BOTTOM_LEFT; } hMainMenu = GetMenu (hMainWnd); for (i = 0; i < 15; i++) { EnableMenuItem (hMainMenu, i+1, MF_BYPOSITION | MF_ENABLED); } DrawMenuBar (hMainWnd); FORWARD_WM_QUERYNEWPALETTE (hWnd, SendMessage); } SetCursor (hCursor); return (nRet); } /*---[ExtractCommandData]---------------------------------------------------- Syntax: L_BOOL ExtractCommandData( ); Parameters: None. ProtoType: Painteff.c Notes: - This procedure is responsible for extracting the image Dir 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.szImageDir, 0, sizeof ( Data.szImageDir ) ); lstrcpyn ( Data.szImageDir, pszFirst - 1, ( nStringLen - nFirstPos ) ) ; return TRUE; }