16,472
社区成员
发帖
与我相关
我的任务
分享
// MFCView.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
class CMyView : public CView
{
public:
virtual void OnDraw( CDC* pDC );
DECLARE_MESSAGE_MAP( )
public:
afx_msg void OnPaint( );
afx_msg void OnTest( );
};
BEGIN_MESSAGE_MAP( CMyView, CView )
ON_WM_PAINT( )
ON_COMMAND( ID_TEST, OnTest )
END_MESSAGE_MAP( )
void CMyView::OnTest( )
{
AfxMessageBox( "OnTest" );
}
void CMyView::OnPaint( )
{
PAINTSTRUCT ps = { 0 };
HDC hDC = ::BeginPaint( m_hWnd, &ps );
TextOut( hDC, 100, 100, "View", 4 );
::EndPaint( m_hWnd, &ps );
}
void CMyView::OnDraw( CDC * pDC )
{
pDC->TextOut( 100, 100, "Hello View");
}
class CViewFrame : public CFrameWnd
{
public:
CViewFrame( );
virtual BOOL OnCmdMsg( UINT nID, int nCode,
void* pExtra,
AFX_CMDHANDLERINFO* pHandlerInfo );
DECLARE_MESSAGE_MAP( )
public:
afx_msg int OnCreate( LPCREATESTRUCT lpCreateStruct );
public:
CMyView * m_pwndView;
//*请看这里,我们创建View的时候使用的是指针方式。没有采用 (类 对象;)方式。
老师说这里不能写成CMyView m_pwndView; (类 对象;)这种方式 因为在PostNcDestory()函数中有一个
delete this;语句,所以为了防止内存释放掉两次 这里不能使用CMyView m_pwndView;而必须使用
CMyView * m_pwndView; 但是为什么在MFC使用向导创建出来的程序中就是用了 :
protected: // control bar embedded members
CStatusBar m_wndStatusBar;
CToolBar m_wndToolBar;
CChildView m_wndView;
我们可以看到:
CChildView m_wndView;这种方式 为什么这种方式可以??????????????
*/
};
BEGIN_MESSAGE_MAP( CViewFrame, CFrameWnd )
ON_WM_CREATE( )
END_MESSAGE_MAP( )
CViewFrame::CViewFrame( )
{
m_pwndView = NULL;
}
int CViewFrame::OnCreate(LPCREATESTRUCT lpCreateStruct )
{
CFrameWnd::OnCreate( lpCreateStruct );
//View对象
m_pwndView = new CMyView( );
//创建View
m_pwndView->Create( NULL, "View",
WS_CHILD|WS_VISIBLE|WS_BORDER,
CRect( 100, 100, 200, 200 ),
this, AFX_IDW_PANE_FIRST );
SetActiveView( m_pwndView );
//ModifyStyle( WS_BORDER )
ModifyStyleEx( WS_EX_CLIENTEDGE, 0 );
return 0;
}
BOOL CViewFrame::OnCmdMsg( UINT nID, int nCode,
void* pExtra,
AFX_CMDHANDLERINFO* pHandlerInfo )
{
if( NULL != m_pwndView )
{
if( TRUE == m_pwndView->OnCmdMsg( nID,
nCode, pExtra, pHandlerInfo ) )
{
return TRUE;
}
}
return CFrameWnd::OnCmdMsg( nID, nCode,
pExtra, pHandlerInfo );
}
class CViewApp : public CWinApp
{
public:
virtual BOOL InitInstance( );
};
CViewApp theApp;
BOOL CViewApp::InitInstance( )
{
CViewFrame * pWnd = new CViewFrame( );
pWnd->Create( NULL, "ViewApp",
WS_OVERLAPPEDWINDOW,
CFrameWnd::rectDefault, NULL,
MAKEINTRESOURCE( IDR_MAINFRM ) );
m_pMainWnd = pWnd;
m_pMainWnd->ShowWindow( SW_SHOW );
m_pMainWnd->UpdateWindow( );
return TRUE;
}
[/quote]
销毁cmyview的代码在哪里// MFCView.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
class CMyView : public CView
{
public:
virtual void OnDraw( CDC* pDC );
DECLARE_MESSAGE_MAP( )
public:
afx_msg void OnPaint( );
afx_msg void OnTest( );
};
BEGIN_MESSAGE_MAP( CMyView, CView )
ON_WM_PAINT( )
ON_COMMAND( ID_TEST, OnTest )
END_MESSAGE_MAP( )
void CMyView::OnTest( )
{
AfxMessageBox( "OnTest" );
}
void CMyView::OnPaint( )
{
PAINTSTRUCT ps = { 0 };
HDC hDC = ::BeginPaint( m_hWnd, &ps );
TextOut( hDC, 100, 100, "View", 4 );
::EndPaint( m_hWnd, &ps );
}
void CMyView::OnDraw( CDC * pDC )
{
pDC->TextOut( 100, 100, "Hello View");
}
class CViewFrame : public CFrameWnd
{
public:
CViewFrame( );
virtual BOOL OnCmdMsg( UINT nID, int nCode,
void* pExtra,
AFX_CMDHANDLERINFO* pHandlerInfo );
DECLARE_MESSAGE_MAP( )
public:
afx_msg int OnCreate( LPCREATESTRUCT lpCreateStruct );
public:
CMyView * m_pwndView;
//*请看这里,我们创建View的时候使用的是指针方式。没有采用 (类 对象;)方式。
老师说这里不能写成CMyView m_pwndView; (类 对象;)这种方式 因为在PostNcDestory()函数中有一个
delete this;语句,所以为了防止内存释放掉两次 这里不能使用CMyView m_pwndView;而必须使用
CMyView * m_pwndView; 但是为什么在MFC使用向导创建出来的程序中就是用了 :
protected: // control bar embedded members
CStatusBar m_wndStatusBar;
CToolBar m_wndToolBar;
CChildView m_wndView;
我们可以看到:
CChildView m_wndView;这种方式 为什么这种方式可以??????????????
*/
};
BEGIN_MESSAGE_MAP( CViewFrame, CFrameWnd )
ON_WM_CREATE( )
END_MESSAGE_MAP( )
CViewFrame::CViewFrame( )
{
m_pwndView = NULL;
}
int CViewFrame::OnCreate(LPCREATESTRUCT lpCreateStruct )
{
CFrameWnd::OnCreate( lpCreateStruct );
//View对象
m_pwndView = new CMyView( );
//创建View
m_pwndView->Create( NULL, "View",
WS_CHILD|WS_VISIBLE|WS_BORDER,
CRect( 100, 100, 200, 200 ),
this, AFX_IDW_PANE_FIRST );
SetActiveView( m_pwndView );
//ModifyStyle( WS_BORDER )
ModifyStyleEx( WS_EX_CLIENTEDGE, 0 );
return 0;
}
BOOL CViewFrame::OnCmdMsg( UINT nID, int nCode,
void* pExtra,
AFX_CMDHANDLERINFO* pHandlerInfo )
{
if( NULL != m_pwndView )
{
if( TRUE == m_pwndView->OnCmdMsg( nID,
nCode, pExtra, pHandlerInfo ) )
{
return TRUE;
}
}
return CFrameWnd::OnCmdMsg( nID, nCode,
pExtra, pHandlerInfo );
}
class CViewApp : public CWinApp
{
public:
virtual BOOL InitInstance( );
};
CViewApp theApp;
BOOL CViewApp::InitInstance( )
{
CViewFrame * pWnd = new CViewFrame( );
pWnd->Create( NULL, "ViewApp",
WS_OVERLAPPEDWINDOW,
CFrameWnd::rectDefault, NULL,
MAKEINTRESOURCE( IDR_MAINFRM ) );
m_pMainWnd = pWnd;
m_pMainWnd->ShowWindow( SW_SHOW );
m_pMainWnd->UpdateWindow( );
return TRUE;
}
[/quote]
销毁cmyview的代码在哪里[/quote]
前辈需要销毁代码???既然构造了,那么系统不是会自动的提供析构函数吗??所以不需要销毁代码,是这样吧!!!// MFCView.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
class CMyView : public CView
{
public:
virtual void OnDraw( CDC* pDC );
DECLARE_MESSAGE_MAP( )
public:
afx_msg void OnPaint( );
afx_msg void OnTest( );
};
BEGIN_MESSAGE_MAP( CMyView, CView )
ON_WM_PAINT( )
ON_COMMAND( ID_TEST, OnTest )
END_MESSAGE_MAP( )
void CMyView::OnTest( )
{
AfxMessageBox( "OnTest" );
}
void CMyView::OnPaint( )
{
PAINTSTRUCT ps = { 0 };
HDC hDC = ::BeginPaint( m_hWnd, &ps );
TextOut( hDC, 100, 100, "View", 4 );
::EndPaint( m_hWnd, &ps );
}
void CMyView::OnDraw( CDC * pDC )
{
pDC->TextOut( 100, 100, "Hello View");
}
class CViewFrame : public CFrameWnd
{
public:
CViewFrame( );
virtual BOOL OnCmdMsg( UINT nID, int nCode,
void* pExtra,
AFX_CMDHANDLERINFO* pHandlerInfo );
DECLARE_MESSAGE_MAP( )
public:
afx_msg int OnCreate( LPCREATESTRUCT lpCreateStruct );
public:
CMyView * m_pwndView;
//*请看这里,我们创建View的时候使用的是指针方式。没有采用 (类 对象;)方式。
老师说这里不能写成CMyView m_pwndView; (类 对象;)这种方式 因为在PostNcDestory()函数中有一个
delete this;语句,所以为了防止内存释放掉两次 这里不能使用CMyView m_pwndView;而必须使用
CMyView * m_pwndView; 但是为什么在MFC使用向导创建出来的程序中就是用了 :
protected: // control bar embedded members
CStatusBar m_wndStatusBar;
CToolBar m_wndToolBar;
CChildView m_wndView;
我们可以看到:
CChildView m_wndView;这种方式 为什么这种方式可以??????????????
*/
};
BEGIN_MESSAGE_MAP( CViewFrame, CFrameWnd )
ON_WM_CREATE( )
END_MESSAGE_MAP( )
CViewFrame::CViewFrame( )
{
m_pwndView = NULL;
}
int CViewFrame::OnCreate(LPCREATESTRUCT lpCreateStruct )
{
CFrameWnd::OnCreate( lpCreateStruct );
//View对象
m_pwndView = new CMyView( );
//创建View
m_pwndView->Create( NULL, "View",
WS_CHILD|WS_VISIBLE|WS_BORDER,
CRect( 100, 100, 200, 200 ),
this, AFX_IDW_PANE_FIRST );
SetActiveView( m_pwndView );
//ModifyStyle( WS_BORDER )
ModifyStyleEx( WS_EX_CLIENTEDGE, 0 );
return 0;
}
BOOL CViewFrame::OnCmdMsg( UINT nID, int nCode,
void* pExtra,
AFX_CMDHANDLERINFO* pHandlerInfo )
{
if( NULL != m_pwndView )
{
if( TRUE == m_pwndView->OnCmdMsg( nID,
nCode, pExtra, pHandlerInfo ) )
{
return TRUE;
}
}
return CFrameWnd::OnCmdMsg( nID, nCode,
pExtra, pHandlerInfo );
}
class CViewApp : public CWinApp
{
public:
virtual BOOL InitInstance( );
};
CViewApp theApp;
BOOL CViewApp::InitInstance( )
{
CViewFrame * pWnd = new CViewFrame( );
pWnd->Create( NULL, "ViewApp",
WS_OVERLAPPEDWINDOW,
CFrameWnd::rectDefault, NULL,
MAKEINTRESOURCE( IDR_MAINFRM ) );
m_pMainWnd = pWnd;
m_pMainWnd->ShowWindow( SW_SHOW );
m_pMainWnd->UpdateWindow( );
return TRUE;
}