| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1301 人关注过本帖
标题:MFC 新手,求指点迷津
只看楼主 加入收藏
聪儿
Rank: 1
等 级:新手上路
帖 子:75
专家分:3
注 册:2012-9-2
结帖率:77.78%
收藏
已结贴  问题点数:20 回复次数:6 
MFC 新手,求指点迷津
初学MFC 敲了一个教程上面的程序上去,按照要求建立工程文件和设置“使用MFC类库”于是就出现了如下一堆错误,恳请知道!拜谢!

同时,请各位豪杰指导下MFC的编程技巧和学习方法,感激…………
#include <afxwin.h>
 
class CMyWnd:public CFrameWnd
{
private:
    char * Showtext;
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()

void CMyWnd::OnPaint()
{
    CPaintDC dc(this);
    dc.TextOut(20,20,Showtext);
}

void CMyWnd::OnLButtonDown()
{
    Showtext="my first try";
    InvalidateRect(NULL,TRUE);
}

class CMyApp::public CWinApp
{
public:
    BOOL InitInstance();
};

BOOL CMyApp::InitInstance()
{
    CMyWnd * pMainWnd=new CMyWnd;
    pMainWnd->Create(0,"MFC");
    pMainWnd->ShowWindow(m_nCmdShow);
    pMainWnd->UpdateWindow();
    m_pMainWnd=pMainWnd;
    return true;
}

CMyApp MyApp;

编译完整错误提示如下:
--------------------Configuration: 0 - Win32 Debug--------------------
Compiling...
0.cpp
D:\Program Files\DAEMON Tools Lite\0\0.cpp(14) : error C2065: 'On_WM_PAINT' : undeclared identifier
D:\Program Files\DAEMON Tools Lite\0\0.cpp(15) : error C2061: syntax error : identifier 'On_WM_LBUTTONDOWN'
D:\Program Files\DAEMON Tools Lite\0\0.cpp(16) : error C2143: syntax error : missing ';' before '{'
D:\Program Files\DAEMON Tools Lite\0\0.cpp(16) : error C2447: missing function header (old-style formal list?)
D:\Program Files\DAEMON Tools Lite\0\0.cpp(16) : error C2143: syntax error : missing ';' before '}'
D:\Program Files\DAEMON Tools Lite\0\0.cpp(30) : error C2653: 'CMyApp' : is not a class or namespace name
D:\Program Files\DAEMON Tools Lite\0\0.cpp(30) : error C2589: 'public' : illegal token on right side of '::'
D:\Program Files\DAEMON Tools Lite\0\0.cpp(30) : error C2589: 'public' : illegal token on right side of '::'
D:\Program Files\DAEMON Tools Lite\0\0.cpp(30) : error C2589: 'public' : illegal token on right side of '::'
D:\Program Files\DAEMON Tools Lite\0\0.cpp(30) : error C2143: syntax error : missing ';' before '::'
D:\Program Files\DAEMON Tools Lite\0\0.cpp(30) : error C2143: syntax error : missing ';' before '::'
D:\Program Files\DAEMON Tools Lite\0\0.cpp(31) : error C2143: syntax error : missing ';' before '{'
D:\Program Files\DAEMON Tools Lite\0\0.cpp(31) : error C2447: missing function header (old-style formal list?)
D:\Program Files\DAEMON Tools Lite\0\0.cpp(36) : error C2027: use of undefined type 'CMyApp'
        D:\Program Files\DAEMON Tools Lite\0\0.cpp(30) : see declaration of 'CMyApp'
D:\Program Files\DAEMON Tools Lite\0\0.cpp(39) : error C2018: unknown character '0xa3'
D:\Program Files\DAEMON Tools Lite\0\0.cpp(39) : error C2018: unknown character '0xac'
D:\Program Files\DAEMON Tools Lite\0\0.cpp(39) : error C2143: syntax error : missing ')' before 'string'
D:\Program Files\DAEMON Tools Lite\0\0.cpp(39) : error C2660: 'Create' : function does not take 1 parameters
D:\Program Files\DAEMON Tools Lite\0\0.cpp(39) : error C2059: syntax error : ')'
D:\Program Files\DAEMON Tools Lite\0\0.cpp(40) : error C2065: 'm_nCmdShow' : undeclared identifier
D:\Program Files\DAEMON Tools Lite\0\0.cpp(42) : error C2065: 'm_pMainWnd' : undeclared identifier
D:\Program Files\DAEMON Tools Lite\0\0.cpp(42) : error C2440: '=' : cannot convert from 'class CMyWnd *' to 'int'
        This conversion requires a reinterpret_cast, a C-style cast or function-style cast
D:\Program Files\DAEMON Tools Lite\0\0.cpp(46) : error C2079: 'MyApp' uses undefined class 'CMyApp'
执行 cl.exe 时出错.

0.obj - 1 error(s), 0 warning(s)


搜索更多相关主题的帖子: public include private void 
2012-09-02 23:39
遗矢的老人
Rank: 9Rank: 9Rank: 9
来 自:成都
等 级:蜘蛛侠
威 望:7
帖 子:325
专家分:1131
注 册:2012-7-20
收藏
得分:0 
C++啊,一看提示就是没加头文件,还有那些丢失分号什么的自己跟着意思改撒

[ 本帖最后由 遗矢的老人 于 2012-9-3 00:14 编辑 ]
2012-09-03 00:10
newdos
Rank: 9Rank: 9Rank: 9
等 级:禁止访问
威 望:6
帖 子:251
专家分:1169
注 册:2012-8-13
收藏
得分:20 
#include <afxwin.h>

class CMyWnd:public CFrameWnd
{
private:
    char * Showtext;
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()

void CMyWnd::OnPaint()
{
    CPaintDC dc(this);
    dc.TextOut(20,20,Showtext);
}

void CMyWnd::OnLButtonDown()
{
    Showtext="my first try";
    InvalidateRect(NULL,TRUE);
}

class CMyApp : public CWinApp //这里是单冒号,不是双冒号
{
public:
    BOOL InitInstance();
};

BOOL CMyApp::InitInstance()
{
    CMyWnd * pMainWnd=new CMyWnd;
    pMainWnd->Create(0, "MFC"); //这里打成中文的逗号了。
    pMainWnd->ShowWindow(m_nCmdShow);
    pMainWnd->UpdateWindow();
    m_pMainWnd=pMainWnd;
    return true;
}

CMyApp myApp;

[ 本帖最后由 newdos 于 2012-9-3 11:33 编辑 ]
2012-09-03 11:30
聪儿
Rank: 1
等 级:新手上路
帖 子:75
专家分:3
注 册:2012-9-2
收藏
得分:0 
回复 3楼 newdos
嗯, 我也刚刚发现。。

真是粗心之极,非常感谢!!
2012-09-03 15:50
聪儿
Rank: 1
等 级:新手上路
帖 子:75
专家分:3
注 册:2012-9-2
收藏
得分:0 
想要再问一个问题:
afx_msg void OnPaint();
和后面消息映射表实现中的
ON_WM_PAINT(),
关系是什么??

我自己理解是 默认对应的关系,即在消息映射表中的默认对应。

由windows识别动作ON_WM_PAINT(),然后由映射表找到处理函数,即OnPaint();

不懂之处在于,OnPaint()如果是默认的,那肯定已经写好了可以完成一定的功能。但是后面又对他进行了实现,完全覆盖了以前的函数,如果这样,那任何的函数名都可以了,就不必要求用默认的对应了。
不明白,请帮忙解释一下吧。。
2012-09-03 15:57
newdos
Rank: 9Rank: 9Rank: 9
等 级:禁止访问
威 望:6
帖 子:251
专家分:1169
注 册:2012-8-13
收藏
得分:0 
afx_msg void OnPaint()是CWnd类定义好的,你可以在OnPaint上面按F12找到对应的定义之处。
它属于窗体自身的消息,所以对于这类消息的响应函数,CWnd里面已经默认定义好了,但没有实现,
CFrameWnd继承自CWnd类,所以你只要继承了这个类,覆盖函数并实现既可响应对应的消息。
这类消息还有很多,你可以在CWnd类里找到。

另一类控件响应消息,可以自定义函数名,例如按钮点击。
在AFXMSG_.H头文件是这样定义的。
// User Button Notification Codes
#define ON_BN_CLICKED(id, memberFxn) \
    ON_CONTROL(BN_CLICKED, id, memberFxn)
#define ON_BN_DOUBLECLICKED(id, memberFxn) \
    ON_CONTROL(BN_DOUBLECLICKED, id, memberFxn)
#define ON_BN_SETFOCUS(id, memberFxn) \
    ON_CONTROL(BN_SETFOCUS, id, memberFxn)
#define ON_BN_KILLFOCUS(id, memberFxn) \
    ON_CONTROL(BN_KILLFOCUS, id, memberFxn)

所以可以使用ON_BN_CLICKED(IDC_BUTTON1, OnButton1Click)这样的宏进行映射,第二个参数也就是成员函数可以自己命名。

找本<深入浅出mfc>这类的书来读读吧。
2012-09-04 10:55
聪儿
Rank: 1
等 级:新手上路
帖 子:75
专家分:3
注 册:2012-9-2
收藏
得分:0 
太感谢了!!
2012-09-04 15:35
快速回复:MFC 新手,求指点迷津
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017571 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved