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

📄 labmisdlg.cpp

📁 可以实现基本的管理功能
💻 CPP
字号:
// LabMISDlg.cpp : implementation file
//

#include "stdafx.h"
#include "LabMIS.h"
#include "LabMISDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CLabMISDlg dialog

CLabMISDlg::CLabMISDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CLabMISDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CLabMISDlg)
	m_editName = _T("");
	m_editQueryName = _T("");
	m_editType = _T("");
	m_editSex = _T("");
	m_editPrj = _T("");
	m_editArea = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	CString strConnection="File Name=my_data2.udl";
	pDB=new CADODatabase;
	pDB->Open(strConnection);
	pRs=new CADORecordset(pDB);
}
CLabMISDlg::~CLabMISDlg()
{
	pDB->Close();
}
void CLabMISDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CLabMISDlg)
	DDX_Control(pDX, IDC_LIST_PERSON, m_listPerson);
	DDX_Text(pDX, IDC_EDIT_NAME, m_editName);
	DDX_Text(pDX, IDC_EDIT_QUERYNAME, m_editQueryName);
	DDX_Text(pDX, IDC_EDIT_TYPE, m_editType);
	DDX_Text(pDX, IDC_EDIT_SEX, m_editSex);
	DDX_Text(pDX, IDC_EDIT_PRJ, m_editPrj);
	DDX_Text(pDX, IDC_EDIT_AREA, m_editArea);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CLabMISDlg, CDialog)
	//{{AFX_MSG_MAP(CLabMISDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_FIRST, OnButtonFirst)
	ON_BN_CLICKED(IDC_BUTTON_ALL, OnButtonAll)
	ON_BN_CLICKED(IDC_BUTTON_LAST, OnButtonLast)
	ON_BN_CLICKED(IDC_BUTTON_PRE, OnButtonPre)
	ON_BN_CLICKED(IDC_BUTTON_NEXT, OnButtonNext)
	ON_BN_CLICKED(IDC_BUTTON_DELETE, OnButtonDelete)
	ON_NOTIFY(NM_CLICK, IDC_LIST_PERSON, OnClickListPerson)
	ON_BN_CLICKED(IDC_BUTTON_EDIT, OnButtonEdit)
	ON_BN_CLICKED(IDC_BUTTON_QUERY, OnButtonQuery)
	ON_BN_CLICKED(IDC_BUTTON_PROFE, OnButtonProfe)
	ON_BN_CLICKED(IDC_BUTTON_DOCTER, OnButtonDocter)
	ON_BN_CLICKED(IDC_BUTTON_MASTER, OnButtonMaster)
	ON_BN_CLICKED(IDC_BUTTON_ADD, OnButtonAdd)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLabMISDlg message handlers

BOOL CLabMISDlg::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_listPerson.InsertColumn(0,"ID",LVCFMT_LEFT,40,-1);
	m_listPerson.InsertColumn(1,"姓名",LVCFMT_LEFT,60,-1);
    m_listPerson.InsertColumn(2,"类型",LVCFMT_LEFT,80,-1);
	m_listPerson.SetExtendedStyle(LVS_EX_FULLROWSELECT| LVS_EX_GRIDLINES);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CLabMISDlg::OnButtonFirst() 
{
	if((pRs->IsOpen())&&(pRs->GetRecordCount()!=0))
	{
		pRs->MoveFirst();
		ShowDetail();
	}
}

void CLabMISDlg::OnButtonAll() 
{
	pRs->Open("select * from tbPerson",CADORecordset.openQuery);
    RefreshList(*pRs);
	ShowDetail();
	m_listPerson.SetFocus();
	m_listPerson.SetItemState(0,LVIS_SELECTED, LVIS_SELECTED);
}

void CLabMISDlg::ShowDetail()
{
	if((pRs->IsOpen())&&(pRs->GetRecordCount()>0))
	{
		pRs->GetFieldValue(1,m_editName);
		pRs->GetFieldValue(2,m_editType);
		pRs->GetFieldValue(3,m_editSex);
		pRs->GetFieldValue(4,m_editArea);
		pRs->GetFieldValue(5,m_editPrj);
		UpdateData(FALSE);
	}
}

void CLabMISDlg::OnButtonLast() 
{
	if((pRs->IsOpen())&&(pRs->GetRecordCount()!=0))
	{
		pRs->MoveLast();
		ShowDetail();
	}
}

void CLabMISDlg::OnButtonPre() 
{
	if((pRs->IsOpen())&&(pRs->GetRecordCount()!=0)&&(!pRs->IsBOF()))
	{
		pRs->MovePrevious();
		if (pRs->IsBOF()) 
		{
			MessageBox("首记录!");
			return;
		}	
		ShowDetail();
	}
}

void CLabMISDlg::OnButtonNext() 
{
	if((pRs->IsOpen())&&(pRs->GetRecordCount()!=0)&&(!pRs->IsEOF()))
	{	
		pRs->MoveNext();
		if (pRs->IsEOF()) 
		{
			MessageBox("末记录!");
			return;
		}	
		ShowDetail();
	/*
		long i=pRs->GetAbsolutePosition();
			CString index;
			index.Format("%d",i);
			MessageBox(index);*/
	
	}
}

void CLabMISDlg::OnButtonDelete() 
{
	int sel=m_listPerson.GetSelectionMark();
	if(sel<0)
	{
		MessageBox("没有选择删除的记录!");
		return;
	}
    CString strID=m_listPerson.GetItemText(sel,0);
	CString strSQL="delete from tbPerson where ID="+strID;
	pDB->Execute(strSQL);
	//重新执行记录集的原查询操作更新数据
	pRs->Requery(adConnectUnspecified);
	RefreshList(*pRs);
	ShowDetail();
}

void CLabMISDlg::RefreshList(CADORecordset& recordset) 
{
    m_listPerson.DeleteAllItems();

	if(!recordset.IsOpen())
	   return;
    if(recordset.GetRecordCount()<=0)
		return;
	recordset.MoveFirst();
	CString strID,strName,strType;
	int indexofList=0;
	while(!(recordset.IsEOF()))
	{
		recordset.GetFieldValue("ID",strID);
		recordset.GetFieldValue("Name",strName);
		recordset.GetFieldValue("Type",strType);
		m_listPerson.InsertItem(indexofList,strID);
		m_listPerson.SetItemText(indexofList,1,strName);
		m_listPerson.SetItemText(indexofList,2,strType);
		indexofList+=1;
		recordset.MoveNext();
	}
	recordset.MoveFirst();
}

void CLabMISDlg::OnClickListPerson(NMHDR* pNMHDR, LRESULT* pResult) 
{
	int sel=m_listPerson.GetSelectionMark();
	if(sel<0)
		return;
	else
	{
		CString strID=m_listPerson.GetItemText(sel,0);		
	    if(pRs->Find("ID="+strID,1)||pRs->Find("ID="+strID,-1))
	    			ShowDetail();   		    		
	}
	*pResult = 0;
}

void CLabMISDlg::OnButtonEdit() 
{
	int sel=m_listPerson.GetSelectionMark();
	if(sel<0)
	{
		MessageBox("没有选择要修改的记录!");
		return;
	}
	UpdateData(TRUE);
    pRs->SetFieldValue("Name",m_editName);
	pRs->SetFieldValue("Type",m_editType);
	pRs->SetFieldValue("Sex",m_editSex);
	pRs->SetFieldValue("ReArea",m_editArea);
	pRs->SetFieldValue("WorkPrj",m_editPrj);
	RefreshList(*pRs);

	m_listPerson.SetFocus();
	m_listPerson.SetItemState(sel,LVIS_SELECTED, LVIS_SELECTED);
}

void CLabMISDlg::OnButtonQuery() 
{
	UpdateData(TRUE);
	if(m_editQueryName.IsEmpty())
	{
		MessageBox("没有输入查询姓名!");
		return;
	}
	CString strSQL;
	strSQL.Format("select * from tbPerson where Name='%s'",m_editQueryName);
	pRs->Close();
	pRs->Open(_bstr_t(strSQL),CADORecordset.openQuery);
	RefreshList(*pRs);
	ShowDetail();
	m_listPerson.SetFocus();
	m_listPerson.SetItemState(0,LVIS_SELECTED, LVIS_SELECTED);
}

void CLabMISDlg::OnButtonProfe() 
{
	CString strStoredPro="getProfessor";
	pRs->Close();
	pRs->Open(strStoredPro,CADORecordset.openStoredProc);
	RefreshList(*pRs);
	ShowDetail();
	
}

void CLabMISDlg::OnButtonDocter() 
{
	CString strStoredPro="getDoctor";
	pRs->Close();
	pRs->Open(strStoredPro,CADORecordset.openStoredProc);
	RefreshList(*pRs);
	ShowDetail();
}

void CLabMISDlg::OnButtonMaster() 
{
	CString strStoredPro="getMaster";
	pRs->Close();
	pRs->Open(strStoredPro,CADORecordset.openStoredProc);
	RefreshList(*pRs);
	ShowDetail();
}

void CLabMISDlg::OnButtonAdd() 
{
	UpdateData(TRUE);
	pRs->AddNew();
	pRs->SetFieldValue("Name",m_editName);
	pRs->SetFieldValue("Type",m_editType);
	pRs->SetFieldValue("Sex",m_editSex);
	pRs->SetFieldValue("ReArea",m_editArea);
	pRs->SetFieldValue("WorkPrj",m_editPrj);
	pRs->Update();
	RefreshList(*pRs);
}

⌨️ 快捷键说明

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