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

📄 studentdlg.cpp

📁 一个用VC写的学生成绩管理系统,其中对数据库的访问是采用ODBC方式.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// StudentDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Student.h"
#include "StudentDlg.h"
#include "odbc.h"
#include "Adddlg.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()

/////////////////////////////////////////////////////////////////////////////
// CStudentDlg dialog

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

void CStudentDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CStudentDlg)
	DDX_Control(pDX, IDC_COMBO3, m_sort);
	DDX_Control(pDX, IDC_LIST1, m_list);
	DDX_Control(pDX, IDC_COMBO2, m_comb_type);
	DDX_Control(pDX, IDC_COMBO1, m_comb_class);
	DDX_Text(pDX, IDC_EDIT1, m_find);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CStudentDlg, CDialog)
	//{{AFX_MSG_MAP(CStudentDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDOK, OnSearch)
	ON_CBN_CLOSEUP(IDC_COMBO1, OnCloseupCombo1)
	ON_BN_CLICKED(IDC_Bseeall, OnBseeall)
	ON_BN_CLICKED(IDC_BADD, OnBadd)
	ON_NOTIFY(NM_RCLICK, IDC_LIST1, OnRclickList1)
	ON_COMMAND(ID_MDelete, OnMDelete)
	ON_COMMAND(ID_MADD, OnMadd)
	ON_WM_SIZE()
	ON_CBN_CLOSEUP(IDC_COMBO3, OnCloseup_sort)
	ON_COMMAND(ID_MEdit, OnMEdit)
	ON_BN_CLICKED(IDC_Babout, OnBabout)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CStudentDlg message handlers

BOOL CStudentDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	//调用Register函数注册数据库
		//获取当前文件路径
	char *path=new char[100];
	::GetCurrentDirectory(100,path);
	CString path1;
	path1.Format("%s",path);
	CString path2=path1+"\\student.mdb";
	RegisterDBSource("student.mdb",path2);

	// Add "About..." menu item to system menu.
	m_comb_class.AddString("1班");
	m_comb_class.AddString("2班");
	m_comb_class.AddString("3班");
	m_comb_class.SetCurSel(0);

	m_comb_type.AddString("姓名");
	m_comb_type.AddString("性别");
	m_comb_type.AddString("学号");
	m_comb_type.AddString("排名");
	m_comb_type.SetCurSel(0);
	m_sort.AddString("学号");
	m_sort.AddString("总分");
	m_sort.AddString("排名");
	m_sort.AddString("英语");
	m_sort.AddString("数学");
	m_sort.AddString("物理");
	m_sort.AddString("C++");
	m_sort.AddString("数据结构");
	m_sort.SetCurSel(0);
	//添加表格项目
	m_list.InsertColumn(0,"学号",LVCFMT_CENTER,100,-1);//100为表项宽度
	m_list.InsertColumn(1,"姓名",LVCFMT_CENTER,80,-1);
	m_list.InsertColumn(2,"性别",LVCFMT_CENTER,50,-1);
	m_list.InsertColumn(3,"班级",LVCFMT_CENTER,50,-1);
	m_list.InsertColumn(4,"英语",LVCFMT_CENTER,80,-1);
	m_list.InsertColumn(5,"数学",LVCFMT_CENTER,80,-1);
	m_list.InsertColumn(6,"物理",LVCFMT_CENTER,80,-1);
	m_list.InsertColumn(7,"C++",LVCFMT_CENTER,80,-1);
	m_list.InsertColumn(8,"数据结构",LVCFMT_CENTER,80,-1);
	m_list.InsertColumn(9,"总分",LVCFMT_CENTER,80,-1);
	m_list.InsertColumn(10,"排名",LVCFMT_CENTER,80,-1);
	m_list.InsertColumn(11,"备注",LVCFMT_CENTER,80,-1);
	m_list.SetExtendedStyle(m_list.GetExtendedStyle() 
		| LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);

	sql="select * from 1班";
		RefreshData(sql);
		UpdateData(FALSE);
	/////默认的是以学号排序/////////////////////
	m_record.m_strSort="学号";
	OnBseeall();
	// 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
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CStudentDlg::RegisterDBSource(CString strDSName, CString strDBPath)
{
	HKEY hKey;
	DWORD nLabel; 	
	CString strBaseKey = _T("SOFTWARE\\ODBC\\ODBC.INI");
	CString strMid = strBaseKey + _T("\\ODBC Data Sources") ;	
	if(strDSName.IsEmpty()) return;
	if(strDBPath.IsEmpty()) return;	
	CString strDataSource = strBaseKey + _T("\\") + strDSName;	
	CString strMdb = _T("Microsoft Access Driver (*.mdb)");
	CString strDBDriver = _T("C:\\WINNT\\System32\\odbcjt32.dll");
	CString strFIL = _T("Ms Access;");
	CString strUID = _T("");	
	RegCreateKeyEx(HKEY_CURRENT_USER,
		strMid,
		0, 
		NULL, 
		REG_OPTION_NON_VOLATILE, 
		KEY_ALL_ACCESS,
		NULL,
		&hKey, 
		&nLabel );//获取数据源键值句柄	
	RegSetValueEx(hKey,
		strDSName,
		0,
		REG_SZ,
		(const unsigned char *)((LPCTSTR)strMdb),
		strlen((LPCTSTR)strMdb)+1);///设置数据源类型	
	RegCreateKeyEx(HKEY_CURRENT_USER,
		strDataSource,
		0, 
		NULL, 
		REG_OPTION_NON_VOLATILE, 
		KEY_ALL_ACCESS,
		NULL,
		&hKey, 
		&nLabel );//创建数据源子键	
	RegSetValueEx(hKey,
		_T("DBQ"),
		0,
		REG_SZ,
		(const unsigned char *)((LPCTSTR)strDBPath),
		strlen((LPCTSTR)strDBPath)+1);//数据库表的全路径	
	RegSetValueEx(hKey,
		_T("Driver"),
		0,
		REG_SZ,
		(const unsigned char *)((LPCTSTR)strDBDriver),
		strlen((LPCTSTR)strDBDriver)+1);//ODBC驱动的全路径	
	RegSetValueEx(hKey,
		_T("FIL"),
		0,
		REG_SZ,
		(const unsigned char *)((LPCTSTR)strFIL),
		strlen((LPCTSTR)strFIL)+1);//表的类型	
	RegSetValueEx(hKey,
		_T("UID"),
		0,
		REG_SZ,
		(const unsigned char *)((LPCTSTR)strUID),
		strlen((LPCTSTR)strUID)+1);//必须项	
	DWORD DriverId = (DWORD)25;
	RegSetValueEx(hKey,
		_T("DriverId"),
		0,
		REG_DWORD,
		(const BYTE *)(&DriverId),
		sizeof(DWORD));//必须项
	DWORD SafeTrans = (DWORD)0;
	RegSetValueEx(hKey,
		_T("SafeTransactions"),
		0,
		REG_DWORD,
		(const BYTE *)(&SafeTrans),
		sizeof(DWORD));//可选项
}

void CStudentDlg::OnSearch() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	if(m_find.GetLength()<1)
	{
		MessageBox("请输入查询内容!");
		return;
	}
	int num_class,num_type;	
	num_class=m_comb_class.GetCurSel();
	num_type=m_comb_type.GetCurSel();
	switch(num_type)
	{
	case 0:
		type="姓名";
		break;
	case 1:
		type="性别";
		break;
	case 2:
		type="学号";
		break;
	case 3:
		type="排名";
		break;
	}

	switch(num_class)
	{
	case 0:
		sql="select * from 1班 where "+type+" like '%"+m_find+"%'";
		RefreshData(sql);
		UpdateData(FALSE);
		break;
	case 1:
		sql="select * from 2班 where "+type+" like '%"+m_find+"%'";
		RefreshData(sql);
		UpdateData(FALSE);
		break;
	case 2:
		sql="select * from 3班 where "+type+" like '%"+m_find+"%'";
		RefreshData(sql);
		UpdateData(FALSE);
		break;
	}
}

void CStudentDlg::RefreshData(CString strSQL)
{
	m_list.SetFocus();
	m_list.DeleteAllItems();
	m_list.SetRedraw(FALSE);

	if(!m_record.Open(AFX_DB_USE_DEFAULT_TYPE,strSQL))   
	{
		MessageBox("打开数据库失败!","数据库错误",MB_OK);
		return ;
	}	
	int i=0;
	CString paiming;
//	m_record.MoveFirst();
	while(!m_record.IsEOF())
	{
		m_list.InsertItem(i,"12");
		m_list.SetItemText(i,0,m_record.m_column1);
		m_list.SetItemText(i,1,m_record.m_column2);
		m_list.SetItemText(i,2,m_record.m_column3);
		m_list.SetItemText(i,3,m_record.m_column4);
		m_list.SetItemText(i,4,m_record.m_column5);
		m_list.SetItemText(i,5,m_record.m_column6);
		m_list.SetItemText(i,6,m_record.m_column7);
		m_list.SetItemText(i,7,m_record.m_C__);
		m_list.SetItemText(i,8,m_record.m_column8);
		m_list.SetItemText(i,9,m_record.m_column9);
		paiming.Format("%d",i+1);//////////////////////////////
		m_list.SetItemText(i,10,m_record.m_column10);
		m_list.SetItemText(i,11,m_record.m_column11);		
		m_record.MoveNext();
		i++;
	}
	m_record.Close();
	m_list.SetRedraw(TRUE);
}

void CStudentDlg::OnCloseupCombo1() 
{

⌨️ 快捷键说明

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