📄 studentdlg.cpp
字号:
// StudentDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Student.h"
#include "StudentDlg.h"
#include "DrawDlg.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)
// NOTE: the ClassWizard will add member initialization here
//}}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_COMBO1, m_ComboTable);
DDX_Control(pDX, IDC_LIST1, m_ListCtrl);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CStudentDlg, CDialog)
//{{AFX_MSG_MAP(CStudentDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_DESTROY()
ON_CBN_SELCHANGE(IDC_COMBO1, OnSelchangeCombo1)
ON_BN_CLICKED(IDC_BUTTON_STAT, OnButtonStat)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CStudentDlg message handlers
BOOL CStudentDlg::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_ListCtrl.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
m_pConnection.CreateInstance(__uuidof(Connection)); // 初始化Connection指针
m_pConnection->ConnectionString
="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\\..\\student.mdb;";
// 此句代码在一行书写
m_pConnection->ConnectionTimeout = 30; // 允许连接超时时间,单位为秒
HRESULT hr = m_pConnection->Open("","","",0);
if (hr != S_OK)
MessageBox("无法连接指定的数据库!");
m_ComboTable.AddString( "学生基本信息表" );
m_ComboTable.AddString( "课程信息表" );
m_ComboTable.AddString( "学生成绩表" );
m_ComboTable.SetCurSel( 0 );
OnSelchangeCombo1();
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::OnDestroy()
{
CDialog::OnDestroy();
// TODO: Add your message handler code here
if (m_pConnection)
m_pConnection->Close();// 关闭连接
}
void CStudentDlg::DispAllRec(CString strSQL)
{
m_ListCtrl.DeleteAllItems();
int nColumnCount = m_ListCtrl.GetHeaderCtrl()->GetItemCount();
for (int i=0; i<nColumnCount; i++)
m_ListCtrl.DeleteColumn(0);
_CommandPtr pCmd;
pCmd.CreateInstance(__uuidof(Command)); // 初始化Command指针
pCmd->ActiveConnection = m_pConnection; // 指向已有的连接
pCmd->CommandText = _bstr_t(strSQL);
_RecordsetPtr pSet;
pSet.CreateInstance(__uuidof(Recordset)); // 初始化Recordset指针
pSet = pCmd->Execute(NULL, NULL, adCmdText );
// 建立列表控件的列表头
FieldsPtr flds = pSet->GetFields(); // 获取当前表的字段指针
_variant_t Index;
Index.vt = VT_I2;
m_ListCtrl.InsertColumn(0, "序号", LVCFMT_LEFT, 40 );
for (i = 0; i < (int)flds->GetCount(); i++) {
Index.iVal = i;
int nWidth = flds->GetItem(Index)->GetDefinedSize()*9;
if ( nWidth < 40 ) nWidth = 40;
if ( nWidth > 200 ) nWidth = 200;
m_ListCtrl.InsertColumn(i+1, (LPSTR)flds->GetItem(Index)->GetName(),
LVCFMT_LEFT, nWidth);
}
// 显示记录
_bstr_t str, value;
int nItem = 0;
CString strItem;
while(!pSet->adoEOF){
strItem.Format("%d", nItem+1);
m_ListCtrl.InsertItem(nItem, strItem );
for (i = 0; i < (int)flds->GetCount(); i++) {
Index.iVal = i;
str = flds->GetItem(Index)->GetName();
try{
value = pSet->GetCollect(str);
m_ListCtrl.SetItemText( nItem, i+1, (LPCSTR)value );
}catch(...)
{}
}
pSet->MoveNext();
nItem++;
}
pSet->Close();
}
void CStudentDlg::OnSelchangeCombo1()
{
// TODO: Add your control notification handler code here
int nIndex = m_ComboTable.GetCurSel();
if ( nIndex == CB_ERR ) return;
switch ( nIndex )
{
case 0: DispAllRec("SELECT * FROM student ORDER BY XH"); break;
case 1: DispAllRec("SELECT * FROM course ORDER BY KCH"); break;
case 2: DispAllRec("SELECT * FROM score ORDER BY XH"); break;
}
}
void CStudentDlg::OnButtonStat()
{
// TODO: Add your control notification handler code here
CDrawDlg dlg;
dlg.m_nMode = 1;
dlg.m_strTitle = "示例:这是一个圆饼图";
dlg.m_uData.Add( 2 ); dlg.m_uData.Add( 1 );
dlg.m_uData.Add( 11 ); dlg.m_uData.Add( 15 );
dlg.m_uData.Add( 24 ); dlg.m_uData.Add( 6 );
dlg.DoModal();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -