如何在VS 2013上运行旧的mfc程序

How to run old mfc program on VS 2013?

本文关键字:mfc 程序 运行 VS 2013      更新时间:2023-10-16

我想让这个程序在VS 2013上运行。放置以下内容无济于事:

#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS
#define USE_STANDARD_FILE_FUNCTIONS

我仍然得到:

1>------ Build started: Project: test, Configuration: Debug Win32 ------
1>  Code3C.cpp
1>c:program files (x86)microsoft visual studio 12.0vcatlmfcincludeafx.h(38): warning C4996:        'MBCS_Support_Deprecated_In_MFC': MBCS support in MFC is deprecated and may be removed in a future     version of MFC.
1>          c:program files (x86)microsoft visual studio 12.0vcatlmfcincludeafx.h(33) : see     declaration of 'MBCS_Support_Deprecated_In_MFC'
1>  _WIN32_WINNT not defined. Defaulting to _WIN32_WINNT_MAXVER (see WinSDKVer.h)
1>c:usersrevistdesktopsallcodesallcodecode3ccode3c.cpp(76): error C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          c:program files (x86)microsoft visual studio 12.0vcincludestdio.h(211) : see declaration of 'fopen'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

该计划:

CCode3.h:

#include <afxwin.h>
#include <afxdlgs.h>    //dialog boxes
#include "resource.h"
#define n 10
class CCode3C : public CFrameWnd
{
private:
CPoint *pt;
public:
CCode3C();
CCode3C::~CCode3C();
afx_msg void OnFileOpen();
afx_msg void OnFileSave();
afx_msg void OnGenerate();
afx_msg void OnClear();
afx_msg void OnExit();
DECLARE_MESSAGE_MAP()
};
class CMyWinApp : public CWinApp
{
public:
virtual BOOL InitInstance();
};

CCode3C.cpp

#include "Code3C.h"
CMyWinApp  MyApplication;
BOOL CMyWinApp::InitInstance()
{
CCode3C* pFrame = new CCode3C;
m_pMainWnd = pFrame;
pFrame->ShowWindow(SW_SHOW);
pFrame->UpdateWindow();
return TRUE;
}
BEGIN_MESSAGE_MAP(CCode3C,CFrameWnd)
ON_COMMAND(ID_FILEOPEN,OnFileOpen)
ON_COMMAND(ID_FILESAVE,OnFileSave)
ON_COMMAND(ID_GENERATE,OnGenerate)
ON_COMMAND(ID_CLEAR,OnClear)
ON_COMMAND(ID_EXIT,OnExit)
END_MESSAGE_MAP()
CCode3C::CCode3C()
{
Create(NULL, "Code3C: File menus",
    WS_OVERLAPPEDWINDOW,CRect(0,0,600,400),
    NULL,MAKEINTRESOURCE(IDR_MENU1));
pt=new CPoint [n+1];
}
CCode3C::~CCode3C()
{
delete pt;
}
void CCode3C::OnClear()
{
CClientDC dc(this);
CRect rc;
GetClientRect(&rc);
CBrush whiteBrush(RGB(255,255,255));
dc.FillRect(&rc,&whiteBrush);
for (int i=1;i<=n;i++)
    pt[i]=CPoint(0,0);
}
void CCode3C::OnGenerate()
{
CClientDC dc(this);
CString str;
time_t seed=time(NULL); 
srand((unsigned)seed);
OnClear();
dc.TextOut(50,50,"Generating Random Numbers");
for (int i=1;i<=n;i++)
{
    pt[i].x=100+rand()%400; pt[i].y=50+rand()%300;
    str.Format("%d   %d",pt[i].x,pt[i].y);
    dc.TextOut(50,80+20*i,str);
}
}
void CCode3C::OnFileOpen()
{
CClientDC dc(this);
CString str;
CRect rc;
FILE *ifp;
char strFilter[] = {"TXT Files (*.txt)|*.txt|All Files (*.*)|*.*||"};
CFileDialog FileDlg(TRUE,".txt",NULL,0,strFilter);
if (FileDlg.DoModal()==IDOK)
{
    str=FileDlg.GetFileName();
    ifp = fopen(str, "r");
    dc.TextOut(350,50,"File Opened: "+str);
    for (int i=1;i<=n;i++)
    {
        fscanf(ifp,"%d %d",&pt[i].x,&pt[i].y);
        rc=CRect(pt[i].x-30,pt[i].y-30,pt[i].x+30,pt[i].y+30);
        dc.Ellipse(rc);
        rc=CRect(pt[i].x-1,pt[i].y-1,pt[i].x+1,pt[i].y+1);
        dc.Rectangle(rc);
    }
    fclose(ifp);
}
}
void CCode3C::OnFileSave()
{
CClientDC dc(this);
CString str;
FILE *ofp;
char strFilter[] = {"TXT Files (*.txt)|*.txt|All Files (*.*)|*.*||"};
CFileDialog FileDlg(FALSE,".txt",NULL,0,strFilter);
if( FileDlg.DoModal()==IDOK)
{
    str=FileDlg.GetFileName();
    ofp=fopen(str,"w");
    dc.TextOut(50,20,"File Saved: "+str);
    str.Format("%d",n);
    dc.TextOut(50,50,"Contents: "+str+" randomly generated numbers");
    for (int i=1;i<=n;i++)
        fprintf(ofp,"%d  %dn",pt[i].x,pt[i].y);
    fclose(ofp);
}
}
void CCode3C::OnExit()
{
CCode3C::OnExit();
}

请记住,我是编程新手(大约 4 周前开始(,因此请回答以下问题之一:是的,我是认真的。

有时确实需要在当前的Visual Studio版本上运行旧的MBCS MFC代码。在这种情况下,在 http://msdn.microsoft.com/en-us/library/dn251007.aspx 下载 VS MBCS MFC 加载项应该可以解决您的问题。