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

📄 userregdlg.cpp

📁 一个简单的VC数据库程序! 一个简单的VC数据库程序!
💻 CPP
字号:
// UserRegDlg.cpp : implementation file
//

#include "stdafx.h"
#include "UserReg.h"
#include "UserRegDlg.h"
#include "UserInfoRst.h"
#include "LoginDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CUserRegDlg dialog

CUserRegDlg::CUserRegDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CUserRegDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CUserRegDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CUserRegDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CUserRegDlg)
	DDX_Control(pDX, IDC_LIST1, m_Main);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CUserRegDlg, CDialog)
	//{{AFX_MSG_MAP(CUserRegDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_EXIT, OnButtonExit)
	ON_BN_CLICKED(IDC_BUTTON_ADD, OnButtonAdd)
	ON_BN_CLICKED(IDC_BUTTON_MOD, OnButtonMod)
	ON_BN_CLICKED(IDC_BUTTON_DEL, OnButtonDel)
	ON_BN_CLICKED(IDC_BUTTON_SHOW, OnButtonShow)
	ON_COMMAND(IDM_ADD, OnAdd)
	ON_COMMAND(IDM_DEL, OnDel)
	ON_COMMAND(IDM_EXIT, OnExit)
	ON_COMMAND(IDM_MOD, OnMod)
	ON_NOTIFY(NM_DBLCLK, IDC_LIST1, OnDblclkList1)
	ON_BN_CLICKED(IDC_BUTTON_REFRESH, OnButtonRefresh)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CUserRegDlg message handlers

BOOL CUserRegDlg::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
	CLoginDlg LoginDlg;
	LoginDlg.DoModal();

	m_Main.SetTextColor(RGB (0, 0, 255));
	m_Main.SetTextBkColor(RGB (140, 180, 220));
	m_Main.SetBkColor(RGB (140, 180, 220));

	m_Main.InsertColumn(0,"学号",LVCFMT_LEFT,80);
	m_Main.InsertColumn(1,"姓名",LVCFMT_LEFT,80);
	m_Main.InsertColumn(2,"籍贯",LVCFMT_LEFT,80);
	m_Main.InsertColumn(3,"电话",LVCFMT_LEFT,80);
	m_Main.InsertColumn(4,"学院",LVCFMT_LEFT,80);
	m_Main.InsertColumn(5,"成绩",LVCFMT_LEFT,80);
	m_Main.ModifyStyle(0,LVS_REPORT|LVS_SHOWSELALWAYS|LVS_SINGLESEL);
	m_Main.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES|
		LVS_EX_HEADERDRAGDROP);
	CUserInfoRst m_info;
	try
	{
		if(m_info.IsOpen())
			m_info.Close();
		m_info.Open();
		m_info.m_strSort="UserNo DESC";
		m_info.Requery();
		while(!m_info.IsEOF())
		{
			CString strno="";
			CString strgrade="";
			strno.Format("%d",m_info.m_UserNo);
			strgrade.Format("%d",m_info.m_Grade);
			int nItem=m_Main.InsertItem(0,strno);
			m_Main.SetItemText(nItem,1,m_info.m_Name);
			m_Main.SetItemText(nItem,2,m_info.m_City);
			m_Main.SetItemText(nItem,3,m_info.m_Tel);
			m_Main.SetItemText(nItem,4,m_info.m_Col);
			m_Main.SetItemText(nItem,5,strgrade);
			m_info.MoveNext();
		}
	}
	catch(...)
	{
		m_info.Close();
	}
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CUserRegDlg::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 CUserRegDlg::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 CUserRegDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CUserRegDlg::OnButtonExit() 
{
	// TODO: Add your control notification handler code here
	CDialog::OnCancel();
}

void CUserRegDlg::OnButtonAdd() 
{
	// TODO: Add your control notification handler code here
	CString strNo="";
	CString strName="";
	CString strCity="";
	CString strTel="";
	CString strCol="";
	CString strGrade="";

	GetDlgItem(IDC_EDIT_NO)->GetWindowText(strNo);
	GetDlgItem(IDC_EDIT_NAME1)->GetWindowText(strName);
	GetDlgItem(IDC_EDIT_CITY)->GetWindowText(strCity);
	GetDlgItem(IDC_EDIT_TEL)->GetWindowText(strTel);
	GetDlgItem(IDC_EDIT_COL)->GetWindowText(strCol);
	GetDlgItem(IDC_EDIT_GRADE)->GetWindowText(strGrade);

	CUserInfoRst m_info;
	
	if(strlen(strNo)<=0||strlen(strName)<=0)
	{
		MessageBox("学号和姓名不能为空!");
		return;
	}
	if(strlen(strNo)>10)
	{
		MessageBox("学号不能超过10位!");
		return;
	}

	int iNo=atoi(strNo);
	long iGrade=atoi(strGrade);
	
	try
	{
		if(m_info.IsOpen())
			m_info.Close();
		m_info.Open();

		m_info.AddNew();
		m_info.m_UserNo=iNo;
		m_info.m_Name=strName;
		m_info.m_City=strCity;
		m_info.m_Tel=strTel;
		m_info.m_Col=strCol;
		m_info.m_Grade=iGrade;
		m_info.Update();
		m_info.Close();
		int nItem=m_Main.InsertItem(0,strNo);
		m_Main.SetItemText(nItem,1,strName);
		m_Main.SetItemText(nItem,2,strCity);
		m_Main.SetItemText(nItem,3,strTel);
		m_Main.SetItemText(nItem,4,strCol);
		m_Main.SetItemText(nItem,5,strGrade);
		MessageBox("恭喜,用户信息添加成功!");
	}
	catch(...)
	{
		m_info.Close();
		MessageBox("用户信息添加失败,请确认你输入的用户信息!");
	}
}

void CUserRegDlg::OnButtonMod() 
{
	// TODO: Add your control notification handler code here
	int nItem=m_Main.GetNextItem(-1,LVNI_SELECTED);
	if(nItem<0)
	{
		MessageBox("未选中其中一项,不能进行修改操作!");
		return;
	}
	CString strNo="";
	CString strName="";
	CString strCity="";
	CString strTel="";
	CString strCol="";
	CString strGrade="";

	GetDlgItem(IDC_EDIT_NO)->GetWindowText(strNo);
	GetDlgItem(IDC_EDIT_NAME1)->GetWindowText(strName);
	GetDlgItem(IDC_EDIT_CITY)->GetWindowText(strCity);
	GetDlgItem(IDC_EDIT_TEL)->GetWindowText(strTel);
	GetDlgItem(IDC_EDIT_COL)->GetWindowText(strCol);
	GetDlgItem(IDC_EDIT_GRADE)->GetWindowText(strGrade);

	CUserInfoRst m_info;
	
		if(strlen(strNo)<=0||strlen(strName)<=0)
	{
		MessageBox("学号和姓名不能为空!");
		return;
	}
	if(strlen(strNo)>10)
	{
		MessageBox("学号不能超过10位!");
		return;
	}

	int iNo=atoi(strNo);
	long iGrade=atoi(strGrade);

	try
	{
		if(m_info.IsOpen())
			m_info.Close();
		m_info.m_strFilter.Format("UserNo=%s",m_Main.GetItemText(nItem,0));
		m_info.Open();
		if(!m_info.IsEOF())
		{
			m_info.Edit();

		m_info.m_UserNo=iNo;
		m_info.m_Name=strName;
		m_info.m_City=strCity;
		m_info.m_Tel=strTel;
		m_info.m_Col=strCol;
		m_info.m_Grade=iGrade;

		m_info.Update();
		m_Main.SetItemText(nItem,0,strNo);		
		m_Main.SetItemText(nItem,1,strName);
		m_Main.SetItemText(nItem,2,strCity);
		m_Main.SetItemText(nItem,3,strTel);
		m_Main.SetItemText(nItem,4,strCol);
		m_Main.SetItemText(nItem,5,strGrade);
		MessageBox("恭喜,用户信息修改成功!");
		}
		else
		{
			MessageBox("未找到该学号的用户!");
		}
		m_info.Close();
	}
	catch(...)
	{
		m_info.Close();
		MessageBox("用户信息修改失败,请确认你输入的用户信息!");
	}
}

void CUserRegDlg::OnButtonDel() 
{
	// TODO: Add your control notification handler code here
	int nItem=m_Main.GetNextItem(-1,LVNI_SELECTED);
	if(nItem<0)
	{
		MessageBox("未选中其中一项,不能进行删除操作!");
		return;
	}

	CUserInfoRst m_info;

	try
	{
		if(m_info.IsOpen())
			m_info.Close();	

		m_info.m_strFilter.Format("UserNo =%s",m_Main.GetItemText(nItem,0));
		m_info.Open();

		if(!m_info.IsEOF())
		{
			m_info.Delete();
			m_Main.DeleteItem(nItem);
			MessageBox("恭喜,用户信息删除成功!");
		}
		else
		{
			MessageBox("未找到该学号的用户!");
		}
		m_info.Close();
	}
	catch(...)
	{
		m_info.Close();
		MessageBox("用户信息删除失败!");
	}
}
void CUserRegDlg::OnButtonShow() 
{
	// TODO: Add your control notification handler code here
		int nItem=m_Main.GetNextItem(-1,LVNI_SELECTED);
	if(nItem<0)
	{
		MessageBox("未选中其中一项,不能进行修改操作!");
		return;
	}
	CString strNo=m_Main.GetItemText(nItem,0);
	CString strName=m_Main.GetItemText(nItem,1);
	CString strCity=m_Main.GetItemText(nItem,2);
	CString strTel=m_Main.GetItemText(nItem,3);
	CString strCol=m_Main.GetItemText(nItem,4);
	CString strGrade=m_Main.GetItemText(nItem,5);

	GetDlgItem(IDC_EDIT_NO)->SetWindowText(strNo);
	GetDlgItem(IDC_EDIT_NAME1)->SetWindowText(strName);
	GetDlgItem(IDC_EDIT_CITY)->SetWindowText(strCity);
	GetDlgItem(IDC_EDIT_TEL)->SetWindowText(strTel);
	GetDlgItem(IDC_EDIT_COL)->SetWindowText(strCol);
	GetDlgItem(IDC_EDIT_GRADE)->SetWindowText(strGrade);
}

void CUserRegDlg::OnAdd() 
{
	// TODO: Add your command handler code here
	OnButtonAdd();
}

void CUserRegDlg::OnDel() 
{
	// TODO: Add your command handler code here
	OnButtonDel();
}

void CUserRegDlg::OnExit() 
{
	// TODO: Add your command handler code here
	OnButtonExit();
}

void CUserRegDlg::OnMod() 
{
	// TODO: Add your command handler code here
	OnButtonMod();
}

void CUserRegDlg::OnDblclkList1(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	int nItem=m_Main.GetNextItem(-1,LVNI_SELECTED);
	if(nItem<0)
	{
		MessageBox("未选中其中一项,不能进行修改操作!");
		return;
	}
	CString strNo=m_Main.GetItemText(nItem,0);
	CString strName=m_Main.GetItemText(nItem,1);
	CString strCity=m_Main.GetItemText(nItem,2);
	CString strTel=m_Main.GetItemText(nItem,3);
	CString strCol=m_Main.GetItemText(nItem,4);
	CString strGrade=m_Main.GetItemText(nItem,5);

	GetDlgItem(IDC_EDIT_NO)->SetWindowText(strNo);
	GetDlgItem(IDC_EDIT_NAME1)->SetWindowText(strName);
	GetDlgItem(IDC_EDIT_CITY)->SetWindowText(strCity);
	GetDlgItem(IDC_EDIT_TEL)->SetWindowText(strTel);
	GetDlgItem(IDC_EDIT_COL)->SetWindowText(strCol);
	GetDlgItem(IDC_EDIT_GRADE)->SetWindowText(strGrade);
	*pResult = 0;
}

void CUserRegDlg::OnButtonRefresh() 
{
	// TODO: Add your control notification handler code here
	m_Main.DeleteAllItems();
		CUserInfoRst m_info;
	try
	{
		if(m_info.IsOpen())
			m_info.Close();
		m_info.Open();
		m_info.m_strSort="UserNo DESC";
		m_info.Requery();
		while(!m_info.IsEOF())
		{
			CString strno="";
			CString strgrade="";
			strno.Format("%d",m_info.m_UserNo);
			strgrade.Format("%d",m_info.m_Grade);
			int nItem=m_Main.InsertItem(0,strno);
			m_Main.SetItemText(nItem,1,m_info.m_Name);
			m_Main.SetItemText(nItem,2,m_info.m_City);
			m_Main.SetItemText(nItem,3,m_info.m_Tel);
			m_Main.SetItemText(nItem,4,m_info.m_Col);
			m_Main.SetItemText(nItem,5,strgrade);
			m_info.MoveNext();
		}
	}
	catch(...)
	{
		m_info.Close();
	}
}

⌨️ 快捷键说明

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