📄 mypredlg.cpp
字号:
// mypreDlg.cpp : implementation file
//
#include "stdafx.h"
#include "mypre.h"
#include "mypreDlg.h"
//#include <comdef.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern CMypreApp theApp; //指向ADO的指针
CListCtrl* pListCtrl;
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CMypreDlg dialog
CMypreDlg::CMypreDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMypreDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMypreDlg)
m_polity = 0;
m_physics = 0;
m_number = _T("");
m_name = _T("");
m_math = 0;
m_history = 0;
m_geography = 0;
m_english = 0;
m_chinese = 0;
m_chemistry = 0;
m_biology = 0;
m_all_grade = 0;
m_avg_grade = 0.0;
m_query = _T("");
m_radio = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMypreDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMypreDlg)
DDX_Control(pDX, IDC_COMBO, m_combo);
DDX_Control(pDX, IDC_LIST, m_list);
DDX_Text(pDX, IDC_EDIT_polity, m_polity);
DDV_MinMaxInt(pDX, m_polity, 0, 100);
DDX_Text(pDX, IDC_EDIT_physics, m_physics);
DDV_MinMaxInt(pDX, m_physics, 0, 100);
DDX_Text(pDX, IDC_EDIT_number, m_number);
DDV_MaxChars(pDX, m_number, 20);
DDX_Text(pDX, IDC_EDIT_name, m_name);
DDV_MaxChars(pDX, m_name, 20);
DDX_Text(pDX, IDC_EDIT_math, m_math);
DDV_MinMaxInt(pDX, m_math, 0, 100);
DDX_Text(pDX, IDC_EDIT_history, m_history);
DDV_MinMaxInt(pDX, m_history, 0, 100);
DDX_Text(pDX, IDC_EDIT_geography, m_geography);
DDV_MinMaxInt(pDX, m_geography, 0, 100);
DDX_Text(pDX, IDC_EDIT_english, m_english);
DDV_MinMaxInt(pDX, m_english, 0, 100);
DDX_Text(pDX, IDC_EDIT_chinese, m_chinese);
DDV_MinMaxInt(pDX, m_chinese, 0, 100);
DDX_Text(pDX, IDC_EDIT_chemistry, m_chemistry);
DDV_MinMaxInt(pDX, m_chemistry, 0, 100);
DDX_Text(pDX, IDC_EDIT_biology, m_biology);
DDV_MinMaxInt(pDX, m_biology, 0, 100);
DDX_Text(pDX, IDC_EDIT_all_grade, m_all_grade);
DDX_Text(pDX, IDC_EDIT_avg_grade, m_avg_grade);
DDX_Text(pDX, IDC_EDIT_query, m_query);
DDV_MaxChars(pDX, m_query, 20);
DDX_Radio(pDX, IDC_RADIO_number, m_radio);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMypreDlg, CDialog)
//{{AFX_MSG_MAP(CMypreDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BTN_add, OnBTNadd)
ON_BN_CLICKED(IDC_BTN_update, OnBTNupdate)
ON_BN_CLICKED(IDC_BTN_delete, OnBTNdelete)
ON_NOTIFY(HDN_ITEMCLICK, IDC_LIST, OnItemclickList)
ON_NOTIFY(NM_CLICK, IDC_LIST, OnClickList)
ON_BN_CLICKED(IDC_BUTTON5, OnBTNsort)
ON_CBN_SELCHANGE(IDC_COMBO, OnSelchangeCombo)
ON_BN_CLICKED(IDC_RADIO_number, OnRADIOnumber)
ON_BN_CLICKED(IDC_RADIO_name, OnRADIOname)
ON_BN_CLICKED(IDC_BTN_query, OnBTNquery)
ON_BN_CLICKED(IDC_BUTTON6, OnBTN_exit)
ON_BN_CLICKED(IDC_BTN_about, OnBTNabout)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMypreDlg message handlers
BOOL CMypreDlg::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
//////*****************************************************************
pListCtrl= &m_list;
//////*****************************************************************
//定制列表框样式
m_list.ModifyStyle(0,LVS_REPORT|LVS_SHOWSELALWAYS|LVS_SINGLESEL);
m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES|LVS_EX_HEADERDRAGDROP);
//添加列表框列
m_list.InsertColumn(0,"ID",LVCFMT_CENTER,30);
m_list.InsertColumn(1,"学号",LVCFMT_CENTER,60);
m_list.InsertColumn(2,"姓名",LVCFMT_CENTER,100);
m_list.InsertColumn(3,"语文",LVCFMT_CENTER,50);
m_list.InsertColumn(4,"数学",LVCFMT_CENTER,50);
m_list.InsertColumn(5,"英语",LVCFMT_CENTER,50);
m_list.InsertColumn(6,"物理",LVCFMT_CENTER,50);
m_list.InsertColumn(7,"化学",LVCFMT_CENTER,50);
m_list.InsertColumn(8,"政治",LVCFMT_CENTER,50);
m_list.InsertColumn(9,"历史",LVCFMT_CENTER,50);
m_list.InsertColumn(10,"地理",LVCFMT_CENTER,50);
m_list.InsertColumn(11,"生物",LVCFMT_CENTER,50);
m_list.InsertColumn(12,"总成绩",LVCFMT_CENTER,60);
m_list.InsertColumn(13,"平均成绩",LVCFMT_CENTER,60);
//***********************************************************
m_combo.AddString("总成绩");
m_combo.AddString("语文");
m_combo.AddString("数学");
m_combo.AddString("英语");
m_combo.AddString("物理");
m_combo.AddString("化学");
m_combo.AddString("政治");
m_combo.AddString("历史");
m_combo.AddString("地理");
m_combo.AddString("生物");
////*********************************************************
////*********************************************************
// 使用ADO创建数据库记录集
m_pRecordset.CreateInstance(__uuidof(Recordset));
// 在ADO操作中建议语句中要常用try...catch()来捕获错误信息,
// 因为它有时会经常出现一些想不到的错误。
try
{
m_pRecordset->Open("SELECT * FROM StudentGrade", // 查询StudentGrade表中所有字段
theApp.m_pConnection.GetInterfacePtr(), // 获取库接库的IDispatch指针
adOpenDynamic,
adLockOptimistic,
adCmdText);
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
/////读出Access中已经存在的数据
ReadData();
/////**********************************************************
/////**********************************************************
return TRUE; // return TRUE unless you set the focus to a control
}
void CMypreDlg::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 CMypreDlg::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 CMypreDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
////////////////////////////////////////////////////////////
//函数:
//功能:添加数据
////////////////////////////////////////////////////////////
void CMypreDlg::OnBTNadd()
{
// TODO: Add your control notification handler code here
UpdateData();
if(m_number==""||m_name=="")
{
AfxMessageBox("学号和姓名不能为空!");
return;
}
if(m_chinese<0||m_chinese>100)
{
AfxMessageBox("语文成绩输入有误!");
return;
}
if(m_math<0||m_math>100)
{
AfxMessageBox("数学成绩输入有误!");
return;
}
if(m_english<0||m_english>100)
{
AfxMessageBox("英语成绩输入有误!");
return;
}
if(m_physics<0||m_physics>100)
{
AfxMessageBox("物理成绩输入有误!");
return;
}
if(m_chemistry<0||m_chemistry>100)
{
AfxMessageBox("化学成绩输入有误!");
return;
}
if(m_polity<0||m_polity>100)
{
AfxMessageBox("政治成绩输入有误!");
return;
}
if(m_history<0||m_history>100)
{
AfxMessageBox("历史成绩输入有误!");
return;
}
if(m_geography<0||m_geography>100)
{
AfxMessageBox("地理成绩输入有误!");
return;
}
if(m_biology<0||m_biology>100)
{
AfxMessageBox("生物成绩输入有误!");
return;
}
try
{
m_all_grade=m_chinese+m_math+m_english+m_physics+m_chemistry+m_polity+m_history+m_geography+m_biology;
m_avg_grade=m_all_grade/9.0;
// int ai=(int)(m_avg_grade*100);
// m_avg_grade=((float)ai)/100;
m_pRecordset->AddNew();
m_pRecordset->PutCollect("Number",_variant_t(m_number));
m_pRecordset->PutCollect("Name",_variant_t(m_name));
m_pRecordset->PutCollect("Chinese", _variant_t((short)m_chinese));
m_pRecordset->PutCollect("Math",_variant_t((short)m_math));
m_pRecordset->PutCollect("English",_variant_t((short)m_english));
m_pRecordset->PutCollect("Physics",_variant_t((short)m_physics));
m_pRecordset->PutCollect("Chemistry",_variant_t((short)m_chemistry));
m_pRecordset->PutCollect("Polity",_variant_t((short)m_polity));
m_pRecordset->PutCollect("History",_variant_t((short)m_history));
m_pRecordset->PutCollect("Geography",_variant_t((short)m_geography));
m_pRecordset->PutCollect("Biology",_variant_t((short)m_biology));
m_pRecordset->PutCollect("All_grade",_variant_t((short)m_all_grade));
m_pRecordset->PutCollect("Avg_grade",_variant_t((float)m_avg_grade));
m_pRecordset->Update();
AfxMessageBox("添加成功!");
int nCurSel=m_list.GetItemCount();
SetIndex();///////////////////////////////////////////////
UpdateData();
ReadData();
m_list.SetHotItem(nCurSel);
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
}
/////////////////////////////////////////////////////////////
//函数:
//功能:更新数据
/////////////////////////////////////////////////////////////
void CMypreDlg::OnBTNupdate()
{
// TODO: Add your control notification handler code here
UpdateData();
if(m_list.GetItemCount()==0||m_number==""||m_name=="")
{
AfxMessageBox("表中的记录为空或姓名和年龄没有填写!");
return;
}
else if(m_list.GetHotItem()<0||m_list.GetHotItem()>m_list.GetItemCount())
m_list.SetHotItem(0);
if(m_list.GetSelectedCount()<1)
{
AfxMessageBox("请选择要更改的记录!");
return;
}
if(m_chinese<0||m_chinese>100)
{
AfxMessageBox("语文成绩输入有误!");
return;
}
if(m_math<0||m_math>100)
{
AfxMessageBox("数学成绩输入有误!");
return;
}
if(m_english<0||m_english>100)
{
AfxMessageBox("英语成绩输入有误!");
return;
}
if(m_physics<0||m_physics>100)
{
AfxMessageBox("物理成绩输入有误!");
return;
}
if(m_chemistry<0||m_chemistry>100)
{
AfxMessageBox("化学成绩输入有误!");
return;
}
if(m_polity<0||m_polity>100)
{
AfxMessageBox("政治成绩输入有误!");
return;
}
if(m_history<0||m_history>100)
{
AfxMessageBox("历史成绩输入有误!");
return;
}
if(m_geography<0||m_geography>100)
{
AfxMessageBox("地理成绩输入有误!");
return;
}
if(m_biology<0||m_biology>100)
{
AfxMessageBox("生物成绩输入有误!");
return;
}
try
{
m_all_grade=m_chinese+m_math+m_english+m_physics+m_chemistry+m_polity+m_history+m_geography+m_biology;
m_avg_grade=m_all_grade/9.0;
m_pRecordset->PutCollect("Number",_variant_t(m_number));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -