-------------
Sample01.h
class CSample01App:public CWinApp
{
public :
virtual BOOL InitInstance();};class CSample01Frame:public CFrameWnd
{
public :
CSampele01Frame();
protected:
afx_msg void OnPaint();
DECLARE_MESSAGE_MAP()
};---------------------
Sample01.cpp
#include<afxwin.h>
#include"Sample01.h"CSample01App theApp;
BOOL CSample01App::InitInstance()
{
m_pMainWnd=new CSample01Frame();
m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();
return TRUE;
}BEGIN_MESSAGE_MAP(CSample01Frame,CFrameWnd)
   ON_WM_PAINT()
END_MESSAGE_MAP()CSample01Frame::CSampele01Frame()
{
Create(NULL,"MFC应用程序框架");
}void CSample01Frame::OnPaint ()
{
CPaintDC dc(this);
dc.TextOut(100,100,"Hello World!");
}

解决方案 »

  1.   

    m_pMainWnd- >ShowWindow(m_nCmdShow); 在这之前要创建窗体, 构造函数应该没创建成功
    Create(NULL,"MFC应用程序框架"); 
      

  2.   

    CWnd( );
    Constructs a CWnd object.The Windows window is not created and attached until the CreateEx or Create member function is called.
    也就是说,调用CWnd()并不能真正的创建对话框。Returns m_hWnd, or NULL if the this pointer is NULL.
    HWND GetSafeHwnd( ) const;
    Return Value
    Returns the window handle for a window. Returns NULL if the CWnd is not attached to a window or if it is used with a NULL CWnd pointer. 
    这个是对m_hWnd的解释。
    也就是说如果没有调用CreateEx,那么m_hWnd始终是NULL,所以没有办法使用。 
    m_pMainWnd=new CSample01Frame();
    m_pMainWnd->Create(NULL,"MFC应用程序框架");
    m_pMainWnd- >ShowWindow(m_nCmdShow);
    m_pMainWnd- >UpdateWindow(); 
      

  3.   

    Create的解释:
    You construct a child window in two steps. First, call the constructor, which constructs the CWnd object. Then call Create, which creates the Windows child window and attaches it to CWnd. Create initializes the window's class name and window name and registers values for its style, parent,
    and ID.也就是说必须是先调用了constructor,而LZ的Create(NULL,"MFC应用程序框架")方法就在constructor中,这种
    逻辑肯定是错了。
      

  4.   

    怎么阅读次数是42,但回帖的只有几个呢?用向导生成可以,但是只有这样自己写代码才能明白MFC的封装机制和运行机理啊你们是怎么学习vc的呢?
      

  5.   

    re:1,2 楼:
    m_pMainWnd=new CSample01Frame(); 
    不是就在创建一个窗体吗?我先做个测试试试re:3 楼:单纯的这样是不行的:
    m_pMainWnd=new CSample01Frame(); 
    m_pMainWnd- >Create(NULL,"MFC应用程序框架"); 
    m_pMainWnd-  >ShowWindow(m_nCmdShow); 
    m_pMainWnd-  >UpdateWindow(); 
    原来是在CFrameWnd里 调用Create,他只接受两个参数,其他均默认即可,但是现在用m_pMainWnd调用的话就
    相当于在CWnd里的Create函数,这里不能那么多的缺省参数。
      

  6.   

    我将代码改成这样:
    m_pMainWnd=new CSample01Frame(); 
    m_pMainWnd->Create("abc","zhangyanli",WS_OVERLAPPEDWINDOW,CRect(0,0,100,200),NULL,0);
    m_pMainWnd->ShowWindow(m_nCmdShow); 
    m_pMainWnd->UpdateWindow();还是不行,急哭了 都......
    帮帮忙啊!!!!
      

  7.   

    m_pMainWnd- >Create(NULL,"zhangyanli",WS_OVERLAPPEDWINDOW,CRect(0,0,100,200),NULL,0); 
      

  8.   

    高手在哪里啊,你们都没碰到过这样的问题吗?
    你们怎么学的mfc 啊
      

  9.   

    LZ如果知道是家伙在捣鬼................
    class CSample01Frame:public CFrameWnd
    {
    public :
    CSampele01Frame();//注意这里啊,仔细看
    protected:
    afx_msg void OnPaint();
    DECLARE_MESSAGE_MAP()
    }; 
      

  10.   

    首先:使用这种方法只能创建子窗口,也就是说必须要有父窗口的情况下才能创建,而在m_pMainWnd没有创建的情况下,父窗口不存在,所以没有办法这样处理。至于在其他的情况下,为什么就可以这样来创建,我想主要是因为其中都已经有了父窗口吧。这只是我的意见,需要高手解决。
      

  11.   

    m_pMainWnd= new CSample01Frame; 
    ((CSample01Frame*)m_pMainWnd)->Create(NULL, "MFC应用程序框架");
    m_pMainWnd->ShowWindow(m_nCmdShow); 
    m_pMainWnd->UpdateWindow(); 
    return TRUE; 不要在构造函数中调用Create
      

  12.   

    我在这里郑重的向大家道歉,是我的疏忽造成的,tiantangniao232 指出的很对,是那里出了问题.
    但绝对不是在诚心捣鬼,抱歉抱歉.也谢谢楼上的几位.另外,to:shanhqk:
    首先谢谢你,在我的构造函数中是可以用Create的,其实在这里并不是真正的调用MFC,而是我们自己调用MFC框架里的一些函数
    然后自己封装的类,而在MFC框架里可能是你说的那样Create不能放在constructor里.是该好好自己检讨一下了,呵呵,,,