class CPhonesListBox:public ClistBox之后出现了问题我没找到,帮忙找找谢谢了,急呀!!!!

tm_wb 2005-09-07 09:19:58
我在做一个简单的电话号簿的程序练习框架是用MFC AppWizard 生成的
我我这样定义客户区类
class CPhonesListBox:public ClistBox然后
class CChildView:public CPhonesListBox我想把客户区做成列表框样式的
但是在对话框接受到名字和号码之后并没有把名字和号码加入客户区内
大家帮帮我
代码很多最好大家留下邮箱
不过我还是把我认为有问题的地方粘出来,大家帮忙看看,如果认为不是这些地方有问题我会把剩余地方粘出来,谢谢
PhonesListBox.h
/////////////////////////////////////////////////////////////////////////////////
#if !defined(AFX_PHONESLISTBOX_H__F58B1338_C25E_4C92_8329_EFFE34CA0D33__INCLUDED_)
#define AFX_PHONESLISTBOX_H__F58B1338_C25E_4C92_8329_EFFE34CA0D33__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// PhonesListBox.h : header file
//

/////////////////////////////////////////////////////////////////////////////
// CPhonesListBox window

class CPhonesListBox : public CListBox
{
// Construction
public:
CPhonesListBox();

// Attributes
public:

// Operations
public:

// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CPhonesListBox)
//}}AFX_VIRTUAL

// Implementation
public:
void NewEntry();
virtual ~CPhonesListBox();

// Generated message map functions
protected:
CFont m_font;
//{{AFX_MSG(CPhonesListBox)
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnEditItem();
//}}AFX_MSG

DECLARE_MESSAGE_MAP()
};

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_PHONESLISTBOX_H__F58B1338_C25E_4C92_8329_EFFE34CA0D33__INCLUDED_)

///////////////////////////////////////////////////////////////////////////////

PhonesListBox.cpp
///////////////////////////////////////////////////////////////////////////////
// PhonesListBox.cpp : implementation file
//

#include "stdafx.h"
#include "Phones.h"
#include "PhonesListBox.h"
#include "PhoneEdit.h"
#include "EditDialog.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CPhonesListBox

CPhonesListBox::CPhonesListBox()
{
}

CPhonesListBox::~CPhonesListBox()
{
}


BEGIN_MESSAGE_MAP(CPhonesListBox, CListBox)
//{{AFX_MSG_MAP(CPhonesListBox)
ON_WM_CREATE()
ON_CONTROL_REFLECT(LBN_DBLCLK,OnEditItem)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPhonesListBox message handlers



int CPhonesListBox::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CListBox::OnCreate(lpCreateStruct) == -1)
return -1;

m_font.CreatePointFont(80,_T("MS Sans Serif"));
SetFont(&m_font,FALSE);
SetTabStops(128);
return 0;
}

void CPhonesListBox::OnEditItem()
{
CEditDialog dlg;
CString strItem;
int nIndex=GetCurSel();
GetText(nIndex,strItem);
int nPos=strItem.Find(_T('\t'));

dlg.m_strName=strItem.Left(nPos);
dlg.m_strPhone=strItem.Right(strItem.GetLength()-nPos-1);
if(dlg.DoModal()==IDOK)
{
strItem=dlg.m_strName+_T("\t")+dlg.m_strPhone;
DeleteString(nIndex);
AddString(strItem);
}
SetFocus();
}

void CPhonesListBox::NewEntry()
{
CEditDialog dlg;
if(dlg.DoModal()==IDOK)
{
CString strItem=dlg.m_strName+_T("\t")+dlg.m_strPhone;
AddString(strItem);
}
SetFocus();
}
///////////////////////////////////////////////////////////////////////////



...全文
72 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
tm_wb 2005-09-08
  • 打赏
  • 举报
回复
我是想把View做成类似ListBox一类的东西
所以才从CPhonesListBox派生出CChildView
菜牛 2005-09-07
  • 打赏
  • 举报
回复
代码多,就没仔细看了,自己调试时候跟踪一下,定位错误语句。

不过感觉你的思路有问题,CPhonesListBox应该做成View的子控件,View从普通控件类继承肯定有问题。
tm_wb 2005-09-07
  • 打赏
  • 举报
回复
CChildView.h
////////////////////////////////////////////////////////////////////////////
// ChildView.h : interface of the CChildView class
//
/////////////////////////////////////////////////////////////////////////////

#if !defined(AFX_CHILDVIEW_H__3CA7BEED_19E7_4C62_8CF1_0BF3C24A755E__INCLUDED_)
#define AFX_CHILDVIEW_H__3CA7BEED_19E7_4C62_8CF1_0BF3C24A755E__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "PhonesListBox.h"
/////////////////////////////////////////////////////////////////////////////
// CChildView window

class CChildView : public CPhonesListBox
{
// Construction
public:
CChildView();

// Attributes
public:

// Operations
public:

// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CChildView)
protected:
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
//}}AFX_VIRTUAL

// Implementation
public:

virtual ~CChildView();

// Generated message map functions
protected:
BOOL SaveFile(LPCTSTR pszFile);
BOOL LoadFile(LPCTSTR pszFile);
static const TCHAR m_szFilters[];
CString m_strPathName;
//{{AFX_MSG(CChildView)
afx_msg void OnFileNew();
afx_msg void OnFileOpen();
afx_msg void OnFileSave();
afx_msg void OnFileSaveAs();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_CHILDVIEW_H__3CA7BEED_19E7_4C62_8CF1_0BF3C24A755E__INCLUDED_)
/////////////////////////////////////////////////////////////////////////////////////

CChildView.cpp
///////////////////////////////////////////////////////////////////////////////////
// ChildView.cpp : implementation of the CChildView class
//

#include "stdafx.h"
#include "Phones.h"
#include "ChildView.h"
#include "PhonesListBox.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CChildView

const TCHAR CChildView::m_szFilters[]=_T("Phone Files(*.phn)|*.phn|All Files(*.*)|*.*||");
CChildView::CChildView()
{
}

CChildView::~CChildView()
{
}


BEGIN_MESSAGE_MAP(CChildView,CWnd )
//{{AFX_MSG_MAP(CChildView)
ON_COMMAND(ID_FILE_NEW, OnFileNew)
ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
ON_COMMAND(ID_FILE_SAVE, OnFileSave)
ON_COMMAND(ID_FILE_SAVE_AS, OnFileSaveAs)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CChildView message handlers

BOOL CChildView::PreCreateWindow(CREATESTRUCT& cs)
{
if (!CWnd::PreCreateWindow(cs))
return FALSE;

cs.dwExStyle |= WS_EX_CLIENTEDGE;
cs.style &= ~WS_BORDER;
cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS,
::LoadCursor(NULL, IDC_ARROW), HBRUSH(COLOR_WINDOW+1), NULL);

return TRUE;
}



BOOL CChildView::SaveFile(LPCTSTR pszFile)
{
BOOL bResult=FALSE;

try
{
CStdioFile file(pszFile,CFile::modeWrite|CFile::modeCreate);
DWORD dwCount=GetCount();
file.Write(&dwCount,sizeof(dwCount));
if(dwCount)
{
for(int i=0;i<(int)dwCount;i++)
{
CString string;
GetText(i,string);
string+=_T("\n");
file.WriteString(string);
}
}
bResult=TRUE;
}
catch(CFileException* e)
{
e->ReportError();
e->Delete();
}
return bResult;
}

BOOL CChildView::LoadFile(LPCTSTR pszFile)
{
BOOL bResult=FALSE;

try
{
CStdioFile file(pszFile,CFile::modeRead);
ResetContent();
DWORD dwCount;
file.Read(&dwCount,sizeof(dwCount));
if(dwCount)
{
for(int i=0;i<(int)dwCount;i++)
{
CString string;
file.ReadString(string);
AddString(string);
}
}
bResult=TRUE;
}
catch(CFileException* e)
{
e->ReportError();
e->Delete();
}
return bResult;
}

void CChildView::OnFileNew()
{
NewEntry();
}

void CChildView::OnFileOpen()
{
CFileDialog dlg(TRUE,_T("phn"),_T("*.phn"),OFN_FILEMUSTEXIST|OFN_HIDEREADONLY,m_szFilters);
if(dlg.DoModal()==IDOK)
{
if(LoadFile(dlg.GetPathName()))
{
m_strPathName=dlg.GetPathName();
SetCurSel(0);
}
}
}

void CChildView::OnFileSave()
{
if(!m_strPathName.IsEmpty())
SaveFile(m_strPathName);
else
OnFileSaveAs();
}

void CChildView::OnFileSaveAs()
{
CFileDialog dlg(FALSE,_T("phn"),m_strPathName,OFN_OVERWRITEPROMPT|OFN_PATHMUSTEXIST|OFN_HIDEREADONLY,
m_szFilters);

if(dlg.DoModal()==IDOK)
if(SaveFile(dlg.GetPathName()))
m_strPathName=dlg.GetPathName();

}

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧