📄 big2gbdlg.cpp
字号:
// BIG2GBDlg.cpp : implementation file
//
#include "stdafx.h"
#include "BIG2GB.h"
#include "BIG2GBDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CBIG2GBDlg dialog
CBIG2GBDlg::CBIG2GBDlg(CWnd* pParent /*=NULL*/)
: CDialog(CBIG2GBDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CBIG2GBDlg)
m_SourceFileName = _T("");
m_DescFileName = _T("");
m_Result = 1; //默认为简体
//}}AFX_DATA_INIT
bBigToGB=TRUE;
strLine.SetSize(0);
m_lineNumber=CalLineNumber();
m_text="\n";
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CBIG2GBDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CBIG2GBDlg)
DDX_Text(pDX, IDC_EDIT1, m_SourceFileName);
DDX_Text(pDX, IDC_EDIT2, m_DescFileName);
DDX_Radio(pDX, IDC_RADIO1, m_Result);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CBIG2GBDlg, CDialog)
//{{AFX_MSG_MAP(CBIG2GBDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_DIRBROWSER, OnDirbrowser)
ON_BN_CLICKED(IDC_FILEBROWSER, OnFilebrowser)
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_RADIO1, OnRadio1)
ON_BN_CLICKED(IDC_RADIO2, OnRadio2)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBIG2GBDlg message handlers
BOOL CBIG2GBDlg::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 CBIG2GBDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CBIG2GBDlg::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();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CBIG2GBDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
//源文件名
void CBIG2GBDlg::OnDirbrowser()
{
CFileDialog fileDialog(TRUE,NULL,NULL,NULL,"文本文件(*.txt)|*.txt|所有文件(*.*)|*.*||");
if (fileDialog.DoModal() == IDOK) {
strOpenTXTName = fileDialog.GetPathName();
m_SourceFileName = strOpenTXTName;
UpdateData(FALSE);
m_text=_T("");
}
}
//另存为目标文件名
void CBIG2GBDlg::OnFilebrowser()
{
CFileDialog fileDialog( FALSE,"*.txt",NULL,NULL,"文本文件(*.txt)|*.txt|所有文件(*.*)|*.*||");
if (fileDialog.DoModal() == IDOK) {
strSaveTXTName=fileDialog.GetPathName();
m_DescFileName = strSaveTXTName;
UpdateData(FALSE);
}
}
void CBIG2GBDlg::OnOK()
{
if (strSaveTXTName.IsEmpty() || strOpenTXTName.IsEmpty ())
{
AfxMessageBox("\n 抱歉,请选择要转换的文件名和转换后另存为的文件名! ");
return;
}
//读文件
TCHAR buffer[0xff]="\0";
CStdioFile file(m_SourceFileName, CFile::modeRead|CFile::typeBinary);
DWORD count = 0;
DWORD Length = file.GetLength();
do
{ memset(buffer,'\0',sizeof(buffer));
count=file.Read(buffer, sizeof(buffer)-1);
m_text += buffer;
}
while (count == sizeof(buffer)-1);
m_text+="\n";
DWORD tmp=m_text.GetLength();
m_lineNumber=CalLineNumber();
//简繁体转换
if(m_Result==1)
BigtoGB();
else
GBtoBig() ;
//写入文件
try {
CStdioFile file(m_DescFileName, CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);
m_text="";
for(long i=0;i<m_lineNumber;i++)
{
m_text+=strLine[i];
m_text+='\r'; //每一行后要加上一换行符,否则会出现乱码现象
m_text+='\n';
}
file.WriteHuge(m_text, m_text.GetLength());
AfxMessageBox("文件转换成功! ");
}
catch (...)
{
return ;
}
// CDialog::OnOK();
}
CString CBIG2GBDlg::Range(long start, long end)
{
return m_text.Mid(start,end-start);
}
CString CBIG2GBDlg::GetText()
{
return m_text;
}
int CBIG2GBDlg::GetTextLength()
{
return m_text.GetLength();
}
long CBIG2GBDlg::CalLineNumber()
{
strLine.SetSize(0);
CString strTemp;
BOOL bReturn=FALSE;
if(!GetTextLength())
return 0;
long line_num=0;
int index=0;
do
{
TCHAR c=m_text.GetAt(index++);
if(c=='\t')
strTemp+=" ";
else if(c!='\n')
strTemp+=c;
else
{
bReturn=TRUE;
line_num++;;
strLine.Add(strTemp.Left(strTemp.GetLength()-1));
strTemp="\0";
}
}
while(index<GetTextLength());
if(!bReturn)
strLine.Add(strTemp);
return strLine.GetSize();
}
long CBIG2GBDlg::GetLineNumber()
{
return m_lineNumber;
}
void CBIG2GBDlg::BigtoGB()
{
CalLineNumber();
LPWORD pBIGTable=((CBIG2GB*)AfxGetApp())->GetBigTable();
if(m_text=="\n")
return;
for(int Line=0;Line<GetLineNumber();Line++)
{
BYTE* SourceSTR;
SourceSTR=(BYTE* )((LPCTSTR)strLine[Line]);
int size=strLine[Line].GetLength();
if(size)
{
BYTE* DestSTR=new BYTE[size+1];
unsigned i=0;
do
{
// is English 是英文字符
if(SourceSTR[i]<0xA1||(SourceSTR[i+1]<0x40))
{
DestSTR[i]=SourceSTR[i];
i++;
}
else //是BIG5的汉字码
{
DestSTR[i+1]=HIBYTE(pBIGTable[(SourceSTR[i]-0xA1)*0xBF+SourceSTR[i+1]-0x40]);
DestSTR[i]=LOBYTE(pBIGTable[(SourceSTR[i]-0xA1)*0xBF+SourceSTR[i+1]-0x40]);
i+=2;
}
}
while(i<strlen((char*)(SourceSTR)));
DestSTR[i]='\0';
strLine[Line]=DestSTR;
delete DestSTR;
}
}
}
void CBIG2GBDlg::GBtoBig()
{
CalLineNumber();
LPWORD pGBTable=((CBIG2GB*)AfxGetApp())->GetGBTable();
if(m_text=="\n")
return;
for(int Line=0;Line<GetLineNumber();Line++)
{
BYTE* SourceSTR;
SourceSTR=(BYTE* )((LPCTSTR)strLine[Line]);
int size=strLine[Line].GetLength();
if(size)
{
BYTE* DestSTR=new BYTE[size+1];
unsigned i=0;
do
{
// is English 是英文字符
if(SourceSTR[i]<0xA1||(SourceSTR[i+1]<0xA1))
{
DestSTR[i]=SourceSTR[i];
i++;
}
else if(SourceSTR[i]>0xA1&&SourceSTR[i]<0xB0) //是GB2312的汉字码
{
DestSTR[i+1]=HIBYTE(pGBTable[(SourceSTR[i]-0xA1)*0x5E+SourceSTR[i+1]-0xA1]);
DestSTR[i]=LOBYTE(pGBTable[(SourceSTR[i]-0xA1)*0x5E+SourceSTR[i+1]-0xA1]);
i+=2;
}
else
{
DestSTR[i+1]=HIBYTE(pGBTable[(SourceSTR[i]-0xA7)*0x5E+SourceSTR[i+1]-0xA1]);
DestSTR[i]=LOBYTE(pGBTable[(SourceSTR[i]-0xA7)*0x5E+SourceSTR[i+1]-0xA1]);
i+=2;
}
}
while(i<strlen((char*)(SourceSTR)));
DestSTR[i]='\0';
strLine[Line]=DestSTR;
delete DestSTR;
}
}
}
void CBIG2GBDlg::OnButton1()
{
if (m_DescFileName.IsEmpty())
{
AfxMessageBox("\n 抱歉,没有发现转换后的文件名! ");
return;
}
HINSTANCE result =ShellExecute(NULL, _T("open"), m_DescFileName, NULL,NULL, SW_SHOW);
}
void CBIG2GBDlg::OnRadio1()
{
m_Result=0;
}
void CBIG2GBDlg::OnRadio2()
{
m_Result=1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -