/*[]=====================================================================[]*/ /*[] LEADTOOLS for Windows(Class Library) - Version 11 []*/ /*[] []*/ /*[] []*/ /*[] Copyright (c) 1991-2000 LEAD Technologies, Inc. []*/ /*[] All Rights Reserved. []*/ /*[]=====================================================================[]*/ /*---(MemoryCB)------------------------------------------------------------ We have made the assumption that the user has the knowledge of Object Oriented programing of C under 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 LMemoryFile::LoadMemory() and a Callback function, painting while loading, 3. display the loaded image in a window. Usage: MEMORYCB --------------------------------------------------------------------------*/ // LTLMCbVw.cpp : implementation of the CLTLMemCbView class // #include "stdafx.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif #define WM_PAINTWHILELOAD WM_USER + 0 ///////////////////////////////////////////////////////////////////////////// // CLTLMemCbView IMPLEMENT_DYNCREATE(CLTLMemCbView, CView) BEGIN_MESSAGE_MAP(CLTLMemCbView, CView) //{{AFX_MSG_MAP(CLTLMemCbView) ON_WM_SIZE() ON_WM_CREATE() //}}AFX_MSG_MAP ON_MESSAGE(WM_PAINTWHILELOAD,PaintwhileLoad) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CLTLMemCbView construction/destruction CLTLMemCbView::CLTLMemCbView() { } CLTLMemCbView::~CLTLMemCbView() { } BOOL CLTLMemCbView::PreCreateWindow(CREATESTRUCT& cs) { // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return CView::PreCreateWindow(cs); } ///////////////////////////////////////////////////////////////////////////// // CLTLMemCbView drawing void CLTLMemCbView::OnDraw(CDC* pDC) { HPALETTE oldPalette = NULL; CLTLMemCbDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); if(m_LUserBitmap.GetPalette()) { oldPalette = SelectPalette(pDC->m_hDC, m_LUserBitmap.GetPalette(), TRUE); RealizePalette(pDC->m_hDC); } // Parepare to paint m_LUserBitmap.Paint()->SetDC(pDC->m_hDC); m_LUserBitmap.Paint()->PaintDC(); if(oldPalette) SelectPalette(pDC->m_hDC, oldPalette, TRUE); } ///////////////////////////////////////////////////////////////////////////// // CLTLMemCbView diagnostics #ifdef _DEBUG void CLTLMemCbView::AssertValid() const { CView::AssertValid(); } void CLTLMemCbView::Dump(CDumpContext& dc) const { CView::Dump(dc); } CLTLMemCbDoc* CLTLMemCbView::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CLTLMemCbDoc))); return (CLTLMemCbDoc*)m_pDocument; } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CLTLMemCbView message handlers void CLTLMemCbView::OnSize(UINT nType, int cx, int cy) { CView::OnSize(nType, cx, cy); RECT rcClient; GetClientRect(&rcClient); m_LUserBitmap.SetDstRect(&rcClient); m_LUserBitmap.SetClipDstRect(&rcClient); } LRESULT CLTLMemCbView::PaintwhileLoad(WPARAM wParam, LPARAM lParam) { LUserMemoryFile *pUserMemFile = (LUserMemoryFile *)m_LUserBitmap.MemoryFile(); pUserMemFile->pView = this; pUserMemFile->EnableCallBack(TRUE); if(LoadFileIntoBuffer() != SUCCESS) return FAILURE; m_LUserBitmap.MemoryFile()->GetInfo(m_LeadBuffer, &m_fi,0,NULL); if(m_LUserBitmap.MemoryFile()->LoadMemory(m_LeadBuffer,m_fi.BitsPerPixel, m_fi.Order,LOADFILE_ALLOCATE |LOADFILE_STORE) != SUCCESS) { LBase::DisplayErrorFromList(GetSafeHwnd()); return FAILURE; } ValidateRect (NULL); return SUCCESS; } int CLTLMemCbView::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CView::OnCreate(lpCreateStruct) == -1) return -1; PostMessage(WM_PAINTWHILELOAD, 0, 0); return 0; } L_INT CLTLMemCbView::LoadFileIntoBuffer() { CFile ImageFile; CLTLMemCbApp* pTheApp = (CLTLMemCbApp* )AfxGetApp(); L_INT nRetCode; L_TCHAR szErrMsg[256] = _T(""); if(pTheApp->m_strCmdLine == "\0") { LBase::DisplayError(GetSafeHwnd(), _T("No file to be opened.")); //AfxGetMainWnd()->DestroyWindow(); return 0; } // Open file with read only mode if(!ImageFile.Open(pTheApp->m_strCmdLine, CFile::modeRead)) { wsprintf(szErrMsg, _T("Can't open file: %s"),pTheApp->m_strCmdLine); LBase::DisplayError(GetSafeHwnd(), szErrMsg); return -1; } // Get the file size to Allocate the buffer L_INT32 lFileSize = ImageFile.GetLength(); if((nRetCode=m_LeadBuffer.Reallocate(lFileSize)) != SUCCESS) { ImageFile.Close(); LBase::DisplayError(GetSafeHwnd(),nRetCode); return -1; } L_INT32 lSizeToRead = 0; L_INT32 lActualRead = 0; // The actual size of read chunk L_UCHAR *pMoveBuffer = NULL; if((pMoveBuffer = (L_UCHAR *)m_LeadBuffer.Lock()) == NULL) { LBase::DisplayErrorFromList(GetSafeHwnd()); return -1; } // Read the file not as one chunk but breaked into 32k while(lSizeToRead < lFileSize) { TRY { lActualRead = ImageFile.Read(pMoveBuffer, min(lFileSize - lSizeToRead, 32000)); lSizeToRead += lActualRead; pMoveBuffer += lActualRead; // Advance the pointer buffer } CATCH(CFileException, e) { ImageFile.Close(); wsprintf(szErrMsg, _T("Error while reading file: %s"), pTheApp->m_strCmdLine); LBase::DisplayError(GetSafeHwnd(), szErrMsg); } END_CATCH } m_LeadBuffer.Unlock(); ImageFile.Close(); return SUCCESS; } void CLTLMemCbView::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView) { if(bActivate && pActivateView == this) HandlePalette(WM_QUERYNEWPALETTE,m_hWnd);; CView::OnActivateView(bActivate, pActivateView, pDeactiveView); } L_BOOL CLTLMemCbView::HandlePalette(UINT uMsg,HWND hWnd) { HDC hDC; switch(uMsg) { case WM_PALETTECHANGED: if(m_hWnd== hWnd) return TRUE; case WM_SYSCOLORCHANGE: case WM_QUERYNEWPALETTE: hDC = ::GetDC(m_hWnd); HPALETTE hPalette = m_LUserBitmap.CreatePaintPalette(hDC); L_UINT uColor=0; if(hPalette) { HPALETTE oldPalette = ::SelectPalette(hDC,hPalette,(uMsg==WM_PALETTECHANGED)); uColor=::RealizePalette(hDC); ::SelectPalette(hDC,oldPalette,TRUE); } ::ReleaseDC(m_hWnd,hDC); if(uColor!=0) { InvalidateRect(NULL,false); UpdateWindow(); return TRUE; } break; } return FALSE; }