/*[]=====================================================================[]*/ /*[] LEADTOOLS for Windows - Version 11 []*/ /*[] []*/ /*[] []*/ /*[] Copyright (c) 1991-2000 LEAD Technologies, Inc. []*/ /*[] All Rights Reserved. []*/ /*[]=====================================================================[]*/ /*---(Memory)------------------------------------------------------------- LEAD Functions Used. L_FileInfoMemory L_InitBitmap L_LoadBitmapMemory L_CreatePaintPalette L_PaintDC L_FreeBitmap We have made the assumption that the user has the knowledge of programing in C and Windows. This example will: 1. load an image file (name passed as a command line parameter) into allocated memory, 2. load the image from memory to a bitmap with L_LoadBitmapMemory, 3. display the loaded image in a window. Usage: MEMORY --------------------------------------------------------------------------*/ #include /* Required for all Windows applications. */ #include /* Needed for message cracker. */ #include #include "TCHAR.H" #include "..\\..\\..\\include\\l_bitmap.h" /* LEADTOOLS main header file. */ #include "..\\..\\..\\include\\l_error.h" /* LEADTOOLS error definition header file. */ #include "Memory.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 */ if (!ExtractCommandData ()) { return FALSE; } if (!InitInstance (hInstance, nCmdShow)) /* Perform 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: Memory.h Notes: Initializes window class structure and registers window class. --------------------------------------------------------------------------*/ L_BOOL InitApplication (HANDLE hInstance) { WNDCLASS wcWindowClass; wcWindowClass.style = 0; /* 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 Application. */ wcWindowClass.hIcon = LoadIcon (hInstance, TEXT("LEAD")); wcWindowClass.hCursor = LoadCursor (NULL, IDC_ARROW); wcWindowClass.hbrBackground = GetStockObject (WHITE_BRUSH); wcWindowClass.lpszMenuName = NULL; /* No menu */ wcWindowClass.lpszClassName = TEXT("LEADWClass"); return (RegisterClass (&wcWindowClass)); /* Register the window class and */ /* return the result code. */ } /*---[InitInstance]---------------------------------------------------------- Syntax: L_BOOL InitInstance( HANDLE hInstance, L_INT nCmdShow ) Parameters: hInstance Current instance. nCmdShow Parameter for first ShowWindow() call. Prototype: Memory.h Notes: Saves instance handle and creates main window. --------------------------------------------------------------------------*/ L_BOOL InitInstance (HANDLE hInstance, L_INT nCmdShow) { HWND hWnd; hWnd = CreateWindow ( TEXT("LEADWClass"), TEXT("LEADTOOLS Sample Application"), /* Window title */ WS_OVERLAPPEDWINDOW, /* Window style. */ CW_USEDEFAULT, /* Default horizontal position. */ CW_USEDEFAULT, /* Default vertical position. */ CW_USEDEFAULT, /* Window width. */ CW_USEDEFAULT, /* 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 (hWnd == NULL) return (FALSE); /* If window could not be created, return "failure" */ ShowWindow (hWnd, nCmdShow); /* Show the window. */ UpdateWindow (hWnd); /* Sends 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: Memory.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_CREATE, Window_OnCreate); 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_OnCreate]----------------------------------------------------- Syntax: BOOL Window_OnCreate(HWND hWnd, CREATESTRUCT FAR* lpCreateStruct); Parameters: hWnd Window handle. lpCreateStruct Windows Create Structure. Prototype: Memory.h Notes: This procedure is responsible for handling WM_CREATE. --------------------------------------------------------------------------*/ BOOL Window_OnCreate (HWND hWnd, CREATESTRUCT FAR * lpCreateStruct) { static LOADFILEOPTION LoadFileOption; L_INT32 lSizeToRead; L_INT32 lFileSize; L_UINT32 uNumOfBytesRead; L_TCHAR buf[1024]; RECT rWndSize; RECT rWndRect; HANDLE hFile; L_INT nRet; HANDLE hBuf; L_CHAR L_HUGE *pBuf; L_CHAR L_HUGE *pMoverBuf; UNREFERENCED_PARAMETER (lpCreateStruct); hFile = CreateFile ( Data.szFilename, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ); if(hFile== NULL || hFile == INVALID_HANDLE_VALUE) { MessageBox (hWnd, TEXT("Error Opening File!"), TEXT("Error"), MB_OK); return (FALSE); } // Try to obtain hFile's size lFileSize = SetFilePointer(hFile, 0, NULL, FILE_END ); SetFilePointer(hFile, 0, NULL, FILE_BEGIN ); /* Allocate a buffer to hold the file. */ if ((hBuf = GlobalAlloc (GMEM_MOVEABLE, lFileSize)) == NULL) { MessageBox (hWnd, TEXT("Not enough memory!"), TEXT("Error"), MB_OK); CloseHandle (hFile); return (FALSE); } pBuf = (L_CHAR L_HUGE *) GlobalLock (hBuf); pMoverBuf = pBuf; lSizeToRead = 0; /* Read the file in a loop, because the file could be larger than a single read can request. */ while (lSizeToRead < lFileSize) { if ( !ReadFile(hFile, pMoverBuf, min (lFileSize - lSizeToRead, 32000), &uNumOfBytesRead, NULL)) { MessageBox (hWnd, TEXT("Error reading file!"), TEXT("Error"), MB_OK); GlobalUnlock (hBuf); GlobalFree (hBuf); CloseHandle (hFile); return (FALSE); /* Failure on creation! */ } else { lSizeToRead += uNumOfBytesRead; pMoverBuf += uNumOfBytesRead; } } CloseHandle (hFile); /* Get information about the file in memory. */ Data.FileInfo.uStructSize = sizeof(FILEINFO); nRet = L_FileInfoMemory (pBuf, &Data.FileInfo, sizeof(FILEINFO), lFileSize, 0, NULL); GetWindowRect (hWnd, &rWndSize); /* Set up RECT for sizing the window to the image dimensions. */ rWndRect.left = 0; rWndRect.top = 0; rWndRect.right = INFOWIDTH(&Data.FileInfo); rWndRect.bottom = INFOHEIGHT(&Data.FileInfo); /* Create RECT to size of image plus window frame. */ AdjustWindowRect (&rWndRect, WS_OVERLAPPEDWINDOW, FALSE); /* Change the window size to match the image. */ MoveWindow (hWnd, rWndSize.left, rWndSize.top, (rWndRect.right - rWndRect.left), (rWndRect.bottom - rWndRect.top), TRUE); if (nRet == SUCCESS) { /* Init. the bitmap to the image width, height, and bits per pixel. */ L_InitBitmap (&Data.BitmapHandle, sizeof(BITMAPHANDLE), Data.FileInfo.Width, Data.FileInfo.Height, Data.FileInfo.BitsPerPixel); L_GetDefaultLoadFileOption(&LoadFileOption, sizeof(LOADFILEOPTION)); LoadFileOption.Flags |= ELO_ROTATED; /* Load the bitmap, using the image bits per pixel. */ nRet = L_LoadBitmapMemory (pBuf, &Data.BitmapHandle, sizeof(BITMAPHANDLE), Data.FileInfo.BitsPerPixel, ORDER_BGR, lFileSize, &LoadFileOption, &Data.FileInfo); if (nRet != SUCCESS) { wsprintf (buf, TEXT("Error %d loading %s."), nRet, (LPTSTR) Data.szFilename); MessageBox (hWnd, buf, TEXT("Error"), MB_OK); nRet = FALSE; } else /* Tell the window to get a new palette for painting. */ FORWARD_WM_QUERYNEWPALETTE (hWnd, SendMessage); } else { wsprintf (buf, TEXT("L_FileInfoMemory returned error %d."), nRet); MessageBox (hWnd, buf, TEXT("Error"), MB_OK); nRet = FALSE; } GlobalUnlock (hBuf); GlobalFree (hBuf); return (nRet); /* SUCCESS (TRUE) if creation is OK, */ /* FALSE if creation is failure. */ } /*---[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_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: Memory.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.BitmapHandle.Flags.Allocated) { hDC = GetDC (hWnd); /* Generate a new logical palette (if needed) for painting. */ Data.hPalette = L_CreatePaintPalette (hDC, &Data.BitmapHandle); /* Select and Realize the palette. */ hPalette = SelectPalette (hDC, Data.hPalette, TRUE); RealizePalette (hDC); /* Force a repaint. */ InvalidateRect (hWnd, NULL, FALSE); /* Return the old palette. */ SelectPalette (hDC, hPalette, TRUE); ReleaseDC (hWnd, hDC); } 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: Memory.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.BitmapHandle.Flags.Allocated) { hDC = GetDC (hWnd); /* Generate a new logical palette (if needed) for painting. */ Data.hPalette = L_CreatePaintPalette (hDC, &Data.BitmapHandle); if (Data.hPalette) /* Is a palette needed? */ { hPalette = SelectPalette (hDC, Data.hPalette, FALSE); nNoColors = RealizePalette (hDC); if (nNoColors) /* If the palette changed, force a WM_PAINT */ InvalidateRect (hWnd, NULL, FALSE); /* Restore the old palette. */ SelectPalette (hDC, hPalette, TRUE); } ReleaseDC (hWnd, hDC); } return (nNoColors); } /*---[Window_OnPaint]------------------------------------------------------- Syntax: VOID Window_OnPaint( HWND hWnd ); Parameters: hWnd Window handle. Prototype: Memory.h Notes: This procedure is responsible for handling WM_PAINT. --------------------------------------------------------------------------*/ VOID Window_OnPaint (HWND hWnd) { HDC hdc; RECT rc; PAINTSTRUCT ps; HPALETTE hOldPal = NULL; hdc = BeginPaint (hWnd, &ps);/* Get DC */ if (Data.BitmapHandle.Flags.Allocated) /* Do we have an image? */ { if (Data.hPalette) /* Is there a palette that needs to be selected? */ hOldPal = SelectPalette (hdc, Data.hPalette, TRUE); /* Select it. */ /* Setup the Destination Rectangle. */ SetRect (&rc, 0, 0, BITMAPWIDTH(&Data.BitmapHandle), BITMAPHEIGHT(&Data.BitmapHandle)); /* Paint it */ L_PaintDC (hdc, &Data.BitmapHandle, NULL, NULL, &rc, &ps.rcPaint, SRCCOPY); if (Data.hPalette) /* Return old palette if there is one. */ SelectPalette (hdc, hOldPal, TRUE); } EndPaint (hWnd, &ps); /* Return DC */ return; } /*---[Window_OnDestroy]---------------------------------------------------- Syntax: VOID Window_OnDestroy( HWND hWnd ); Parameters: hWnd Window handle. Prototype: Memory.h Notes: This procedure is responsible for handling WM_DESTROY. --------------------------------------------------------------------------*/ VOID Window_OnDestroy (HWND hWnd) { UNREFERENCED_PARAMETER (hWnd); if (Data.BitmapHandle.Flags.Allocated) /* Do we have an image? */ L_FreeBitmap (&Data.BitmapHandle); if (Data.hPalette) /* Delete palette if there is one. */ DeleteObject (Data.hPalette); PostQuitMessage (0); /* Post WM_QUIT, to end the application. */ return; } /*---[ExtractCommandData]---------------------------------------------------- Syntax: L_BOOL ExtractCommandData( ); Parameters: None. ProtoType: Memory.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; }