📄 fontsetconverterdlg.cpp
字号:
// FontSetConverterDlg.cpp : implementation file
//
#include "stdafx.h"
#include "FontSetConverter.h"
#include "FontSetConverterDlg.h"
#include "shapefil.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()
/////////////////////////////////////////////////////////////////////////////
// CFontSetConverterDlg dialog
CFontSetConverterDlg::CFontSetConverterDlg(CWnd* pParent /*=NULL*/)
: CDialog(CFontSetConverterDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CFontSetConverterDlg)
// 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);
m_pFileDir = NULL;
m_nDirBuffSize = 0;
m_nDirNum = 0;
}
void CFontSetConverterDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CFontSetConverterDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CFontSetConverterDlg, CDialog)
//{{AFX_MSG_MAP(CFontSetConverterDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BTN_BROWSE, OnBrowse)
ON_BN_CLICKED(IDC_BTN_UTF8TOGBK, OnBtnUtf8ToGbk)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFontSetConverterDlg message handlers
BOOL CFontSetConverterDlg::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 CFontSetConverterDlg::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 CFontSetConverterDlg::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 CFontSetConverterDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CFontSetConverterDlg::ReAllocDirBuff(int nSize)
{
ASSERT(nSize >= m_nDirBuffSize);
if(nSize <= m_nDirBuffSize)
return;
CString * pString = new CString [nSize];
if(!pString)
return;
for(int i=0; i<m_nDirNum; i++)
{
pString[i] = m_pFileDir[i];
}
if(m_pFileDir)
delete [] m_pFileDir;
m_pFileDir = pString;
m_nDirBuffSize = nSize;
}
void CFontSetConverterDlg::AllocDirBuff(int nSize)
{
if(m_pFileDir)
delete [] m_pFileDir;
m_pFileDir = NULL;
m_nDirNum = 0;
m_nDirBuffSize = 0;
ASSERT(nSize >0);
if(nSize <= 0)
return;
m_pFileDir = new CString [nSize];
if(!m_pFileDir)
{
delete [] m_pFileDir;
return;
}
m_nDirBuffSize = nSize;
}
void CFontSetConverterDlg::FreeDirBuff()
{
if(m_pFileDir)
delete [] m_pFileDir;
m_pFileDir = NULL;
m_nDirNum = 0;
m_nDirBuffSize = 0;
}
// find dbf path
void CFontSetConverterDlg::OnBrowse()
{
FreeDirBuff();
AllocDirBuff(5000);
BROWSEINFO bi;
ZeroMemory(&bi, sizeof(BROWSEINFO));
bi.hwndOwner = this->GetSafeHwnd();
LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
TCHAR szPath[MAX_PATH] = {0};
SHGetPathFromIDList(pidl, szPath);
FindFileInDir(szPath, ".dbf");
}
void CFontSetConverterDlg::FindFileInDir(CString strDir, CString strPostFix)
{
WIN32_FIND_DATA fd;
CString strSearchName = strDir + "\\*.*";
HANDLE hSearch = ::FindFirstFile(strSearchName, &fd);
if(hSearch == INVALID_HANDLE_VALUE)
{
return;
}
if( (fd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) && strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, "..") )
{
CString strNewDir;
strNewDir.Format("%s\\%s", strDir, fd.cFileName);
FindFileInDir(strNewDir, strPostFix);
}
else if(!(fd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY))
{
CString strFileName;
strFileName.Format("%s\\%s", strDir, fd.cFileName);
if (-1 != strFileName.Find(strPostFix))
{
if(m_nDirNum >= m_nDirBuffSize)
{
ReAllocDirBuff(2*m_nDirBuffSize);
}
*(m_pFileDir+m_nDirNum) = strFileName;
m_nDirNum++;
}
}
while(::FindNextFile(hSearch, &fd))
{
if( (fd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) && strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, "..") )
{
CString strNewDir;
strNewDir.Format("%s\\%s", strDir, fd.cFileName);
FindFileInDir(strNewDir, strPostFix);
}
else if(!(fd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY))
{
CString strFileName;
strFileName.Format("%s\\%s", strDir, fd.cFileName);
if (-1 != strFileName.Find(strPostFix))
{
if(m_nDirNum >= m_nDirBuffSize)
{
ReAllocDirBuff(2*m_nDirBuffSize);
}
*(m_pFileDir+m_nDirNum) = strFileName;
m_nDirNum++;
}
}
}
FindClose(hSearch);
}
void CFontSetConverterDlg::CheckDirectory(CString strDir)
{
CString tmpString;
CString tmpString2 = strDir;
CString tmpString3;
while(-1 != tmpString2.Find('\\'))
{
tmpString += tmpString2.Left(tmpString2.Find('\\'));
tmpString3 = tmpString2.Right(tmpString2.GetLength() - tmpString2.Find('\\')-1 );
CreateDirectory(tmpString, NULL);
tmpString2 = tmpString3;
tmpString += "\\";
}
tmpString += tmpString2;
CreateDirectory(tmpString, NULL);
}
// convert UTF8 TO GBK
void CFontSetConverterDlg::OnBtnUtf8ToGbk()
{
// TODO: Add your control notification handler code here
for(int i=0; i<m_nDirNum; i++)
{
ConvertUTF8ToGBK(m_pFileDir[i]);
}
AfxMessageBox("Finish!");
}
//-----------------------------------------------------------------------
// function name: ConvertUTF8ToGBK
// param:
// CString strDbf
// return:
//
// remark:
// convert dbf file from UTF8 to GBK
//
void CFontSetConverterDlg::ConvertUTF8ToGBK(CString strDbf)
{
CString strNewDbf = strDbf;
strNewDbf.Insert(strNewDbf.Find('\\')+1, "HH\\");
DBFFieldType eType;
int nWidth;
int nDecimals;
int nFieldNum;
char szFieldName[256];
int nRecNum;
CheckDirectory(strNewDbf.Left(strNewDbf.ReverseFind('\\')));
DBFHandle hSrcDbf = DBFOpen(strDbf.GetBuffer(strDbf.GetLength()), "rb");
DBFHandle hDestSrc = DBFCreate(strNewDbf.GetBuffer(strNewDbf.GetLength()));
nFieldNum = DBFGetFieldCount(hSrcDbf);
for(int i=0; i<nFieldNum; i++)
{
eType = DBFGetFieldInfo(hSrcDbf, i, szFieldName, &nWidth, &nDecimals);
DBFAddField(hDestSrc, szFieldName, eType, nWidth, nDecimals);
}
nRecNum = DBFGetRecordCount(hSrcDbf);
for(int ri=0; ri<nRecNum; ri++)
{
for(int fi=0; fi<nFieldNum; fi++)
{
eType = DBFGetFieldInfo(hSrcDbf, fi, szFieldName, &nWidth, &nDecimals);
if(eType==FTInteger && nWidth > 10)
{
eType = FTDouble;
}
switch(eType)
{
case FTInteger:
{
int nValue = DBFReadIntegerAttribute(hSrcDbf, ri, fi);
DBFWriteIntegerAttribute(hDestSrc, ri, fi, nValue);
break;
}
case FTDouble:
{
double dValue = DBFReadDoubleAttribute(hSrcDbf, ri, fi);
DBFWriteDoubleAttribute(hDestSrc, ri, fi, dValue);
break;
}
case FTLogical:
{
DBFWriteLogicalAttribute(hDestSrc, ri, fi, *(DBFReadLogicalAttribute(hSrcDbf, ri, fi)));
break;
}
case FTString:
{
char* szString = new char[nWidth + 2];
memset(szString, 0, nWidth+2);
strncpy(szString, DBFReadStringAttribute(hSrcDbf, ri, fi), nWidth);
char * pGBK_Half = CharUTF8ToGBK(szString);
DBFWriteStringAttribute(hDestSrc, ri, fi, pGBK_Half);
if(pGBK_Half)
delete [] pGBK_Half;
delete [] szString;
break;
}
case FTInvalid:
break;
default:
break;
}
}
}
DBFClose(hSrcDbf);
DBFClose(hDestSrc);
if(!DeleteFile(strDbf))
{
CString strWarning;
strWarning = "delete file failed, please delete file " + strDbf;
AfxMessageBox(strWarning);
}
if(!MoveFile(strNewDbf, strDbf))
{
CString strWarning;
strWarning = "delete file failed, please move file " + strNewDbf;
AfxMessageBox(strWarning);
}
}
//-----------------------------------------------------------------------
// function name: CharUTF8ToGBK
// param:
// char* strUTF8 UTF8 charactors
// return:
// GBK charactors
// remark:
// convert charactors from UTF8 to GBK
//
char* CFontSetConverterDlg::CharUTF8ToGBK(char* strUTF8)
{
int len = MultiByteToWideChar(CP_UTF8, 0, strUTF8, -1, NULL, 0);
unsigned short * wszGBK = new unsigned short[len + 1];
memset(wszGBK, 0, len * 2 + 2);
MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUTF8, -1, wszGBK, len);
len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL);
char *szGBK = new char[len + 1];
memset(szGBK, 0, len + 1);
WideCharToMultiByte(CP_ACP,0, wszGBK, -1, szGBK, len, NULL, NULL);
char *szGBK_Half = new char [len + 1];
memset(szGBK_Half, 0, len + 1);
LCMapString(LOCALE_USER_DEFAULT, LCMAP_HALFWIDTH, szGBK, strlen(szGBK)+1, szGBK_Half, len + 1);
delete[]szGBK;
delete[]wszGBK;
return szGBK_Half;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -