// MyUnicodeDlg.cpp : implementation file
//
//#pragma warning(disable:4089)
#include "stdafx.h"
#include "MyUnicode.h"
#include "MyUnicodeDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMyUnicodeDlg dialog
CMyUnicodeDlg::CMyUnicodeDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMyUnicodeDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMyUnicodeDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
//m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_Path=_T("");
}
void CMyUnicodeDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Text(pDX,FDIR,m_Path);
//{{AFX_DATA_MAP(CMyUnicodeDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMyUnicodeDlg, CDialog)
//{{AFX_MSG_MAP(CMyUnicodeDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//ON_BN_CLICKED(FINDDIR, OnFindDir)
ON_BN_CLICKED(FINDDIR,OnFileOpenEx)
ON_BN_CLICKED(CONVERTGO,OnConvertGo)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyUnicodeDlg message handlers
BOOL CMyUnicodeDlg::OnInitDialog()
{
CDialog::OnInitDialog();
//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
}
// 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 CMyUnicodeDlg::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 CMyUnicodeDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMyUnicodeDlg::OnFindDir()
{/*
CString str;
BROWSEINFO bi;
LPITEMIDLIST idl=NULL;
char name[MAX_PATH];
ZeroMemory(&bi,sizeof(BROWSEINFO));
bi.pidlRoot = idl;
bi.hwndOwner=GetSafeHwnd();
bi.pszDisplayName=name;
bi.lpszTitle="浏览文件夹";
bi.lpfn=NULL;
bi.ulFlags=BIF_BROWSEINCLUDEFILES|BIF_EDITBOX |BIF_RETURNONLYFSDIRS;
idl=SHBrowseForFolder(&bi);
if(idl==NULL)
return;
SHGetPathFromIDList(idl,str.GetBuffer(MAX_PATH));
str.ReleaseBuffer();
m_Path=str;
if(str.GetAt(str.GetLength()-1)!='\\')
{
m_Path+="\\";
}
UpdateData(FALSE);
*/
}
void CMyUnicodeDlg::OnFileOpenEx_Error()
{/*
CString pathName ;
DWORD size;
char temp[1000];
CFileDialog mFileDlg(TRUE, NULL,NULL,NULL ,"All Files (*.*)|*.*||", AfxGetMainWnd());
const int OFN_DONTADDTORECENT=0x2000000;
mFileDlg.m_ofn.lStructSize=sizeof(OPENFILENAME);
mFileDlg.m_ofn.hwndOwner=AfxGetMainWnd()->m_hWnd;
mFileDlg.m_ofn.hInstance=AfxGetInstanceHandle();
//mFileDlg.m_ofn.lpstrCustomFilter=NULL;
//mFileDlg.m_ofn.nMaxCustFilter=0;
//mFileDlg.m_ofn.nFilterIndex=0;
mFileDlg.m_ofn.nMaxFile=MAX_PATH;
mFileDlg.m_ofn.nMaxFileTitle=MAX_PATH;
//mFileDlg.m_ofn.lpstrInitialDir=NULL;
//mFileDlg.m_ofn.lpstrFileTitle=NULL;
mFileDlg.m_ofn.Flags=OFN_CREATEPROMPT|OFN_HIDEREADONLY|OFN_DONTADDTORECENT|OFN_EXPLORER;
//mFileDlg.m_ofn.nFileOffset=0;
//mFileDlg.m_ofn.nFileExtension=0;
//mFileDlg.m_ofn.lCustData=0L;
//mFileDlg.m_ofn.lpfnHook=NULL;
//mFileDlg.m_ofn.lpTemplateName=NULL;
mFileDlg.m_ofn.lpstrDefExt=TEXT("ppp");
mFileDlg.m_ofn.lpstrTitle="打开文件";
mFileDlg.m_ofn.lpstrFilter="文件(*.*)\0*.*\0\0";
//strcpy(mFileDlg.m_ofn.lpstrFile,mFileDlg.GetPathName());
if(mFileDlg.DoModal()==IDOK)
{
pathName=mFileDlg.GetPathName();
}
else
{
return ;
}
if(pathName)
{
CFile file(pathName,CFile::modeRead);
CArchive ar(&file,CArchive::load);
{
size = ar.Read((void*)temp,sizeof(1000));
}
}
*/
}
void CMyUnicodeDlg::OnFileOpenEx()
{
OPENFILENAME ofn;
int openok=FALSE;//标识是否在公用对话中按下确定
const int OFN_DONTADDTORECENT=0x2000000;
memset(&ofn,0, sizeof(OPENFILENAME));
ofn.lStructSize=sizeof(OPENFILENAME);
ofn.hwndOwner=AfxGetMainWnd()->m_hWnd;
ofn.hInstance=AfxGetInstanceHandle();
ofn.lpstrCustomFilter=NULL;
ofn.nMaxCustFilter=0;
ofn.nFilterIndex=0;
ofn.nMaxFile=MAX_PATH;
ofn.nMaxFileTitle=MAX_PATH;
ofn.lpstrInitialDir=NULL;
ofn.lpstrFileTitle=NULL;
ofn.Flags=OFN_CREATEPROMPT/*|OFN_ENABLESIZING*/|OFN_EXPLORER|OFN_DONTADDTORECENT;
ofn.nFileOffset=0;
ofn.nFileExtension=0;
ofn.lCustData=0;
ofn.lpfnHook=NULL;
ofn.lpTemplateName=NULL;
ofn.lpstrDefExt=NULL;
ofn.lpstrTitle="打开文件";
ofn.lpstrFilter="文件(*.*)\0*.*\0\0";
ofn.lpstrFile=m_Path.GetBuffer(MAX_PATH);
openok=GetOpenFileName(&ofn);
if(openok)
{
}
else
{
m_Path.ReleaseBuffer();
return;
}
m_Path.ReleaseBuffer();
UpdateData(FALSE);
}
void CMyUnicodeDlg::OnConvertGo()
{
HANDLE hFile;
DWORD dwRead;
DWORD dwFileSize;
DWORD uninum;
int n,m,t,tn;
//unsigned char fhead[2];
char *ff;
wchar_t *unicodeff;
CString pathend;
short bRet = (short)0xFEFF;
CFileFind find;
if(!find.FindFile(m_Path))
{//该文件不存在
find.Close();
return;
}
find.Close();
//-------------------------------------------------
pathend=m_Path;
n=pathend.ReverseFind('\\');
if(n==-1)
{//找不到
return;
}
m=pathend.ReverseFind('.');
if((m!=-1)&&(m>n))
{
pathend.Insert(m,'_');
t=m+1;
}
else
{
pathend+=_T("_");
t=pathend.GetLength();
}
tn=0;
while(find.FindFile(pathend))
{//已存在该文件
pathend.Insert(t,'_');
t++;
tn++;
if(tn>10000)
{
find.Close();
return;
}
}
find.Close();
//--------------------------------------------------
hFile=CreateFile(m_Path,GENERIC_READ,FILE_SHARE_READ,NULL,
OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL|FILE_FLAG_SEQUENTIAL_SCAN,NULL);
dwFileSize = GetFileSize(hFile, NULL);
if(!dwFileSize)
{
CloseHandle(hFile);
return;
}
ff=new char[dwFileSize];
SetFilePointer(hFile,0,0,FILE_BEGIN);
ReadFile(hFile,(LPBYTE)ff,dwFileSize,&dwRead,NULL);
CloseHandle(hFile);
uninum=MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED|MB_USEGLYPHCHARS,ff,-1,NULL,0);
uninum=dwFileSize;
unicodeff=new wchar_t[uninum];
//CString ssss;ssss.Format("%d,%d",dwFileSize,uninum);//----------------------------------
//MessageBox(ssss,NULL,MB_OK);//-------------------------
MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED|MB_USEGLYPHCHARS,ff,dwFileSize,unicodeff,uninum);//MB_PRECOMPOSED
hFile=CreateFile(pathend,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ,NULL,
CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL|FILE_FLAG_SEQUENTIAL_SCAN,NULL);
WriteFile(hFile,(BYTE*)&bRet,2,&dwRead,NULL);
WriteFile(hFile,(BYTE*)unicodeff,uninum*2,&dwRead,NULL);
CloseHandle(hFile);
delete []unicodeff;
delete []ff;
}