我在单文档窗口里面创建了一个按钮,也写了消息效应函数,但是不知道为什么运行之后就是没反应,郁闷了好长时间了,今天把程序贴出来,请各位大侠帮忙看看是哪里的问题。谢谢
红色部分就是我添加的程序
这是MainFrm.cpp里的// MainFrm.cpp : implementation of the CMainFrame class
//#include "stdafx.h"
#include "excise2.h"#include "MainFrm.h"
#define IDB_BUTTON  100
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// CMainFrameIMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
// NOTE - the ClassWizard will add and remove mapping macros here.
//    DO NOT EDIT what you see in these blocks of generated code !
ON_WM_CREATE()
ON_BN_CLICKED(IDB_BUTTON, HandleButton) //}}AFX_MSG_MAP
END_MESSAGE_MAP()static UINT indicators[] =
{
ID_SEPARATOR,           // status line indicator
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL,
};/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destructionCMainFrame::CMainFrame()
{
// TODO: add member initialization code here

}CMainFrame::~CMainFrame()
{
}int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;

if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{
TRACE0("Failed to create toolbar\n");
return -1;      // fail to create
} if (!m_wndStatusBar.Create(this) ||
!m_wndStatusBar.SetIndicators(indicators,
  sizeof(indicators)/sizeof(UINT)))
{
TRACE0("Failed to create status bar\n");
return -1;      // fail to create
} // TODO: Delete these three lines if you don't want the toolbar to
//  be dockable
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar);
    MyButton.Create("Push me",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,CRect(100,100,200,200),this,IDB_BUTTON); return 0;
}BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;
// TODO: Modify the Window class or styles here by modifying
//  the CREATESTRUCT cs return TRUE;
}/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
CFrameWnd::AssertValid();
}void CMainFrame::Dump(CDumpContext& dc) const
{
CFrameWnd::Dump(dc);
}#endif //_DEBUG/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers
void CMainFrame::HandleButton()
{
   AfxMessageBox("123456");
   //MessageBeep(-1);}
这是头文件里的// MainFrm.h : interface of the CMainFrame class
//
/////////////////////////////////////////////////////////////////////////////#if !defined(AFX_MAINFRM_H__D67E58E9_94C0_4177_B8EB_1445ECE23881__INCLUDED_)
#define AFX_MAINFRM_H__D67E58E9_94C0_4177_B8EB_1445ECE23881__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000class CMainFrame : public CFrameWnd
{

protected: // create from serialization only
CMainFrame();
DECLARE_DYNCREATE(CMainFrame)// Attributes
public:// Operations
public:// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMainFrame)
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
//}}AFX_VIRTUAL// Implementation
public:
virtual ~CMainFrame();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endifprotected:  // control bar embedded members
CStatusBar  m_wndStatusBar;
CToolBar    m_wndToolBar;// Generated message map functions
protected:

CButton MyButton;
//{{AFX_MSG(CMainFrame)
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
// NOTE - the ClassWizard will add and remove member functions here.
//    DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG
afx_msg void HandleButton(); DECLARE_MESSAGE_MAP()
};///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_MAINFRM_H__D67E58E9_94C0_4177_B8EB_1445ECE23881__INCLUDED_)

解决方案 »

  1.   

    //{{AFX_MSG(CMainFrame) 
    afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct); 
    // NOTE - the ClassWizard will add and remove member functions here. 
    //    DO NOT EDIT what you see in these blocks of generated code! 
    afx_msg void HandleButton();
    //}}AFX_MSG 
    这样才对
      

  2.   

    按你说的改了,但是蹦出来好多错误,怎么回事啊?
    --------------------Configuration: excise7 - Win32 Debug--------------------
    Compiling...
    MainFrm.cpp
    C:\Program Files\Microsoft Visual Studio\MyProjects\excise7\MainFrm.cpp(20) : error C2509: '_GetBaseMessageMap' : member function not declared in 'CMainFrame'
            c:\program files\microsoft visual studio\myprojects\excise7\mainfrm.h(12) : see declaration of 'CMainFrame'
    C:\Program Files\Microsoft Visual Studio\MyProjects\excise7\MainFrm.cpp(20) : error C2509: 'GetMessageMap' : member function not declared in 'CMainFrame'
            c:\program files\microsoft visual studio\myprojects\excise7\mainfrm.h(12) : see declaration of 'CMainFrame'
    C:\Program Files\Microsoft Visual Studio\MyProjects\excise7\MainFrm.cpp(20) : warning C4273: 'protected: static struct AFX_MSGMAP const CFrameWnd::messageMap' : inconsistent dll linkage.  dllexport assumed.
    C:\Program Files\Microsoft Visual Studio\MyProjects\excise7\MainFrm.cpp(20) : error C2491: 'CMainFrame::CFrameWnd::messageMap' : definition of dllimport static data member not allowed
    C:\Program Files\Microsoft Visual Studio\MyProjects\excise7\MainFrm.cpp(25) : error C2248: 'HandleButton' : cannot access protected member declared in class 'CMainFrame'
            c:\program files\microsoft visual studio\myprojects\excise7\mainfrm.h(49) : see declaration of 'HandleButton'
      

  3.   

    按你说的改了,但是蹦出来好多错误,怎么回事啊?
    --------------------Configuration: excise7 - Win32 Debug--------------------
    Compiling...
    MainFrm.cpp
    C:\Program Files\Microsoft Visual Studio\MyProjects\excise7\MainFrm.cpp(20) : error C2509: '_GetBaseMessageMap' : member function not declared in 'CMainFrame'
            c:\program files\microsoft visual studio\myprojects\excise7\mainfrm.h(12) : see declaration of 'CMainFrame'
    C:\Program Files\Microsoft Visual Studio\MyProjects\excise7\MainFrm.cpp(20) : error C2509: 'GetMessageMap' : member function not declared in 'CMainFrame'
            c:\program files\microsoft visual studio\myprojects\excise7\mainfrm.h(12) : see declaration of 'CMainFrame'
    C:\Program Files\Microsoft Visual Studio\MyProjects\excise7\MainFrm.cpp(20) : warning C4273: 'protected: static struct AFX_MSGMAP const CFrameWnd::messageMap' : inconsistent dll linkage.  dllexport assumed.
    C:\Program Files\Microsoft Visual Studio\MyProjects\excise7\MainFrm.cpp(20) : error C2491: 'CMainFrame::CFrameWnd::messageMap' : definition of dllimport static data member not allowed
    C:\Program Files\Microsoft Visual Studio\MyProjects\excise7\MainFrm.cpp(25) : error C2248: 'HandleButton' : cannot access protected member declared in class 'CMainFrame'
            c:\program files\microsoft visual studio\myprojects\excise7\mainfrm.h(49) : see declaration of 'HandleButton'
      

  4.   

    首先并不需要把
    //{{AFX_MSG(CMainFrame) 
    afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct); 
    // NOTE - the ClassWizard will add and remove member functions here. 
    //    DO NOT EDIT what you see in these blocks of generated code! 
    afx_msg void HandleButton(); 
    //}}AFX_MSGafx_msg void HandleButton();应该放在后面,,就像你原来的那样在}}AFX_MSG之后。。DECLARE_MESSAGE_MAP()之前。。这样是为了不干扰VC自己添加的消息响应函数你先看看你的IDB_BUTTON,在String Table里面有没有出现。。(资源浏览里面)
    把ID弄成你的IDB_BUTTON。。Value随便给。。Caption一定要写下,写一个字母也行否则就不会响应函数了。。你试试看,
      

  5.   

    还是没反应啊?怎么回事啊?我人都快疯掉了!!!!!
    我就是在单文档视图上面建了一个按钮,刚开始是在MainFrame里面Creat了一个按钮,按钮能显示,但是加入消息处理函数之后没反应。在网上看到这样的情况都是很简单的几行程序,为什么我的就不可以实现呢!!!!!后来我又看到有人说要在View里面建,所以我又照做了,结果按钮显示不出来?我觉得应该在View里面建,有人说消息跟父窗口有关,那我这个按钮的父窗口就是这个视图喽?是不是窗体又是视图的父窗口?我快疯掉了,一个按钮都处理了快三天了,手头又没有好的资料,各位大侠就帮帮我吧,要是有实例什么的就更好了,先谢谢了!
      

  6.   

    跟着我一步一步做吧..
    我创建了一个工程,叫TestView,类向导自动添加了文件TestViewView.h和TestViewView.cpp
    先打开TestViewView.h,完成一些函数声明和变量定义的工作..//Button的ID
    #define IDC_BUTTON 2001class CTestViewView : public CView
    {
    protected: // create from serialization only
    CTestViewView();
    DECLARE_DYNCREATE(CTestViewView)// Attributes
    public:
    CTestViewDoc* GetDocument();// Operations
    public:
    CButton m_Button;  //动态创建一个Button用到的变量
    // Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CTestViewView)
    public:
    virtual void OnDraw(CDC* pDC);  // overridden to draw this view
    virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
    protected:
    virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
    virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
    virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
    //}}AFX_VIRTUAL// Implementation
    public:
    void DrawLine();
    virtual ~CTestViewView();
    #ifdef _DEBUG
    virtual void AssertValid() const;
    virtual void Dump(CDumpContext& dc) const;
    #endifprotected:// Generated message map functions
    protected:
    //{{AFX_MSG(CTestViewView)
    afx_msg void OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
    afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
    afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
    //}}AFX_MSG
    afx_msg void OnClickButton();  //响应单击Button的函数
    DECLARE_MESSAGE_MAP()
    };
    接下来再打开TestViewView.cpp文件,添加消息映射..
    BEGIN_MESSAGE_MAP(CTestViewView, CView)
    //{{AFX_MSG_MAP(CTestViewView)
    ON_WM_VSCROLL()
    ON_WM_CREATE()
    ON_WM_LBUTTONDOWN()
    //}}AFX_MSG_MAP
    // Standard printing commands
    ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
    ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
    ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
    ON_COMMAND(IDC_BUTTON, OnClickButton)  //为这个Button添加消息映射
    END_MESSAGE_MAP()//接下来就是在OnCreate()中动态的创建一个Button
    int CTestViewView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
    {
    if (CView::OnCreate(lpCreateStruct) == -1)
    return -1;

    // TODO: Add your specialized creation code here

    m_Button.Create("确定",WS_CHILD | WS_VISIBLE | WS_BORDER,CRect(0,0,50,50),this,IDC_BUTTON);
    return 0;
    }最后再是写消息响应函数
    void CTestViewView::OnClickButton()
    {
    MessageBox("");
    }
      

  7.   

    我刚刚试了一下
    在View里面可以实现的。。你先重载一个OnInitialUpdate的虚函数把Create添加进去。。其他的都不要改了~~可以用。已经亲测
    void CExcise2View::OnInitialUpdate() 
    {
    CView::OnInitialUpdate();

    // TODO: Add your specialized code here and/or call the base class
    MyButton.Create("Push me",WS_CHILD |WS_VISIBLE|BS_PUSHBUTTON ,CRect(100,100,200,200),this,IDB_BUTTON);
    }
      

  8.   

    谢谢你,问题解决了。
    但是我不是像你那样在int CTestViewView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
    里创建按钮,我是在::OnInitialUpdate() 中创建的,我用你那种方法一直显示不出来,我不知道为什么,楼上的能解释一下不?
      

  9.   

    噢,是不是OnCreate只是产生VIEW的基本结构和变量,产生了窗口但是并没有产生视图,而在OnInitialUpDate()中初始化视图中控件?是这样理解吗?