首页上一页 1 下一页尾页 1 条记录 1/1页
请技术高手解答下
发表在C语言图书答疑
2009-08-03
是否精华
是
否
版块置顶:
是
否
我利用AppWizard创建一个单文档程序项目,命名为CGridDemo。利用ClassWizard加入两个新类:基类为CEdit类的CCell类和基类为CWnd类的CGridWnd类。下面列出这两个类的主要代码:
class CCell : public CEdit
{
// Construction
public:
CCell();
public:
public:
BOOL SetFocus;
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CCell)
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CCell();
// Generated message map functions
protected:
//{{AFX_MSG(CCell)
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
afx_msg void OnKillfocus();
afx_msg void OnSetfocus();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CCell::CCell()
{
}
CCell::~CCell()
{
}
BEGIN_MESSAGE_MAP(CCell, CEdit)
//{{AFX_MSG_MAP(CCell)
ON_WM_ERASEBKGND()
ON_CONTROL_REFLECT(EN_KILLFOCUS, OnKillfocus)
ON_CONTROL_REFLECT(EN_SETFOCUS, OnSetfocus)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCell message handlers
BOOL CCell::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
RECT rect;
CPen Pen;
CBrush Brush;
LOGBRUSH LogBrush;
if(SetFocus==TRUE)
//当用户操作单元格时,为单元格加上一个黑色边框
{
Pen.CreatePen(PS_SOLID,2,RGB(0,0,0)); //设置线条宽度为两个像素
LogBrush.lbColor=RGB(0,0,0);
LogBrush.lbStyle=BS_HOLLOW;
Brush.CreateBrushIndirect(&LogBrush);
pDC->SelectObject(&Pen);
pDC->SelectObject(&Brush);
this->GetClientRect(&rect);
pDC->Rectangle(&rect); //为单元格加黑色边框
}
else //当用户操作另外的单元格,消除黑色边框
{
Pen.CreatePen(PS_SOLID,2,RGB(255,255,255)); //创建白色的Pen以覆盖边框
LogBrush.lbColor=RGB(0,0,0);
LogBrush.lbStyle=BS_HOLLOW;
Brush.CreateBrushIndirect(&LogBrush);
pDC->SelectObject(&Pen);
pDC->SelectObject(&Brush);
this->GetClientRect(&rect);
pDC->Rectangle(&rect); }
return CEdit::OnEraseBkgnd(pDC);
}
void CCell::OnKillfocus()
{
// TODO: Add your control notification handler code here
SetFocus=FALSE;
CDC* pDC=this->GetWindowDC();
this->OnEraseBkgnd(pDC);
}
void CCell::OnSetfocus()
{
// TODO: Add your control notification handler code here
SetFocus=TRUE; //SetFocus为BOOL型的变量
CDC* pDC=this->GetWindowDC();
this->OnEraseBkgnd(pDC);
//改变单元格外观
}
#include "Cell.h"
class CGridWnd : public CWnd
{
// Construction
public:
CGridWnd();
CGridWnd(CWnd* pWnd,UINT nRow,UINT nCol);
// Attributes
public:
BOOL Create();
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CGridWnd)
public:
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CGridWnd();
// Generated message map functions
protected:
//{{AFX_MSG(CGridWnd)
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
//}}AFX_MSG
private:
int Row; //定义行数
int Col; //定义列数
CWnd* m_pParentWnd; //父窗口句柄
CCell** Cell;
DECLARE_MESSAGE_MAP()
};
CGridWnd::CGridWnd(CWnd* pWnd,UINT nRow,UINT nCol) //CGridWnd类构造函数
{
m_pParentWnd=pWnd; //父窗口句柄
Row=nRow; //行数
Col=nCol; //列数
}
BOOL CGridWnd::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
for(int i=0;i<Row+1;i++)
//变量Row和Col是要创建的表格的行列数
{
pDC->MoveTo(0,i*24); //网格大小为104×24
pDC->LineTo(Col*104,i*24);
}
for(int j=0;j>Col+1;j++)
{
pDC->MoveTo(j*104,0);
pDC->LineTo(j*104,Row*24); }
return TRUE;
// return CWnd::OnEraseBkgnd(pDC);
}
int CGridWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
// if (CWnd::OnCreate(lpCreateStruct) == -1)
// return -1;
// TODO: Add your specialized creation code here
RECT rect;
for(int i=0;i<Row;i++)
{
for(int j=0;j<Col;j++)
{
Cell[i]=new CCell; //根据每行、列的网格数生成相应的单元格
rect.top=i*24+2; //设定每个单元格大小为100×20,并确定单元格所在位置
rect.left=j*104+2;
rect.bottom=rect.top+20;
rect.right=rect.left+100;
Cell[i]->Create(WS_CHILD|WS_VISIBLE,rect,this,0); //在网格中放入单元格
}
}
return 0;
}
BOOL CGridWnd::Create() //重载Create函数,建立一个与父窗口同样大小的Grid窗口
{
RECT rect;
m_pParentWnd->GetClientRect(&rect);
return CWnd::Create(NULL, NULL, WS_CHILD|WS_VISIBLE, rect, m_pParentWnd, NULL,NULL);
}
//下面的代码演示使用表格窗口的方法
void CCGridDemoView::OnDraw(CDC* pDC)
{
CCGridDemoDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
if(!Flags) // 其中Flags为View类的一个保护变量,初始化为FALSE
{
Grid=new CGridWnd(this,5,5); //建立一个5行5列的表格
Grid->Create();
Flags=TRUE;
}
}
编译时出现错误
E:\CGridDemo\CGridDemoView.cpp(66) : error C2065: 'Grid' : undeclared identifier
E:\CGridDemo\CGridDemoView.cpp(66) : error C2440: '=' : cannot convert from 'class CGridWnd *' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
E:\CGridDemo\CGridDemoView.cpp(67) : error C2227: left of '->Create' must point to class/struct/union
GridWnd.cpp
Generating Code...
执行 cl.exe 时出错.
CGridDemo.exe - 1 error(s), 0 warning(s)
而我确不知道Grid这个该怎么定义
class CCell : public CEdit
{
// Construction
public:
CCell();
public:
public:
BOOL SetFocus;
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CCell)
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CCell();
// Generated message map functions
protected:
//{{AFX_MSG(CCell)
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
afx_msg void OnKillfocus();
afx_msg void OnSetfocus();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CCell::CCell()
{
}
CCell::~CCell()
{
}
BEGIN_MESSAGE_MAP(CCell, CEdit)
//{{AFX_MSG_MAP(CCell)
ON_WM_ERASEBKGND()
ON_CONTROL_REFLECT(EN_KILLFOCUS, OnKillfocus)
ON_CONTROL_REFLECT(EN_SETFOCUS, OnSetfocus)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCell message handlers
BOOL CCell::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
RECT rect;
CPen Pen;
CBrush Brush;
LOGBRUSH LogBrush;
if(SetFocus==TRUE)
//当用户操作单元格时,为单元格加上一个黑色边框
{
Pen.CreatePen(PS_SOLID,2,RGB(0,0,0)); //设置线条宽度为两个像素
LogBrush.lbColor=RGB(0,0,0);
LogBrush.lbStyle=BS_HOLLOW;
Brush.CreateBrushIndirect(&LogBrush);
pDC->SelectObject(&Pen);
pDC->SelectObject(&Brush);
this->GetClientRect(&rect);
pDC->Rectangle(&rect); //为单元格加黑色边框
}
else //当用户操作另外的单元格,消除黑色边框
{
Pen.CreatePen(PS_SOLID,2,RGB(255,255,255)); //创建白色的Pen以覆盖边框
LogBrush.lbColor=RGB(0,0,0);
LogBrush.lbStyle=BS_HOLLOW;
Brush.CreateBrushIndirect(&LogBrush);
pDC->SelectObject(&Pen);
pDC->SelectObject(&Brush);
this->GetClientRect(&rect);
pDC->Rectangle(&rect); }
return CEdit::OnEraseBkgnd(pDC);
}
void CCell::OnKillfocus()
{
// TODO: Add your control notification handler code here
SetFocus=FALSE;
CDC* pDC=this->GetWindowDC();
this->OnEraseBkgnd(pDC);
}
void CCell::OnSetfocus()
{
// TODO: Add your control notification handler code here
SetFocus=TRUE; //SetFocus为BOOL型的变量
CDC* pDC=this->GetWindowDC();
this->OnEraseBkgnd(pDC);
//改变单元格外观
}
#include "Cell.h"
class CGridWnd : public CWnd
{
// Construction
public:
CGridWnd();
CGridWnd(CWnd* pWnd,UINT nRow,UINT nCol);
// Attributes
public:
BOOL Create();
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CGridWnd)
public:
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CGridWnd();
// Generated message map functions
protected:
//{{AFX_MSG(CGridWnd)
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
//}}AFX_MSG
private:
int Row; //定义行数
int Col; //定义列数
CWnd* m_pParentWnd; //父窗口句柄
CCell** Cell;
DECLARE_MESSAGE_MAP()
};
CGridWnd::CGridWnd(CWnd* pWnd,UINT nRow,UINT nCol) //CGridWnd类构造函数
{
m_pParentWnd=pWnd; //父窗口句柄
Row=nRow; //行数
Col=nCol; //列数
}
BOOL CGridWnd::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
for(int i=0;i<Row+1;i++)
//变量Row和Col是要创建的表格的行列数
{
pDC->MoveTo(0,i*24); //网格大小为104×24
pDC->LineTo(Col*104,i*24);
}
for(int j=0;j>Col+1;j++)
{
pDC->MoveTo(j*104,0);
pDC->LineTo(j*104,Row*24); }
return TRUE;
// return CWnd::OnEraseBkgnd(pDC);
}
int CGridWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
// if (CWnd::OnCreate(lpCreateStruct) == -1)
// return -1;
// TODO: Add your specialized creation code here
RECT rect;
for(int i=0;i<Row;i++)
{
for(int j=0;j<Col;j++)
{
Cell[i]=new CCell; //根据每行、列的网格数生成相应的单元格
rect.top=i*24+2; //设定每个单元格大小为100×20,并确定单元格所在位置
rect.left=j*104+2;
rect.bottom=rect.top+20;
rect.right=rect.left+100;
Cell[i]->Create(WS_CHILD|WS_VISIBLE,rect,this,0); //在网格中放入单元格
}
}
return 0;
}
BOOL CGridWnd::Create() //重载Create函数,建立一个与父窗口同样大小的Grid窗口
{
RECT rect;
m_pParentWnd->GetClientRect(&rect);
return CWnd::Create(NULL, NULL, WS_CHILD|WS_VISIBLE, rect, m_pParentWnd, NULL,NULL);
}
//下面的代码演示使用表格窗口的方法
void CCGridDemoView::OnDraw(CDC* pDC)
{
CCGridDemoDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
if(!Flags) // 其中Flags为View类的一个保护变量,初始化为FALSE
{
Grid=new CGridWnd(this,5,5); //建立一个5行5列的表格
Grid->Create();
Flags=TRUE;
}
}
编译时出现错误
E:\CGridDemo\CGridDemoView.cpp(66) : error C2065: 'Grid' : undeclared identifier
E:\CGridDemo\CGridDemoView.cpp(66) : error C2440: '=' : cannot convert from 'class CGridWnd *' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
E:\CGridDemo\CGridDemoView.cpp(67) : error C2227: left of '->Create' must point to class/struct/union
GridWnd.cpp
Generating Code...
执行 cl.exe 时出错.
CGridDemo.exe - 1 error(s), 0 warning(s)
而我确不知道Grid这个该怎么定义