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

📄 ext_to_swfdlg.cpp

📁 实现对flash文件的swf格式和exe格式的批量转换!
💻 CPP
字号:
// ext_to_swfDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ext_to_swf.h"
#include "ext_to_swfDlg.h"
#include <stdio.h>

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CExt_to_swfDlg dialog

CExt_to_swfDlg::CExt_to_swfDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CExt_to_swfDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CExt_to_swfDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CExt_to_swfDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CExt_to_swfDlg)
	DDX_Control(pDX, IDC_PROGRESS_CONVERT, m_ctrlProg);
	DDX_Control(pDX, IDC_LIST_SWF, m_ctrlListSwf);
	DDX_Control(pDX, IDC_LIST_EXE, m_ctrlListExe);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CExt_to_swfDlg, CDialog)
	//{{AFX_MSG_MAP(CExt_to_swfDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_CONVERT, OnConvert)
	ON_BN_CLICKED(IDC_CONVERT_EXE, OnConvertExe)
	ON_BN_CLICKED(IDC_OPEN_EXE, OnOpenExe)
	ON_BN_CLICKED(IDC_OPEN_SWF, OnOpenSwf)
	ON_NOTIFY(NM_CLICK, IDC_LIST_EXE, OnClickListExe)
	ON_NOTIFY(NM_CLICK, IDC_LIST_SWF, OnClickListSwf)
	ON_NOTIFY(NM_DBLCLK, IDC_LIST_EXE, OnDblclkListExe)
	ON_NOTIFY(NM_DBLCLK, IDC_LIST_SWF, OnDblclkListSwf)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CExt_to_swfDlg message handlers

BOOL CExt_to_swfDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CExt_to_swfDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

void CExt_to_swfDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

HCURSOR CExt_to_swfDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

BOOL Valid16(CString strBuff)
{
	strBuff.MakeLower();
	for( int i = 0; i < strBuff.GetLength();i++)
	{
		if( strBuff.GetAt(i) >= '0' && strBuff.GetAt(i) <= '9' ||  strBuff.GetAt(i) >= 'a' && strBuff.GetAt(i) <= 'f')
		{
		}
		else
			return FALSE;
	}

	return TRUE;
}

long Convert16to10(CString strLen)
{
	long nLen = 0;
	for( int i = 0; i < strLen.GetLength();i++)
	{
		if( strLen.GetAt(i) >= '0' && strLen.GetAt(i) <= '9' )
			nLen = nLen * 16 + strLen.GetAt(i) - '0';
		
		if( strLen.GetAt(i) >= 'a' && strLen.GetAt(i) <= 'f' )
			nLen = nLen * 16 + strLen.GetAt(i) - 'a' + 10;
		
		if( strLen.GetAt(i) >= 'A' && strLen.GetAt(i) <= 'F' )
			nLen = nLen * 16 + strLen.GetAt(i) - 'A' + 10;
	}

	return nLen;
}

void CExt_to_swfDlg::OnConvert() 
{
	int nCount = m_ctrlListExe.GetSelectedCount();
	if(nCount == 0)
	{
		AfxMessageBox("请选择要转换的文件");
		return ;
	}

	CWnd *pWnd = GetDlgItem(IDC_CONVERT_EXE);
	pWnd->EnableWindow(FALSE);

	m_ctrlProg.SetPos(nCount);
	m_ctrlProg.SetStep(1);
	m_ctrlProg.SetRange(1,nCount);
	m_ctrlProg.SetStep(1);
	m_ctrlProg.SetPos( 0 );

	int nNum = 0;
	POSITION pos = m_ctrlListExe.GetFirstSelectedItemPosition();
	while(pos)
	{
		nNum++;

		int nItem = m_ctrlListExe.GetNextSelectedItem(pos);
		CString strFile = m_ctrlListExe.GetItemText(nItem,0);

		if(!ExeToSwf(_T(strFile)))
		{
			AfxMessageBox(strFile + "\n转换失败");
		}
		m_ctrlProg.SetPos( nNum );
	}
	m_ctrlProg.SetPos(0);
	AfxMessageBox("转换完成");

	pWnd->EnableWindow(TRUE);
}

BOOL CExt_to_swfDlg::ExeToSwf(CString strEXE, CString strSWF)
{
	if(strEXE.IsEmpty())
	{
		return FALSE;
	}
	else
	{
		CString strExt = strEXE.Right(4);
		
		strExt.MakeLower();
		if(strExt != _T(".exe"))
		{
			return FALSE;
		}
	}

	if(strSWF.IsEmpty())
	{
		int nLen = strEXE.GetLength();
		strSWF = strEXE.Left(nLen - 4);
		strSWF += _T(".swf");
	}

	LONG nLen  = 0;
	LONG nTemp = 0;
	CFile exeFile;
	CFile swfFile;
	TRY
	{
		if(!exeFile.Open(strEXE,CFile::modeRead | CFile::typeBinary))
			return FALSE;

		BYTE *pszFlashFlag = new BYTE[9];
		memset(pszFlashFlag,'\0',9);

		nTemp = exeFile.Seek( -8, CFile::end );

		//读取标志位 FA123456(官方格式exe) 和 swf 文件长度
		//官方的 flash 的 exe 格式文件,
		//在文件最后的 8 个字节中,
		//高四位字节为 FA123456 标志位,
		//底四字节为 swf 文件的长度

		exeFile.Read(pszFlashFlag,8); //读取文件的最后8个字节

		CString strFlg;
		
		//高四位
		strFlg.Format("%X%X%X%X",
			pszFlashFlag[3],
			pszFlashFlag[2],
			pszFlashFlag[1],
			pszFlashFlag[0]);
		if(_T("FA123456") != strFlg)
		{
			AfxMessageBox("非标准格式 flash 文件");
			return FALSE;
		}

		//低四位
		CString strLen[4];
		strLen[0].Format("%X",pszFlashFlag[7]);
		strLen[1].Format("%X",pszFlashFlag[6]);
		strLen[2].Format("%X",pszFlashFlag[5]);
		strLen[3].Format("%X",pszFlashFlag[4]);
		delete [] pszFlashFlag;

		for(int i = 0;i < 4; i++)
		{
			int nStrLen = strLen[i].GetLength();
			if( nStrLen > 2 )
				return FALSE;

			for(int j = 0;j < 2 - nStrLen;j++)
			{
				strLen[i] = _T("0") + strLen[i];
			}
		}

		CString strLenght = strLen[0] + strLen[1] + strLen[2] + strLen[3];
		if(!Valid16(strLenght))
			return FALSE;

		//将十六进制转换成十进制
		nTemp = Convert16to10(strLenght); //获取 swf 文件的长度
		nLen = exeFile.Seek( -( nTemp + 8), CFile::end );

		//判断是否为有效 swf 文件,
		//有效的 swf 文件,其文件的头三个字符为 FWS 或 CWS
		BYTE szSwfFlag[4] = {0};
		exeFile.Read(szSwfFlag,3);
		strFlg.Format("%s",szSwfFlag);
		if( strFlg != _T("FWS") && strFlg != _T("CWS"))
			return FALSE;

		nLen = exeFile.Seek( -( nTemp + 8), CFile::end );
		BYTE *pszBuff = new BYTE[nTemp + 1];
		if(pszBuff == NULL)
		{
			exeFile.Close();
			swfFile.Close();

			return FALSE;
		}

		memset(pszBuff,'\0',nTemp + 1);
		exeFile.Read(pszBuff,nTemp);
		exeFile.Close();
		
		if(!swfFile.Open(strSWF,CFile::modeCreate | CFile::modeWrite | CFile::typeBinary))
		{
			return FALSE;
		}
		swfFile.Write(pszBuff,nTemp);

		delete [] pszBuff;
		swfFile.Close();
	}
	CATCH (CException,e)
	{
	}
	END_CATCH
	
	return TRUE;
}

void CExt_to_swfDlg::OnConvertExe() 
{
	int nCount = m_ctrlListSwf.GetSelectedCount();
	if(nCount == 0)
	{
		AfxMessageBox("请选择要转换的文件");
		return ;
	}

	char szFilters[] = "Flash Player(*.exe)|*.exe||";
	CFileDialog dlg(true,"","*.exe",OFN_HIDEREADONLY,szFilters,this);

	if(dlg.DoModal() != IDOK)
	{
		return ;
	}

	CWnd *pWnd = GetDlgItem(IDC_CONVERT);
	pWnd->EnableWindow(FALSE);

	CString strPlayer = dlg.GetFileName();

	m_ctrlProg.SetPos(nCount);
	m_ctrlProg.SetStep(1);
	m_ctrlProg.SetRange(1,nCount);
	m_ctrlProg.SetStep(1);
	m_ctrlProg.SetPos( 0 );

	int nNum = 0;
	POSITION pos = m_ctrlListSwf.GetFirstSelectedItemPosition();
	while(pos)
	{
		nNum++;

		int nItem = m_ctrlListSwf.GetNextSelectedItem(pos);
		CString strFile = m_ctrlListSwf.GetItemText(nItem,0);

		if(!SwfToExe(strPlayer,strFile))
		{
			AfxMessageBox(strFile + "\n转换失败");
		}
		m_ctrlProg.SetPos( nNum );
	}
	m_ctrlProg.SetPos(0);
	AfxMessageBox("转换完成");

	pWnd->EnableWindow(TRUE);
}

BOOL CExt_to_swfDlg::SwfToExe(CString strPlayer,CString strSWF,CString strExe)
{
	if(strSWF.IsEmpty() || strPlayer.IsEmpty())
	{
		return FALSE;
	}
	else
	{
		CString strExt = strSWF.Right(4);
		
		strExt.MakeLower();
		if(strExt != _T(".swf"))
		{
			return FALSE;
		}
	}

	if(strExe.IsEmpty())
	{
		int nLen = strSWF.GetLength();
		strExe = strSWF.Left(nLen - 4);
		strExe += _T(".exe");
	}

	LONG nLen  = 0;
	LONG nTemp = 0;
	CFile playerFile;
	CFile exeFile;
	CFile swfFile;
	TRY
	{
		if(!playerFile.Open(strPlayer,CFile::modeRead | CFile::typeBinary))
			return FALSE;

		if(!swfFile.Open(strSWF,CFile::modeRead | CFile::typeBinary))
			return FALSE;

		BYTE szExeFlag[ 9 ] = { 0 };
		szExeFlag[0] = 0x56; //86 为 0x56的 十进制
		szExeFlag[1] = 0x34;
		szExeFlag[2] = 0x12;
		szExeFlag[3] = 0xFA;

		nLen = swfFile.GetLength();
		CString strLen;
		strLen.Format("%X",nLen);
		int nStrLen = strLen.GetLength();
		if(nStrLen > 8)
		{
			swfFile.Close();
			return FALSE;
		}

		for(int i = 0;i < 8 - nStrLen;i++)
		{
			strLen = _T("0") + strLen;
		}

		CString str1,str2,str3,str4;
		str1 = strLen.Mid(0,2);
		str2 = strLen.Mid(2,2);
		str3 = strLen.Mid(4,2);
		str4 = strLen.Mid(6,2);

		szExeFlag[4] = (int)Convert16to10(str4); 
		szExeFlag[5] = (int)Convert16to10(str3);
		szExeFlag[6] = (int)Convert16to10(str2);
		szExeFlag[7] = (int)Convert16to10(str1);

		if(!exeFile.Open(strExe,CFile::modeCreate | CFile::modeWrite | CFile::typeBinary))
		{
			playerFile.Close();
			swfFile.Close();
			return FALSE;
		}

		nLen = playerFile.GetLength();
		BYTE *pszBuf = new BYTE[ nLen + 1 ];
		memset(pszBuf,'\0',nLen);
		playerFile.Read(pszBuf,nLen);
		playerFile.Close();
		exeFile.Write(pszBuf,nLen);
		delete [] pszBuf;
		pszBuf =  0;

		nLen = swfFile.GetLength();
		pszBuf = new BYTE[ nLen + 1 ];
		memset(pszBuf,'\0',nLen);
		swfFile.Read(pszBuf,nLen);
		swfFile.Close();
		exeFile.Write(pszBuf,nLen);
		delete [] pszBuf;
		pszBuf =  0;
		exeFile.Write(szExeFlag,8);
		exeFile.Close();
	}
	CATCH (CException,e)
	{
	}
	END_CATCH
		
	return TRUE;
}

void CExt_to_swfDlg::OnOpenExe() 
{
	FindExeFile();
}

void CExt_to_swfDlg::OnOpenSwf() 
{
	FindSwfFile();
}

void CExt_to_swfDlg::OnClickListExe(NMHDR* pNMHDR, LRESULT* pResult) 
{
//	FindExeFile();
	*pResult = 0;
}

void CExt_to_swfDlg::OnClickListSwf(NMHDR* pNMHDR, LRESULT* pResult) 
{
//	FindSwfFile();
	*pResult = 0;
}

void CExt_to_swfDlg::OnDblclkListExe(NMHDR* pNMHDR, LRESULT* pResult) 
{
	FindExeFile();
	*pResult = 0;
}

void CExt_to_swfDlg::OnDblclkListSwf(NMHDR* pNMHDR, LRESULT* pResult) 
{
	FindSwfFile();
	*pResult = 0;
}

void CExt_to_swfDlg::FindExeFile()
{
	char szFilters[] = "Flash EXE files(*.exe)|*.exe||";
	CFileDialog dlg(true,"","*.exe",OFN_HIDEREADONLY | OFN_ALLOWMULTISELECT,szFilters,this);

	if(dlg.DoModal() != IDOK)
		return ;

	//清除原有列表文件
	m_ctrlListExe.DeleteAllItems();

	CString strFile ;
	POSITION pos = dlg.GetStartPosition();
	while(pos)
	{
		strFile = dlg.GetNextPathName(pos);

		LVITEM	lvIt;
		lvIt.mask = LVIF_TEXT|LVIF_IMAGE|LVIF_PARAM;
		lvIt.pszText= strFile.GetBuffer(0);
		lvIt.iSubItem = 0;
		lvIt.iItem = 0;
		m_ctrlListExe.InsertItem(&lvIt);
	}
}

void CExt_to_swfDlg::FindSwfFile()
{
	char szFilters[] = "Flash SWF files(*.swf)|*.swf||";
	CFileDialog dlg(true,"","*.swf",OFN_HIDEREADONLY | OFN_ALLOWMULTISELECT,szFilters,this);
	if(dlg.DoModal() != IDOK)
		return ;

	//清除原有列表文件
	m_ctrlListSwf.DeleteAllItems();

	CString strFile ;
	POSITION pos = dlg.GetStartPosition();
	while(pos)
	{
		strFile = dlg.GetNextPathName(pos);

		LVITEM	lvIt;
		lvIt.mask = LVIF_TEXT|LVIF_IMAGE|LVIF_PARAM;
		lvIt.pszText= strFile.GetBuffer(0);
		lvIt.iSubItem = 0;
		lvIt.iItem = 0;
		m_ctrlListSwf.InsertItem(&lvIt);
	}
}

⌨️ 快捷键说明

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