初学Windows和MFC编程,试着写了一个程序,但是出First-chance Exception异常,不知道是怎么回事,各位能否帮忙分析下?
代码如下:#include <afxwin.h>
//由CFrameWnd派生的CMyWnd类
class CMyWnd:public CFrameWnd
{
private:
char *showtext;
public:
LPPOINT lppPoint;
public:
afx_msg void OnPaint();
afx_msg void OnLButtonDown();
DECLARE_MESSAGE_MAP(); //声明消息映射
};
//消息映射的实现
BEGIN_MESSAGE_MAP(CMyWnd,CFrameWnd)
ON_WM_PAINT()
ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()
//WM_PAINT消息处理函数的实现
void CMyWnd::OnPaint()
{
CPaintDC dc(this);
dc.TextOut(lppPoint->x,lppPoint->y,showtext);
}
void CMyWnd::OnLButtonDown()
{
showtext="Program with Message_Map!";
GetCursorPos(lppPoint);
ScreenToClient(lppPoint);
InvalidateRect(NULL,TRUE);
}
//程序员由CWinApp派生的应用程序类
class CMyApp:public CWinApp
{
public:
BOOL InitInstance();
CMyWnd *pMainWnd;
//~CMyApp(){delete pMainWnd->lppPoint;delete pMainWnd;} 顺便问问这里需要delete吗?加上会出错……
};
BOOL CMyApp::InitInstance()
{
pMainWnd=new CMyWnd;
pMainWnd->lppPoint=new tagPOINT;
pMainWnd->lppPoint->x=0;
pMainWnd->lppPoint->y=0;
pMainWnd->Create(0,"MFC");
pMainWnd->ShowWindow(m_nCmdShow);
pMainWnd->UpdateWindow();//First-chance Exception! why? 单步调试,这里出现FC Exception……
m_pMainWnd=pMainWnd;
return TRUE;
}
//定义CMyApp的对象MyApp
CMyApp MyApp;
代码如下:#include <afxwin.h>
//由CFrameWnd派生的CMyWnd类
class CMyWnd:public CFrameWnd
{
private:
char *showtext;
public:
LPPOINT lppPoint;
public:
afx_msg void OnPaint();
afx_msg void OnLButtonDown();
DECLARE_MESSAGE_MAP(); //声明消息映射
};
//消息映射的实现
BEGIN_MESSAGE_MAP(CMyWnd,CFrameWnd)
ON_WM_PAINT()
ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()
//WM_PAINT消息处理函数的实现
void CMyWnd::OnPaint()
{
CPaintDC dc(this);
dc.TextOut(lppPoint->x,lppPoint->y,showtext);
}
void CMyWnd::OnLButtonDown()
{
showtext="Program with Message_Map!";
GetCursorPos(lppPoint);
ScreenToClient(lppPoint);
InvalidateRect(NULL,TRUE);
}
//程序员由CWinApp派生的应用程序类
class CMyApp:public CWinApp
{
public:
BOOL InitInstance();
CMyWnd *pMainWnd;
//~CMyApp(){delete pMainWnd->lppPoint;delete pMainWnd;} 顺便问问这里需要delete吗?加上会出错……
};
BOOL CMyApp::InitInstance()
{
pMainWnd=new CMyWnd;
pMainWnd->lppPoint=new tagPOINT;
pMainWnd->lppPoint->x=0;
pMainWnd->lppPoint->y=0;
pMainWnd->Create(0,"MFC");
pMainWnd->ShowWindow(m_nCmdShow);
pMainWnd->UpdateWindow();//First-chance Exception! why? 单步调试,这里出现FC Exception……
m_pMainWnd=pMainWnd;
return TRUE;
}
//定义CMyApp的对象MyApp
CMyApp MyApp;
因为CMyWnd 类自身带会delete
CMyWnd结束时,自身也会调用自己的析构函数delete内存
这样你程序就退出了..
CRect rcClient;
GetClientRect(&rcClient);
CRuntimeClass* pRtCls = RUNTIME_CLASS(CProcFrame); // CProcFrame派生于CFrameWnd
m_pFrame = (CProcFrame*)pRtCls->CreateObject();
m_pFrame->Create(NULL,NULL,WS_CHILD|WS_VISIBLE,rcClient,this);
//...
顶。估计楼主是看基于Dialog的Application太多老的缘故,所以写出了那样的代码