正学WINDOWS程序设计,照着书上的讲弹出菜单的代码写了写,编译无误运行后在程序里右键点击,就出现下面图中的错误,我仔细看了看源代码,和书上是一样的啊!到底我错在哪了啊?(附源代码压缩包~)
(下面是改过程序的文件,这是MainFrm.h)
// MainFrm.h : interface of the CMainFrame class
//
/////////////////////////////////////////////////////////////////////////////
#if !defined(AFX_MAINFRM_H__D6D8C2C9_CF35_4E2D_8AF5_2528879E9C91__INCLUDED_)
#define AFX_MAINFRM_H__D6D8C2C9_CF35_4E2D_8AF5_2528879E9C91__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class 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;
#endif
protected: // control bar embedded members
CStatusBar m_wndStatusBar;
CToolBar m_wndToolBar;
// Generated message map functions
protected:
//{{AFX_MSG(CMainFrame)
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnContextMenu(CWnd* pWnd, CPoint point);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_MAINFRM_H__D6D8C2C9_CF35_4E2D_8AF5_2528879E9C91__INCLUDED_)
(这是MousePositionView.h)
// MousePositionView.h : interface of the CMousePositionView class
//
/////////////////////////////////////////////////////////////////////////////
#if !defined(AFX_MOUSEPOSITIONVIEW_H__16660782_04A4_4DC6_8FA9_E359E7DAD068__INCLUDED_)
#define AFX_MOUSEPOSITIONVIEW_H__16660782_04A4_4DC6_8FA9_E359E7DAD068__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CMousePositionView : public CView
{
protected: // create from serialization only
CMousePositionView();
DECLARE_DYNCREATE(CMousePositionView)
// Attributes
public:
CMousePositionDoc* GetDocument();
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMousePositionView)
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:
BOOL m_bDisplay;
virtual ~CMousePositionView();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected:
// Generated message map functions
protected:
//{{AFX_MSG(CMousePositionView)
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg void OnNewItem();
afx_msg void OnUpdateNewItem(CCmdUI *pCmdUI);
afx_msg void OnTest();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
private:
CPoint m_MousePos;
};
#ifndef _DEBUG // debug version in MousePositionView.cpp
inline CMousePositionDoc* CMousePositionView::GetDocument()
{ return (CMousePositionDoc*)m_pDocument; }
#endif
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_MOUSEPOSITIONVIEW_H__16660782_04A4_4DC6_8FA9_E359E7DAD068__INCLUDED_)
(这是MainFrm.cpp)
// MainFrm.cpp : implementation of the CMainFrame class
//
#include "stdafx.h"
#include "MousePosition.h"
#include "MainFrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMainFrame
IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
ON_WM_CREATE()
ON_WM_CONTEXTMENU()
//}}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/destruction
CMainFrame::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);
CMenu *pSysMenu=GetMenu();
CMenu *pSubMenu=pSysMenu->GetSubMenu(2);
CString str("显示鼠标坐标(&D)");
pSubMenu->AppendMenu(MF_SEPARATOR);
pSubMenu->AppendMenu(MF_STRING,ID_NEW_ITEM,str);
DrawMenuBar();
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::OnContextMenu(CWnd* pWnd, CPoint point)
{
// TODO: Add your message handler code here
CMenu menu;
menu.LoadMenu(IDR_FLOATMENU);
menu.GetSubMenu(0)->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,point.x,point.y,this);
}
(这是MousePositionView.cpp)
// MousePositionView.cpp : implementation of the CMousePositionView class
//
#include "stdafx.h"
#include "MousePosition.h"
#include "MousePositionDoc.h"
#include "MousePositionView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMousePositionView
IMPLEMENT_DYNCREATE(CMousePositionView, CView)
BEGIN_MESSAGE_MAP(CMousePositionView, CView)
//{{AFX_MSG_MAP(CMousePositionView)
ON_WM_MOUSEMOVE()
ON_COMMAND(ID_NEW_ITEM,OnNewItem)
ON_UPDATE_COMMAND_UI(ID_NEW_ITEM,OnUpdateNewItem)
ON_COMMAND(ID_TEST, OnTest)
//}}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)
END_MESSAGE_MAP()
void CMousePositionView::OnUpdateNewItem(CCmdUI *pCmdUI)
{
pCmdUI->SetCheck(m_bDisplay);
}
/////////////////////////////////////////////////////////////////////////////
// CMousePositionView construction/destruction
CMousePositionView::CMousePositionView()
{
// TODO: add construction code here
m_MousePos=CPoint(0,0);
m_bDisplay=true;
}
CMousePositionView::~CMousePositionView()
{
}
BOOL CMousePositionView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMousePositionView drawing
void CMousePositionView::OnDraw(CDC* pDC)
{
CMousePositionDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
if(!m_bDisplay)
return;
CString str;
str.Format("X 位置:%d",m_MousePos.x);
pDC->TextOut(m_MousePos.x,m_MousePos.y,str);
str.Format("Y 位置:%d",m_MousePos.y);
pDC->TextOut(m_MousePos.x,m_MousePos.y+20,str);
}
/////////////////////////////////////////////////////////////////////////////
// CMousePositionView printing
BOOL CMousePositionView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMousePositionView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMousePositionView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CMousePositionView diagnostics
#ifdef _DEBUG
void CMousePositionView::AssertValid() const
{
CView::AssertValid();
}
void CMousePositionView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMousePositionDoc* CMousePositionView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMousePositionDoc)));
return (CMousePositionDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMousePositionView message handlers
void CMousePositionView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_MousePos=point;
Invalidate(true);
CView::OnMouseMove(nFlags, point);
}
void CMousePositionView::OnNewItem()
{
m_bDisplay=!m_bDisplay;
}
void CMousePositionView::OnTest()
{
// TODO: Add your command handler code here
MessageBox("你点击了ID_TEST弹出菜单项");
}
下面是出错的图:
[此贴子已经被作者于2006-10-12 18:02:10编辑过]