⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 listview.cpp

📁 这个是VC实例教程第7章节的第二个实例的源代码
💻 CPP
字号:
// ListView.cpp : implementation of the CMyListView class
//

#include "stdafx.h"
#include "List.h"

#include "ListDoc.h"
#include "ListView.h"
#include "DlgAdd.h"
#include "DlgDelete.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMyListView

IMPLEMENT_DYNCREATE(CMyListView, CView)

BEGIN_MESSAGE_MAP(CMyListView, CView)
	//{{AFX_MSG_MAP(CMyListView)
	ON_COMMAND(ID_EDIT_ADD, OnEditAdd)
	ON_COMMAND(ID_EDIT_DELETE, OnEditDelete)
	ON_COMMAND(ID_EDIT_SERCH, OnEditSerch)
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CMyListView construction/destruction

CMyListView::CMyListView()
{
	// TODO: add construction code here

}

CMyListView::~CMyListView()
{
}

BOOL CMyListView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMyListView drawing

void CMyListView::OnDraw(CDC* pDC)
{
	CListDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	TEXTMETRIC textMetric;
	pDC->GetTextMetrics(&textMetric);
	int fontHeight = textMetric.tmHeight;
	// Initialize values used in the loop.
	POSITION pos = m_List.GetHeadPosition();
	int displayPosition = 10;
	// Iterate over the list, displaying each node's values.
	while (pos != NULL)
	{
		CStudent* m_pStudent = (CStudent*) m_List.GetNext(pos);
		char s[81];
		wsprintf(s, " 的成绩是 %d.",m_pStudent->m_nScore);
		CString m_string=m_pStudent->m_strName+s;
		pDC->TextOut(10, displayPosition, m_string);
		displayPosition += fontHeight;
	}
}

/////////////////////////////////////////////////////////////////////////////
// CMyListView printing

BOOL CMyListView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CMyListView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CMyListView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CMyListView diagnostics

#ifdef _DEBUG
void CMyListView::AssertValid() const
{
	CView::AssertValid();
}

void CMyListView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CListDoc* CMyListView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CListDoc)));
	return (CListDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMyListView message handlers

void CMyListView::OnEditAdd() 
{
	// TODO: Add your command handler code here
	CDlgAdd dlg;
    dlg.m_strName = "";
    dlg.m_nScore = 0 ;
    // Display the dialog box.
    int result = dlg.DoModal();
    if (result == IDOK)
	{
		// Create and initialize the new node.
		CStudent* m_pStudent = new CStudent;
		m_pStudent->m_strName = dlg.m_strName;
		m_pStudent->m_nScore = dlg.m_nScore;
		// Add the node to the list.
		m_List.AddTail(m_pStudent);
		// Repaint the window.
		Invalidate();
	}
}
	

void CMyListView::OnEditDelete() 
{
	// TODO: Add your command handler code here
	CDlgDelete dlg;
	dlg.m_nRadio = 0;
	// Display the dialog box.
	int result = dlg.DoModal();
	// If the user clicked the OK button...
	if (result == IDOK)
	{
		CStudent* m_pStudent = NULL;
		// Make sure the list isn't empty.
		if (m_List.IsEmpty())
			MessageBox("节点已经全部删除!");
		else
		{
			// Remove the specified node.
			if (dlg.m_nRadio == 0)
				m_pStudent = (CStudent*)m_List.RemoveHead();
			else if(dlg.m_nRadio == 1)
				m_pStudent = (CStudent*)m_List.RemoveTail();
			else 
			{
				if(dlg.m_nNumber < m_List.GetCount())
				{
					POSITION pos = m_List.FindIndex(dlg.m_nNumber);
					POSITION pos1 = pos;
					m_pStudent = (CStudent*)m_List.GetNext(pos1);
					m_List.RemoveAt(pos);
				}
				else
				{
					MessageBox("指定节点超出范围!");
				}
			}
			// Delete the node object and repaint the window.
			if(m_pStudent)
				delete m_pStudent;
			Invalidate();
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -