16,472
社区成员
发帖
与我相关
我的任务
分享
#ifndef USE_NEW
CMainFrame cMyWnd;//
MSG msg;
//m_pMainWnd=&cMyWnd;
cMyWnd.ShowWindow(m_nCmdShow);
cMyWnd.UpdateWindow();
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return FALSE;
// 本程序只是测试,说明堆和栈产生对象的不同。
#include <afxwin.h>
class CMyApp : public CWinApp
{
public:
virtual BOOL InitInstance ();
};
class CMainFrame : public CFrameWnd
{
public:
CMainFrame ();
~CMainFrame();
CPoint m_ptCaretPos;
CStatic m_wndPushButton;
CWnd* pWnd;
protected:
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnPaint ();
afx_msg void OnSetFocus();
afx_msg void OnKillFocus();
DECLARE_MESSAGE_MAP ()
};
///////
CMyApp theApp;
#define USE_NEW
///////////////////////////////////////////////////////////
BOOL CMyApp::InitInstance ()
{
#ifndef USE_NEW
CMainFrame cMyWnd;//
//m_pMainWnd=&cMyWnd;
cMyWnd.ShowWindow(m_nCmdShow);
cMyWnd.UpdateWindow();
while (Run());
return FALSE;
#else
m_pMainWnd = new CMainFrame;
m_pMainWnd->ShowWindow (m_nCmdShow);
m_pMainWnd->UpdateWindow ();
return TRUE;
#endif
}
/////////////////////////////////////////////////////////////////////////
// CMainFrame message map and member functions
BEGIN_MESSAGE_MAP (CMainFrame, CFrameWnd)
ON_WM_CREATE()
ON_WM_PAINT ()
ON_WM_SETFOCUS()
ON_WM_KILLFOCUS()
END_MESSAGE_MAP ()
CMainFrame::CMainFrame ():m_ptCaretPos(0)
{
Create(NULL, _T ("The Hello Application"));
}
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
pWnd=new CWnd;//WS_BORDER |
pWnd->Create(NULL, _T("A CWnd"), WS_SYSMENU | WS_CHILD | WS_CAPTION | WS_VISIBLE, CRect(40, 50, 300, 350), this, 0);//x11);
ASSERT(pWnd->GetSafeHwnd());
m_wndPushButton.Create(_T("STATIC"), WS_CHILD | WS_VISIBLE,CRect(400, 80, 500, 180), this, 1234);
//
return 1;
}
void CMainFrame::OnPaint ()
{
CPaintDC dc (this);
CRect rect;
GetClientRect (&rect);
dc.DrawText (_T ("CMainFrame:Hello, MFC"), -1, &rect,DT_SINGLELINE | DT_CENTER | DT_VCENTER);
dc.SetTextAlign(TA_CENTER|TA_BOTTOM);
// dc.TextOut(300,40,_T("YIRUIRUI"));
}
void CMainFrame::OnSetFocus()
{
CreateSolidCaret(12,35);
SetCaretPos(m_ptCaretPos);
ShowCaret();
}
void CMainFrame::OnKillFocus()
{
HideCaret();
m_ptCaretPos=GetCaretPos();
::DestroyCaret();
}
CMainFrame::~CMainFrame()
{
//在析构函数中销毁在堆上分配的内存空间
if(pWnd->GetSafeHwnd())
{
pWnd->DestroyWindow();
}
delete pWnd;
pWnd=NULL;
}
BOOL CMyApp::InitInstance ()
{
CMainWindow cMyWnd;
m_pMainWnd=&cMyWnd;
cMyWnd.Create (NULL, _T ("The Hello Application"));
cMyWnd.ShowWindow(m_nCmdShow);
cMyWnd.UpdateWindow();
cMyWnd.Detach();
GetLastError();
return TRUE;
}