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

📄 t1dlg.cpp

📁 一个简单的记录集类
💻 CPP
字号:
// t1Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "t1.h"
#include "t1Dlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CT1Dlg dialog

CT1Dlg::CT1Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CT1Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CT1Dlg)
	m_nNO = 0;
	m_strName = _T("");
	m_nType = 0;
	m_nValue = 0;
	m_strDesc = _T("");
	m_nDBNO = 0;
	m_nAddr = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	iItem = -1;
	iTotal = 0;
	OnUpdate = FALSE;
}

void CT1Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CT1Dlg)
	DDX_Control(pDX, IDC_LIST1, m_list);
	DDX_Text(pDX, IDC_EDIT1, m_nNO);
	DDX_Text(pDX, IDC_EDIT2, m_strName);
	DDX_Text(pDX, IDC_EDIT3, m_nType);
	DDX_Text(pDX, IDC_EDIT4, m_nValue);
	DDX_Text(pDX, IDC_EDIT5, m_strDesc);
	DDX_Text(pDX, IDC_EDIT6, m_nDBNO);
	DDX_Text(pDX, IDC_EDIT7, m_nAddr);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CT1Dlg, CDialog)
	//{{AFX_MSG_MAP(CT1Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTN_ADD, OnAdd)
	ON_BN_CLICKED(IDC_BTN_DELETE, OnDelete)
	ON_BN_CLICKED(IDC_BTN_MODIFY, OnModify)
	ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST1, OnItemchangedList1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CT1Dlg message handlers

BOOL CT1Dlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	m_list.InsertColumn(0,"NO",LVCFMT_LEFT,50);
	m_list.InsertColumn(1,"Name",LVCFMT_LEFT,100);
	m_list.InsertColumn(2,"Type",LVCFMT_LEFT,80);
	m_list.InsertColumn(3,"Value",LVCFMT_LEFT,80);
	m_list.InsertColumn(4,"Desc",LVCFMT_LEFT,200);
	m_list.InsertColumn(5,"DBNO",LVCFMT_LEFT,50);
	m_list.InsertColumn(6,"Addr",LVCFMT_LEFT,50);

	set.Open("t2.dat");

	CString str;
	int i=0;

	while(!set.IsEOF())
	{
		str.Format("%03d",set.NO);
		m_list.InsertItem(i,str);
		m_list.SetItemText(i,0,str);
		m_list.SetItemText(i,1,set.Name);
		str.Format("%d",set.Type);
		m_list.SetItemText(i,2,str);
		str.Format("%d",set.Value);
		m_list.SetItemText(i,3,str);
		m_list.SetItemText(i,4,set.Desc);
		str.Format("%d",set.DBNO);
		m_list.SetItemText(i,5,str);
		str.Format("%d",set.Addr);
		m_list.SetItemText(i,6,str);
		set.MoveNext();
		i++;
	}

	iTotal = i;

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CT1Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CT1Dlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CT1Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CT1Dlg::OnAdd() 
{
	UpdateData(TRUE);

	set.NO = m_nNO;
	set.Name = m_strName;
	set.Type = m_nType;
	set.Value = m_nValue;
	set.Desc = m_strDesc;
	set.DBNO = m_nDBNO;
	set.Addr = m_nAddr;

	if (set.AddNew())
	{
		CString str;
		str.Format("%03d",set.NO);
		m_list.InsertItem(iTotal,str);
		m_list.SetItemText(iTotal,0,str);
		m_list.SetItemText(iTotal,1,set.Name);
		str.Format("%d",set.Type);
		m_list.SetItemText(iTotal,2,str);
		str.Format("%d",set.Value);
		m_list.SetItemText(iTotal,3,str);
		m_list.SetItemText(iTotal,4,set.Desc);
		str.Format("%d",set.DBNO);
		m_list.SetItemText(iTotal,5,str);
		str.Format("%d",set.Addr);
		m_list.SetItemText(iTotal,6,str);
		iTotal++;
	}
	else
	{
		AfxMessageBox("Add Failed.");
	}
}

void CT1Dlg::OnDelete() 
{
	if (iItem < 0) return;
	
	set.MoveFirst();
	set.Move(iItem);

	if (set.Delete())
	{
		m_list.DeleteItem(iItem);
	}
}

void CT1Dlg::OnModify() 
{
	if (iItem < 0) return;

	UpdateData(TRUE);

	set.MoveFirst();
	set.Move(iItem);

	set.NO = m_nNO;
	set.Name = m_strName;
	set.Type = m_nType;
	set.Value = m_nValue;
	set.Desc = m_strDesc;
	set.DBNO = m_nDBNO;
	set.Addr = m_nAddr;

	if (set.Update())
	{
		OnUpdate = TRUE;

		CString str;
		str.Format("%03d",set.NO);
		m_list.SetItemText(iItem,0,str);
		m_list.SetItemText(iItem,1,set.Name);
		str.Format("%d",set.Type);
		m_list.SetItemText(iItem,2,str);
		str.Format("%d",set.Value);
		m_list.SetItemText(iItem,3,str);
		m_list.SetItemText(iItem,4,set.Desc);
		str.Format("%d",set.DBNO);
		m_list.SetItemText(iItem,5,str);
		str.Format("%d",set.Addr);
		m_list.SetItemText(iItem,6,str);

		OnUpdate = FALSE;
		
		UpdateData(FALSE);
	}
	else
	{
		AfxMessageBox("Update Failed.");
	}
}

void CT1Dlg::OnItemchangedList1(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
	// TODO: Add your control notification handler code here
	iItem = pNMListView->iItem;

	if (!OnUpdate)
	{

	if (iItem>=0)
	{
		m_nNO = atoi(LPCTSTR(m_list.GetItemText(iItem,0)));
		m_strName = m_list.GetItemText(iItem,1);
		m_nType = atoi(LPCTSTR(m_list.GetItemText(iItem,2)));
		m_nValue = atoi(LPCTSTR(m_list.GetItemText(iItem,3)));
		m_strDesc = m_list.GetItemText(iItem,4);
		m_nDBNO = atoi(LPCTSTR(m_list.GetItemText(iItem,5)));
		m_nAddr = atoi(LPCTSTR(m_list.GetItemText(iItem,6)));
		UpdateData(FALSE);
	}
	else
	{
		m_nNO = 0;
		m_strName = _T("");
		m_nType = 0;
		m_nValue = 0;
		m_strDesc = _T("");
		m_nDBNO = 0;
		m_nAddr = 0;
		UpdateData(FALSE);
	}
	
	}
	*pResult = 0;
}

BOOL CT1Dlg::DestroyWindow() 
{
	set.Close();
	
	return CDialog::DestroyWindow();
}

⌨️ 快捷键说明

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