我把项目从多字节编码改成Unicode编码就不能运行了,报错很多!主要的代码在底下,帮帮忙,谢谢了
主要的功能是打开txt文件,并显示到编辑框中,帮帮忙这不是我写的,我的项目是unicode编码集??
// readTextDlg.cpp : implementation file
// Dwonload by http://www.codefans.net#include "stdafx.h"
#include "readText.h"
#include "readTextDlg.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App Aboutclass 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()/////////////////////////////////////////////////////////////////////////////
// CReadTextDlg dialogCReadTextDlg::CReadTextDlg(CWnd* pParent /*=NULL*/)
: CDialog(CReadTextDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CReadTextDlg)
m_strText = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}void CReadTextDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CReadTextDlg)
DDX_Text(pDX, IDC_EDIT1, m_strText);
//}}AFX_DATA_MAP
}BEGIN_MESSAGE_MAP(CReadTextDlg, CDialog)
//{{AFX_MSG_MAP(CReadTextDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButtonReadText)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CReadTextDlg message handlersBOOL CReadTextDlg::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 CReadTextDlg::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 CReadTextDlg::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 CReadTextDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}void CReadTextDlg::OnButtonReadText() 
{
// TODO: Add your control notification handler code here
CFileDialog *lpszOpenFile;    //定义一个CfileDialog对象 CStdioFile file;
CString filePathName;
int row;
CString result;
//生成一个对话框
lpszOpenFile = new CFileDialog
(TRUE,"","",OFN_FILEMUSTEXIST |OFN_HIDEREADONLY , "文件类型(*.txt)|*.txt|所有文件(*.*)|*.*|");

if(lpszOpenFile->DoModal() == IDOK )//假如点击对话框确定按钮
{
filePathName = lpszOpenFile->GetPathName();//得到打开文件的路径
SetWindowText(filePathName);//在窗口标题上显示路径
} if(filePathName == "")  return; if(!file.Open(filePathName,CFile::modeRead))
{
MessageBox("can not open file!");
return;
}
CString strLine,strTemp;
row = 0;
while(file.ReadString(strLine))
{
char *str = strLine.GetBufferSetLength(strLine.GetLength());
char *p; if(strLine!="")
{
for (p=strtok(str,";");p!=NULL;p=strtok(NULL,";"))
{
strTemp = p;
result+=strTemp; }
row ++;
}
}
int wcsLen = ::MultiByteToWideChar(CP_UTF8, NULL, result, strlen( result), NULL, 0); 
    //分配空间要给'\0'留个空间,MultiByteToWideChar不会给'\0'空间 
    wchar_t* wszString = new wchar_t[wcsLen + 1]; 
    //转换 
    ::MultiByteToWideChar(CP_UTF8, NULL,  result, strlen( result), wszString, wcsLen); 
    //最后加上'\0' 
    wszString[wcsLen] = '\0'; 
    //unicode版的MessageBox API 
    ::MessageBoxW(GetSafeHwnd(), wszString, wszString, MB_OK); 

delete lpszOpenFile;//释放分配的对话框
m_strText = _T(wszString);
    UpdateData(false);

}

解决方案 »

  1.   

    好像编译器使用UNICODE编码的时候,所有的字符串都要改成_T("")这种形式的,我就看到你只有一个用到m_strText = _T("");
    ,其它都没用到,你干脆换成多字节算了
      

  2.   

    只要将类似于MessageBox("can not open file!");这些的写成:MessageBox(_T("can not open file!"));将char改成TCHAR就行了。
      

  3.   

    UNICODE  类型要
    把  char ---> TCHAR
    把  "aaa" --->_T("aaa");
    某些API函数要改为UNICODE版本的。等等