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

📄 imgcategview_helper.cpp

📁 Resource editor base speadrum Chinese mobile
💻 CPP
字号:
// PicCategView_Helper.cpp : implementation file
//

#include "stdafx.h"
#include "resourceeditor.h"
#include "ImgCategView.h"

#include "DirFileInfo.h"
#include "FileDlg.h"
#include "ItemDlg.h"
#include "Util.h"
#include "RenameDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

BOOL CImgCategView::AddImgDir(CDirFileNode ** ppNode, 
							  HTREEITEM & hItem, 
							  LPCTSTR pszDirName )
{
    _ASSERTE( ppNode != NULL && pszDirName != NULL );

    CDirFileNode * pNode = (CDirFileNode *)m_pInfo->MallocNode();
    if( NULL == pNode )
        return FALSE;

    CString strDir = pszDirName;
    strDir.TrimRight(_T('\\'));
    LPCTSTR pszDir = strDir.GetBuffer(0);
    LPCTSTR pszFind = _tcsrchr(pszDir, _T('\\'));
    if( pszFind == NULL )
        pszFind = pszDir;
    else
        pszFind++;

    pNode->bIsDir = TRUE;

//xb
	CString strNewName;
    if(!DoCheckAndRenameDir(*ppNode,pszFind,strNewName))
	{
		strDir.ReleaseBuffer();
		return FALSE;
	}
	_tcscpy(pNode->szID, g_theApp.m_MMIRes.MakeImgID(strNewName));
	_tcscpy(pNode->szName, strNewName);
    strDir.ReleaseBuffer();
//xe*/


    m_pInfo->AddChild(*ppNode, pNode);

    CTreeCtrl &tc = GetTreeCtrl();
    HTREEITEM hInsert = tc.InsertItem(strNewName, IMG_COLL_IDX, IMG_COLL_SEL_IDX, hItem);
   
	_ASSERTE( hInsert != NULL );

    tc.SetItemData(hInsert, (DWORD)pNode);

    *ppNode = pNode;
    hItem   = hInsert;

    return TRUE;
}

BOOL CImgCategView::AddImgFile(CDirFileNode ** ppNode, 
							   HTREEITEM & hItem, 
							   LPCTSTR pszFileName, 
							   BOOL bCompressed /*= TRUE*/ )
{
    _ASSERTE( ppNode != NULL && pszFileName != NULL );

    CDirFileNode * pParentNode = *ppNode;
    HTREEITEM      hParentItem = hItem;

    CString strFileName;

    CMMIRes &mmires = g_theApp.m_MMIRes;
    CString strID = mmires.MakeImgID(pszFileName);

    if( !g_theApp.DoCheckAndRename(pszFileName, strID, strFileName) )
    {
        TRACE0("g_theApp.DoCheckAndRename() fail in AddImgFile()\r\n");
        return FALSE;
    }

    //@hongliang xin 2006-9-25 添加bCompressed参数
	if( !mmires.AddImg(strID, pszFileName, bCompressed) )
    {
        AfxMessageBox(mmires.GetErrMsg());
        return  FALSE;
    }

    PIMGINFO pImgInfo = NULL;
    mmires.m_mapImg.Lookup(strID, pImgInfo);
    _ASSERTE( pImgInfo != NULL );
    LPBYTE pBmp = pImgInfo->pOriginal;

    PBITMAPINFOHEADER pbih = (PBITMAPINFOHEADER)(pBmp + sizeof(BITMAPFILEHEADER));

    CDirFileNode * pNode = (CDirFileNode *)m_pInfo->MallocNode();
    if( NULL == pNode )
    {
        AfxMessageBox(_T("Memory no enough!"));
        mmires.DeleteImg(strID);
        return FALSE;
    }
    m_pInfo->ZeroNode(pNode);
    pNode->nWidth  = pbih->biWidth;
    pNode->nHeight = pbih->biHeight;

    LPCTSTR pName = strFileName.GetBuffer(0);
    LPCTSTR pFind = _tcsrchr(pName, _T('\\'));
    if( pFind != NULL )
        pFind++;
    else
        pFind = pName;

    _tcscpy(pNode->szID, strID);
    _tcscpy(pNode->szName, pFind);
   
    CTreeCtrl & tc = GetTreeCtrl();

    HTREEITEM hInsert = tc.InsertItem(pFind, IMG_IDX, IMG_SEL_IDX, hParentItem );
    strFileName.ReleaseBuffer();

    tc.SetItemData( hInsert, (DWORD)pNode );
  
    m_pInfo->AddChild(pParentNode, pNode);

    *ppNode = pNode;
    hItem   = hInsert;

    return TRUE;
}

BOOL CImgCategView::EnumAndFillImgs( CDirFileNode * pParentNode, 
									 HTREEITEM &hParentItem, 
									 LPCTSTR pszPathName, 
									 BOOL bCompressed /*= TRUE*/ )
{
    _ASSERTE( pParentNode != NULL && pszPathName != NULL );

    BOOL      bRet        = FALSE;
    CString   strPathName = pszPathName;
    HTREEITEM hOldParent  = hParentItem;
    HTREEITEM hItem       = NULL;
    CUtil     fileUtil, dirUtil;

    {
        bRet = AddImgDir(&pParentNode, hParentItem, pszPathName);
        hOldParent = hParentItem;
        
        strPathName.TrimRight(_T('\\'));
        strPathName += _T("\\*.*");
        
        WIN32_FIND_DATA wfd = { 0 };
        HANDLE hFind = ::FindFirstFile(strPathName, &wfd);
        _ASSERTE( hFind != INVALID_HANDLE_VALUE );
        
        ::FindNextFile(hFind, &wfd);
        while( ::FindNextFile(hFind, &wfd) && bRet )
        {
            strPathName = pszPathName;
            strPathName.TrimRight(_T('\\'));
            
            strPathName += _T('\\');
            strPathName += wfd.cFileName;
            
            LPCTSTR pExt = _tcsrchr(wfd.cFileName, _T('.'));
            if( wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
            {
                dirUtil.AddStr(strPathName);
            }
            else  if( pExt != NULL && _tcsicmp(pExt, SZ_FILE_EXT) == 0 )
            {
                fileUtil.AddStr(strPathName);
            }        
        }
        ::FindClose(hFind);

        CDirFileNode * pChild = pParentNode;
        HTREEITEM      hItem  = hParentItem;
        fileUtil.SortStr();
        int nCount = fileUtil.GetStrSize();
        for( int i = 0; bRet && i < nCount; ++i )
        {
            pChild = pParentNode;
            hItem  = hParentItem;
            bRet = AddImgFile(&pChild, hItem, fileUtil.GetStr(i), bCompressed);
        }
        fileUtil.RemoveAll();
    }

    dirUtil.SortStr();
   
    hItem  = hParentItem;
    int nCount = dirUtil.GetStrSize();
    for( int i = 0; bRet && i < nCount; ++i )
    {
        bRet = EnumAndFillImgs( pParentNode, hItem, dirUtil.GetStr(i),bCompressed );
        hItem = hParentItem;
    }
    dirUtil.RemoveAll();

    hParentItem = hOldParent;

    return bRet;
}

BOOL CImgCategView::RemoveAllRes(CDirFileNode *pNode)
{
    _ASSERTE( pNode != NULL );

    RecursiveRemoveAllRes(pNode);

    m_pInfo->RemoveNode(pNode);

    return TRUE;
}

BOOL CImgCategView::RecursiveRemoveAllRes(CDirFileNode *pNode)
{
    if( pNode == NULL )
        return TRUE;

    CDirFileNode * pChild = (CDirFileNode *)m_pInfo->GetChild(pNode);
	while ( NULL != pChild )
	{
		CDirFileNode * pNext = (CDirFileNode *)m_pInfo->GetNext(pChild);
        	
		RecursiveRemoveAllRes( pChild );
                
		pChild = pNext;
	}
	
    if( !m_pInfo->IsHasChild(pNode) )
    {
        CMMIRes &mmires = g_theApp.m_MMIRes;
        mmires.DeleteImg(pNode->szID);
    }
   
    return TRUE;
}

BOOL CImgCategView::CheckBmpSize(LPCTSTR pszFileName, CSize &sizeBmp)
{
    _ASSERTE( pszFileName != NULL );
    _ASSERTE( sizeBmp.cx > 0 && sizeBmp.cy > 0 );

    HANDLE hFile = ::CreateFile( pszFileName, GENERIC_READ, FILE_SHARE_READ,
                                 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
                                 NULL );
    if( hFile == INVALID_HANDLE_VALUE )
    {
        CString strMsg;
        strMsg.Format(_T("The file %s not exist!"), pszFileName);

        AfxMessageBox(strMsg);
        return FALSE;
    }

    DWORD dwRead = 0;
    BITMAPFILEHEADER bfh = { 0 };
    VERIFY( ReadFile(hFile, &bfh, sizeof(BITMAPFILEHEADER), &dwRead, NULL) );

    BITMAPINFOHEADER bih = { 0 };
    VERIFY( ReadFile(hFile, &bih, sizeof(BITMAPINFOHEADER), &dwRead, NULL) );

    CloseHandle(hFile);

    if( bfh.bfType != 0x4d42 )
    {
        CString strMsg;
        strMsg.Format(_T("The file \"%s\" isn't a bitmap file!"), pszFileName);
        AfxMessageBox(strMsg);

        return FALSE;
    }

    if( sizeBmp.cx != bih.biWidth || sizeBmp.cy != bih.biHeight )
    {
        CString strMsg;
        strMsg.Format(_T("The file \"%s\" isn't %d * %d!"), pszFileName, sizeBmp.cx, sizeBmp.cy);
        AfxMessageBox(strMsg);

        return FALSE;
    }

    return TRUE;
}

BOOL CImgCategView::ReplaceDir(CDirFileNode *pNode, 
							   LPCTSTR pszDir, 
							   BOOL bCheck /* = FALSE */, 
							   BOOL bCompressed /*= TRUE*/)
{
    _ASSERTE( m_pInfo != NULL && pNode != NULL);
    _ASSERTE( pszDir != NULL );

    CDirFileNode * pChild = (CDirFileNode *)m_pInfo->GetChild(pNode);
    BOOL bReplaced = FALSE;
    
    RecursiveReplaceDir(pChild, pszDir, bReplaced, bCheck, bCompressed);

    return bReplaced;
}

BOOL CImgCategView::RecursiveReplaceDir(CDirFileNode * pNode, 
										LPCTSTR pszDir, 
										BOOL &bReplaced, 
										BOOL bCheck /* = FALSE */, 
										BOOL bCompressed /*= TRUE*/)
{
    _ASSERTE( m_pInfo != NULL );
    _ASSERTE( pszDir != NULL );
    
	static int i =1;

    if( pNode == NULL )
        return TRUE;
    
    CString strDir    = pszDir;
    BOOL    bRet      = TRUE;
    BOOL    bContinue = FALSE;
    
    CTreeCtrl &tc   = GetTreeCtrl();
    HTREEITEM hSel  = tc.GetSelectedItem();
    _ASSERTE( hSel != NULL );
    {
        CDirFileNode * pSel = (CDirFileNode *)tc.GetItemData(hSel);
        _ASSERTE(pSel != NULL);
    
        for( CDirFileNode * pTemp = pNode; pTemp != NULL && pTemp != pSel; pTemp = (CDirFileNode *)m_pInfo->GetParent(pTemp) )
        {
			if( m_pInfo->GetNext(pTemp) != NULL )
            {
                bContinue = TRUE;
                break;
            }
        }
    }

    strDir += _T("\\");
    strDir += pNode->szName;
    if( m_pInfo->IsDirNode(pNode) )
    {
        CDirFileNode * pChild = (CDirFileNode *)m_pInfo->GetChild(pNode);
        //_ASSERTE( pChild != NULL );
		if(pChild != NULL)
			bRet = RecursiveReplaceDir(pChild, strDir, bReplaced, bCheck, bCompressed);
		else
			bRet = TRUE;
    }
    else if( !ReplaceFile(pNode, strDir, bCheck, hSel, bCompressed) ) //@hongliang.xin 2006-9-26 添加压缩参数
    {
        CString strMsg;
        strMsg.Format(_T("Replace \"%s\" fail! Continue?"), pNode->szName);
        if( bContinue && AfxMessageBox(strMsg, MB_YESNO) == IDNO )
            return FALSE;
    }
    else
    {
        bReplaced = TRUE;
    }    
	i++;
	CString str2;
	str2.Format(_T("%d"),i);
    //if(i>10)
	//	AfxMessageBox(str2);   

    strDir = pszDir;   

    if( bRet && bContinue )
       bRet = RecursiveReplaceDir((CDirFileNode *)m_pInfo->GetNext(pNode), 
	             strDir, bReplaced, bCheck, bCompressed);//@hongliang.xin 2006-9-26 添加压缩参数
	
	return bRet;
}

BOOL CImgCategView::ReplaceFile(CDirFileNode *pNode, 
								LPCTSTR pszFileName, 
                                BOOL bCheck /* = FALSE */,
								HTREEITEM hItem /* = NULL */, 
								BOOL bCompressed /*= TRUE*/ )
{
    _ASSERTE( pNode != NULL && pszFileName != NULL );

    CSize sizeBmp( pNode->nWidth, pNode->nHeight );
    if( bCheck && !CheckBmpSize(pszFileName, sizeBmp) )
        return FALSE;
  
    if( hItem != NULL )
    {
        m_SizeInfo.DeleteByHandle( (DWORD)hItem);
    }

    UINT uOld = 0;
    UINT uNew = 0;
    CMMIRes &mmires = g_theApp.m_MMIRes;
	//@hongliang.xin 2006-9-26 添加压缩参数
    if( mmires.ReplaceImg(pNode->szID, pszFileName, bCheck, &uOld, &uNew, bCompressed) )
    {
        PIMGINFO pImgInfo = NULL;
        VERIFY( mmires.m_mapImg.Lookup(pNode->szID, pImgInfo) );

        PBITMAPFILEHEADER pbfh = (PBITMAPFILEHEADER)pImgInfo->pOriginal;
        PBITMAPINFOHEADER pbih = (PBITMAPINFOHEADER)(pbfh + 1);

        pNode->nWidth  = pbih->biWidth;
        pNode->nHeight = pbih->biHeight;

        if( hItem != NULL && uNew > uOld )
        {
            m_SizeInfo.SetSizeInfo( (DWORD)hItem, uOld, uNew);
        }

	    SetModifiedFlag();
        return TRUE;
    }
    else
    {
        AfxMessageBox(g_theApp.m_MMIRes.GetErrMsg() );
    }

    return FALSE;
}

void CImgCategView::RemoveSizeInfo( HTREEITEM hItem )
{
    if( NULL == hItem )
        return;

    CTreeCtrl &tc = GetTreeCtrl();

    m_SizeInfo.DeleteByHandle( (DWORD)hItem );

    for( HTREEITEM hChild = tc.GetChildItem(hItem); hChild != NULL; 
         hChild = tc.GetNextSiblingItem(hChild) )
    {
        RemoveSizeInfo(hChild);
    }
}

BOOL CImgCategView::RenameDir( CDirFileNode * pParentNode,LPCTSTR pszOldName, CString &strNewName)
{
	CString strOldName(pszOldName);
    _ASSERTE( !strOldName.IsEmpty() );

    CRenameDlg dlg;
    dlg.m_strAdded  = strOldName;
    dlg.m_strAdding = strNewName;
	dlg.m_bDirFile  = TRUE;
	dlg.m_pParentNode = pParentNode;
	dlg.m_pDirFileInfo= m_pInfo;

    if( dlg.DoModal() == IDOK )
    {
        strNewName = dlg.m_strNewName;
        return TRUE;
    }
	else
		return FALSE;
}

BOOL CImgCategView::DoCheckAndRenameDir( CDirFileNode * pParentNode, LPCTSTR pszOldName, CString &strNewName)
{
	CDirFileNode * pCNode =(CDirFileNode *)m_pInfo->GetChild(pParentNode);
	while(pCNode != NULL)
	{
		if(_tcscmp(pCNode->szName, pszOldName)==0)
		{
			if(AfxMessageBox(_T("This directory already exist,\ndo you rename it?"), MB_YESNO)==IDNO)
				return FALSE;
			
			return RenameDir(pParentNode,pszOldName,strNewName);	
		
		}
        pCNode = (CDirFileNode *)m_pInfo->GetNext(pCNode);
	}
	strNewName = pszOldName;
	return TRUE;
}

⌨️ 快捷键说明

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