⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 picbrowserdlg.cpp

📁 Microsoft WinCE 6.0 BSP FINAL release source code for use with the i.MX27ADS TO2 WCE600_FINAL_MX27_S
💻 CPP
字号:
/*
 *
 *  Copyright (C) 2004-2006, Freescale Semiconductor, Inc. All Rights Reserved.
 *  THIS SOURCE CODE, AND ITS USE AND DISTRIBUTION, IS SUBJECT TO THE TERMS
 *  AND CONDITIONS OF THE APPLICABLE LICENSE AGREEMENT
 *
 *  File:            APPS/CAMAPP/PicBrowserDlg.cpp
 *  Purpose:        This file implements the DialogProc for the PicBrowser Dialog.
 *  Notes:
 *
 */

/*********************************************************************
 INCLUDE FILES  
*********************************************************************/
#include <windows.h>
#include <commctrl.h>
#include <commdlg.h>

#include "CameraWindow.h"
#include "resource.h"

/*********************************************************************
 GLOBAL DEFINITIONS  
*********************************************************************/

/*********************************************************************
 GLOBAL OR STATIC VARIABLES  
*********************************************************************/

/*********************************************************************
 STATIC FUNCTION PROTOTYPES  
*********************************************************************/
/********************************************************************
 *
 *  FUNCTION:       PicBrowserDialogProc
 *
 *  DESCRIPTION:    This function handles the dialog messages for the CStatisticsDlg dialog box.
 *
 *  PARAMETERS:     HWND hWndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam
 *
 *  RETURNS:        TRUE if successfully, otherwise return FALSE
 *
 ********************************************************************/
BOOL CALLBACK PicBrowserDialogProc(HWND hWndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    static CPicBrowserDlg *pPicBrowserDlg = NULL;
    UINT index;
    PAINTSTRUCT ps;
    HDC hDC;
    
    switch (uMsg)
    {
        case WM_INITDIALOG:
            pPicBrowserDlg = new CPicBrowserDlg;

            if (NULL == pPicBrowserDlg || FALSE == pPicBrowserDlg->Init(hWndDlg))
            {
                DestroyWindow(hWndDlg);
            }
            index = pPicBrowserDlg->GetFileName();
            if (!pPicBrowserDlg->LoadPic(index))
                DestroyWindow(hWndDlg);
            return TRUE;
            
        case WM_COMMAND:
            switch (LOWORD(wParam))
            {
                case IDCANCEL:
                case IDOK:
                case ID_DLG_CLOSE:
                        DestroyWindow(hWndDlg);
                        return TRUE;
            }
            return FALSE;
            
        case WM_CLOSE:
                DestroyWindow(hWndDlg);
                return TRUE;

        case WM_DESTROY:
            if (NULL != pPicBrowserDlg)
            {
                RETAILMSG(CAMAPP_DEBUG_MSG, (TEXT("PicBrowserDialogProc: WM_DESTROY!\r\n")));
                    delete pPicBrowserDlg;
                    pPicBrowserDlg = NULL;
            }
                return TRUE;
            
        case PD_SHOW:
                if( pPicBrowserDlg )
                {
                    pPicBrowserDlg->Show(static_cast<int>(wParam));
                }
                return TRUE;

        case WM_PAINT:
            RETAILMSG(CAMAPP_DEBUG_MSG, (TEXT("PicBrowserDialogProc: WM_PAINT!\r\n")));
            HWND hLText = GetDlgItem(hWndDlg, IDC_STATIC_VIEW);
            if (!wParam)
            {
                 hDC = BeginPaint(hWndDlg, &ps);
            }
            else
            {
                 hDC = (HDC)wParam;
            }

            if (!wParam)
            {
                 EndPaint(hWndDlg, &ps);
            }
            if (!wParam)
            {
                 hDC = BeginPaint(hLText, &ps);
            }
            else
            {
                 hDC = (HDC)wParam;
            }

            FillRect(hDC, &(ps.rcPaint), (HBRUSH)GetStockObject(BLACK_BRUSH));
             pPicBrowserDlg->DrawPic(hDC);

            if (!wParam)
            {
                 EndPaint(hLText, &ps);
            }

            return TRUE;
    }

    return FALSE;
}

/*********************************************************************
 EXPORTED FUNCTIONS
*********************************************************************/
/********************************************************************
 *
 *  FUNCTION:       CPicBrowserDlg
 *
 *  DESCRIPTION:    Constructor of CPicBrowserDlg class
 *
 *  PARAMETERS:     None
 *
 *  RETURNS:        None
 *
 ********************************************************************/
CPicBrowserDlg::CPicBrowserDlg()
{
    m_hWnd = NULL;
}


/********************************************************************
 *
 *  FUNCTION:       ~CPicBrowserDlg
 *
 *  DESCRIPTION:    Destructor of CPicBrowserDlg class
 *
 *  PARAMETERS:     None
 *
 *  RETURNS:        None
 *
 ********************************************************************/
CPicBrowserDlg::~CPicBrowserDlg()
{
    Fini();
}


/********************************************************************
 *
 *  FUNCTION:       init
 *
 *  DESCRIPTION:    Initialize the CPicBrowserDlg class
 *
 *  PARAMETERS:     HWND hWnd
 *
 *  RETURNS:        TRUE if initialize successfully, otherwise return FALSE
 *
 ********************************************************************/
BOOL  CPicBrowserDlg::Init(HWND hWnd)
{
    m_hWnd = hWnd;
    m_hWndParent = ::GetParent(m_hWnd);
    
       return TRUE;
}

/********************************************************************
 *
 *  FUNCTION:       Fini
 *
 *  DESCRIPTION:    Release all Resources acquired by the CPicBrowserDlg class
 *
 *  PARAMETERS:     None
 *
 *  RETURNS:        TRUE if successfully, otherwise return FALSE
 *
 ********************************************************************/
 BOOL  CPicBrowserDlg::Fini()
{
    BOOL bResult = FALSE;

    if (NULL != m_hWnd)
    {
          DestroyWindow(m_hWnd);
          bResult = TRUE;
    }
    
    if (NULL != m_hWndParent)
    {
            PostMessage(m_hWndParent, PD_CLOSED, NULL, NULL);
            m_hWndParent = NULL;
    }

    return bResult;
}


/********************************************************************
 *
 *  FUNCTION:       Show
 *
 *  DESCRIPTION:    This function will cause the dialog to be shown or hidden.
 *
 *  PARAMETERS:     int iShowCmd
 *
 *  RETURNS:        TRUE if successfully, otherwise return FALSE
 *
 ********************************************************************/
BOOL CPicBrowserDlg::Show(int iShowCmd)
{
    BOOL  bResult = FALSE;

    if (NULL == m_hWnd)
    {
            return bResult;
    }

    if (FALSE == ShowWindow(m_hWnd, iShowCmd))
    {
            bResult = FALSE;
    }
    else
    {
            bResult = TRUE;
    }
    
    return bResult;
}

/********************************************************************
 *
 *  FUNCTION:       GetFileName
 *
 *  DESCRIPTION:    Get save file name and path, 
 *                   return file type, 1 = jpg, 2 = bmp, 0 = not support
 *
 *  PARAMETERS:     None
 *
 *  RETURNS:        Nonzero indicates that the user specified a file name and clicked the OK button
 *
 ********************************************************************/
UINT CPicBrowserDlg::GetFileName()
{
    OPENFILENAME ofn;
    UINT dwFilterIndex;
    WCHAR wcExt[3];
    INT i = 0;
    
    ZeroMemory((LPVOID)&ofn,sizeof(OPENFILENAME));
    m_wcFileName[0] = '\0';
    wcscpy(m_wcFileName,L"capture\0");

    ofn.lStructSize = sizeof(ofn);
    ofn.hwndOwner = m_hWnd;
    ofn.lpstrFilter = L"JPEG Files (*.jpg)\0*.jpg\0BMP Files (*.bmp)\0*.bmp\0\0";
    ofn.lpstrFile = m_wcFileName;
    ofn.nMaxFile = JPEG_FILE_NAME_MAX;
    ofn.lpstrDefExt = L"jpg";
    ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
    if (GetOpenFileName(&ofn) == 0)
        dwFilterIndex = 0;
    
    for (i=0; i<3; i++)
        wcExt[i] = m_wcFileName[ofn.nFileExtension+i];
    
    if (_wcsnicmp(wcExt, L"jpg", 3) == 0)
        dwFilterIndex = 1;
    else if (_wcsnicmp(wcExt, 
        L"bmp", 3) ==0)
        dwFilterIndex =2;
    else 
        dwFilterIndex = 0;
    
    return dwFilterIndex;
}



/********************************************************************
 *
 *  FUNCTION:       LoadPic
 *
 *  DESCRIPTION:    Load a picture
 *
 *  PARAMETERS:     UINT uIndex
 *
 *  RETURNS:        TRUE if successfully, otherwise return FALSE
 *
 ********************************************************************/
BOOL CPicBrowserDlg::LoadPic(UINT uIndex)
{
    
    BOOL bResult = TRUE;
    
    switch(uIndex)
    {
        case 1:        //JPEG file
            {
                UINT uHeight;
                UINT uWidth;
                BYTE *dataBuf;
                //read the file
                dataBuf = JpegFile::JpegFileToRGB(m_wcFileName,
                                        &uWidth,
                                        &uHeight);
                if (dataBuf==NULL) 
                {
                    RETAILMSG(CAMAPP_DEBUG_MSG, (TEXT("LoadPic: Load JPG File failed!\r\n")));
                    bResult = FALSE;
                    break;
                }

                // RGB -> BGR
                JpegFile::BGRFromRGB(dataBuf, uWidth, uHeight);

                BYTE *buf;
                UINT uiOutWidthBytes = uWidth*3;
                // create a DWORD aligned buffer from the JpegFile object
                buf = JpegFile::MakeDwordAlignedBuf(dataBuf,
                                            uWidth,
                                            uHeight,
                                            &uiOutWidthBytes);

                // flip that buffer
                JpegFile::VertFlipBuf(buf, uWidth*3, uHeight);

                // you now have a buffer ready to be used as a DIB
                //create a DIB to display
                BITMAPINFO bmp_info;  
                 /* for win32, must fill up the structures */
                bmp_info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
                bmp_info.bmiHeader.biWidth = uWidth;
                bmp_info.bmiHeader.biHeight = uHeight;
                /* win32 say must be 1 * /
                bmp_info.bmiHeader.biPlanes = 1;
                bmp_info.bmiHeader.biBitCount = 24;
                bmp_info.bmiHeader.biCompression = BI_RGB;
                /* win32 say must be 0 for the next 5 fields */
                bmp_info.bmiHeader.biSizeImage = 0;
                bmp_info.bmiHeader.biXPelsPerMeter = 0;
                bmp_info.bmiHeader.biYPelsPerMeter = 0;
                bmp_info.bmiHeader.biClrUsed = 0;
                bmp_info.bmiHeader.biClrImportant = 0;

                if (!m_DIBSection.SetBitmap(&bmp_info, (void **)buf))
                {
                    RETAILMSG(CAMAPP_ERROR_MSG, (TEXT("LoadPic: SetBitmap for JPG failed!\r\n")));
                    bResult = FALSE;
                }    
                else 
                {
                    RETAILMSG(CAMAPP_ERROR_MSG, (TEXT("LoadPic: Load JPG File done!\r\n")));        
                }
                //Delete databuf and buf
                if (dataBuf != NULL)
                {
                    free(dataBuf);
                    dataBuf = NULL;
                }
                if (buf != NULL)
                {
                    free(buf);
                    buf = NULL;
                }
            }
            break;
            
        case 2:
            if (!m_DIBSection.Load(m_wcFileName))
            {
                RETAILMSG(CAMAPP_DEBUG_MSG, (TEXT("LoadPic: load BMP file failed.!\r\n")));
                bResult = FALSE;
            }
            else 
            {
                RETAILMSG(CAMAPP_DEBUG_MSG, (TEXT("LoadPic: load BMP file Done.!\r\n")));
            }
            break;
            
        default:
            RETAILMSG(CAMAPP_DEBUG_MSG,(TEXT("LoadPic: Unknown file format!\r\n")));
            bResult = FALSE;
            break;
    }
    
    return bResult;
    
}


/********************************************************************
 *
 *  FUNCTION:       DrawPic
 *
 *  DESCRIPTION:    Draw picture in PicBrowser dialog
 *
 *  PARAMETERS:     HDC hDC
 *
 *  RETURNS:        TRUE if successfully, otherwise return FALSE
 *
 ********************************************************************/
BOOL CPicBrowserDlg::DrawPic(HDC hDC)
{
    POINT pt = {0,0};
    
    if (!m_DIBSection.Draw(hDC, pt))
    {
        RETAILMSG(CAMAPP_ERROR_MSG, (TEXT("DrawPic: Draw picture failed!\r\n")));
        return FALSE;
    }
    else 
    {
        RETAILMSG(CAMAPP_ERROR_MSG, (TEXT("DrawPic: Draw picture done!\r\n")));
        return TRUE;
    }
    
        return TRUE;
}

/*********************************************************************
 END OF FILE
*********************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -