📄 adressdlg.cpp
字号:
// AdressDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Adress.h"
#include "AdressDlg.h"
#include "AddDlg.h"
#include "DeleteDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CAdressDlg dialog
CAdressDlg::CAdressDlg(CWnd* pParent /*=NULL*/)
: CDialog(CAdressDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CAdressDlg)
m_editEmail = _T("");
m_editName = _T("");
m_editQueryName = _T("");
m_editQueryTelephone = _T("");
m_editSex = _T("");
m_editTelephone = _T("");
m_editType = _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_data.udl";
pDB=new CADODatabase;
pDB->Open(strConnection);
pRs=new CADORecordset(pDB);
}
void CAdressDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAdressDlg)
DDX_Control(pDX, IDC_LIST_BOOK, m_listBook);
DDX_Text(pDX, IDC_EDIT_EMAIL, m_editEmail);
DDX_Text(pDX, IDC_EDIT_NAME, m_editName);
DDX_Text(pDX, IDC_EDIT_QUERYNAME, m_editQueryName);
DDX_Text(pDX, IDC_EDIT_QUERYTELEPHONE, m_editQueryTelephone);
DDX_Text(pDX, IDC_EDIT_SEX, m_editSex);
DDX_Text(pDX, IDC_EDIT_TELEPHONE, m_editTelephone);
DDX_Text(pDX, IDC_EDIT_TYPE, m_editType);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAdressDlg, CDialog)
//{{AFX_MSG_MAP(CAdressDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_NOTIFY(NM_CLICK, IDC_LIST_BOOK, OnClickListBook)
ON_BN_CLICKED(IDC_BUTTON_QUERY1, OnButtonQuery1)
ON_BN_CLICKED(IDC_BUTTON_ADD, OnButtonAdd)
ON_BN_CLICKED(IDC_BUTTON_QUERY2, OnButtonQuery2)
ON_BN_CLICKED(IDC_BUTTON_EDIT, OnButtonEdit)
ON_BN_CLICKED(IDC_BUTTON_ALL, OnButtonAll)
ON_BN_CLICKED(IDC_BUTTON_DELETE, OnButtonDelete)
ON_BN_CLICKED(IDC_BUTTON_FIRST, OnButtonFirst)
ON_BN_CLICKED(IDC_BUTTON_LAST, OnButtonLast)
ON_BN_CLICKED(IDC_BUTTON_PRE, OnButtonPre)
ON_BN_CLICKED(IDC_BUTTON_NAXT, OnButtonNaxt)
ON_BN_CLICKED(IDC_BUTTON_EXIT, OnButtonExit)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CAdressDlg message handlers
BOOL CAdressDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
CString strConnection="File Name=my_data.udl";
pDB=new CADODatabase;
pDB->Open(strConnection);
pRs=new CADORecordset(pDB);
// 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_listBook.InsertColumn(0,"ID",LVCFMT_LEFT,30,-1);
m_listBook.InsertColumn(1,"姓名",LVCFMT_LEFT,60,-1);
//m_listBook.InsertColumn(2,"类型",LVCFMT_LEFT,40,-1);
m_listBook.InsertColumn(2,"电话号码",LVCFMT_LEFT,90,-1);
m_listBook.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
return TRUE; // return TRUE unless you set the focus to a control
}
void CAdressDlg::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 CAdressDlg::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 CAdressDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CAdressDlg::ShowDetail()
{ //显示当前记录的所有字段的值
if((pRs->IsOpen())&&(pRs->GetRecordCount()>0))
{
pRs->GetFieldValue("id",m_strID);
//MessageBox(m_strID);
pRs->GetFieldValue(1,m_editName);
pRs->GetFieldValue(2,m_editSex);
pRs->GetFieldValue(3,m_editType);
pRs->GetFieldValue(4,m_editTelephone);
pRs->GetFieldValue(5,m_editEmail);
UpdateData(FALSE);
}
}
void CAdressDlg::RefreshList(CADORecordset &recordset)
{ //先删除List控件中的所有数据
m_listBook.DeleteAllItems();
if(!recordset.IsOpen())
return;
if(recordset.GetRecordCount()<=0)
return;
recordset.MoveFirst();
CString strID,strName,strType,strTelephone;
int indexofList=0;
while(!(recordset.IsEOF()))
{
recordset.GetFieldValue("ID",strID);
recordset.GetFieldValue("Name",strName);
// recordset.GetFieldValue("Type",strType);
recordset.GetFieldValue("Telephone",strTelephone);
m_listBook.InsertItem(indexofList,strID);
m_listBook.SetItemText(indexofList,1,strName);
//m_listBook.SetItemText(indexofList,2,strType);
m_listBook.SetItemText(indexofList,2,strTelephone);
indexofList+=1;
recordset.MoveNext();
}
recordset.MoveFirst();
}
void CAdressDlg::OnClickListBook(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
//获取用户选择的行序号
int sel=m_listBook.GetSelectionMark();
//没有选中任何数据
if(sel<0)
return;
else
{
CString strID=m_listBook.GetItemText(sel,0);
//在记录集中搜索List控件中选择的记录
if(pRs->Find("ID="+strID,1)||pRs->Find("ID="+strID,-1))
ShowDetail();
}
*pResult = 0;
}
void CAdressDlg::OnButtonQuery1()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
if(m_editQueryName.IsEmpty())
{
MessageBox("没有输入查询姓名!");
return;
}
CString strSQL;
strSQL.Format("select * from book where Name='%s'",m_editQueryName);
pRs->Close();
pRs->Open(_bstr_t(strSQL),CADORecordset.openQuery);
RefreshList(*pRs);
ShowDetail();
m_listBook.SetFocus();
m_listBook.SetItemState(0,LVIS_SELECTED,LVIS_SELECTED);
}
void CAdressDlg::OnButtonAdd()
{
// TODO: Add your control notification handler code here
CAddDlg Add;
Add.DoModal();
pRs->Open("select * from book",CADORecordset.openQuery);
RefreshList(*pRs);
}
void CAdressDlg::OnButtonQuery2()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
if(m_editQueryTelephone.IsEmpty())
{
MessageBox("没有输入查询电话号码!");
return;
}
CString strSQL;
strSQL.Format("select * from book where Telephone='%s'",m_editQueryTelephone);
pRs->Close();
pRs->Open(_bstr_t(strSQL),CADORecordset.openQuery);
RefreshList(*pRs);
ShowDetail();
m_listBook.SetFocus();
m_listBook.SetItemState(0,LVIS_SELECTED,LVIS_SELECTED);
}
void CAdressDlg::OnButtonEdit()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
CString strSQL;
strSQL.Format("update book set name='%s',sex='%s',type='%s',telephone='%s',email='%s' where id=%s",m_editName,m_editSex,m_editType,m_editTelephone,m_editEmail,m_strID);
pDB->Execute(strSQL);
pRs->Open("select * from Book",CADORecordset.openQuery);
RefreshList(*pRs);
m_listBook.SetFocus();
m_listBook.SetItemState(0,LVIS_SELECTED,LVIS_SELECTED);
}
void CAdressDlg::OnButtonAll()
{
// TODO: Add your control notification handler code here
pRs->Open("select * from Book",CADORecordset.openQuery);
RefreshList(*pRs);
ShowDetail();
m_listBook.SetFocus();
m_listBook.SetItemState(0,LVIS_SELECTED,LVIS_SELECTED);
}
void CAdressDlg::OnButtonDelete()
{
// TODO: Add your control notification handler code here
int sel=m_listBook.GetSelectionMark();
if(sel<0)
{
MessageBox("没有选择删除的记录!");
return;
}
CString strID=m_listBook.GetItemText(sel,0);
//CString strSQL="delete from book where ID="+strID;
CDeleteDlg Del;
Del.setID(strID);
Del.DoModal();
//pDB->Execute(strSQL);
//重新执行记录集的原查询操作更新数据
pRs->Requery(adConnectUnspecified);
RefreshList(*pRs);
ShowDetail();
}
void CAdressDlg::OnButtonFirst()
{
// TODO: Add your control notification handler code here
//判断记录集是否打开,是否有记录
if((pRs->IsOpen())&&(pRs->GetRecordCount()!=0))
{
//记录集指针移动到记录集首
pRs->MoveFirst();
ShowDetail();
}
}
void CAdressDlg::OnButtonLast()
{
// TODO: Add your control notification handler code here
if((pRs->IsOpen())&&(pRs->GetRecordCount()!=0))
{
//记录集指针移动到记录集首
pRs->MoveLast();
ShowDetail();
}
}
void CAdressDlg::OnButtonPre()
{
// TODO: Add your control notification handler code here
//判断记录集是否打开,是否有记录以及判断记录集指针是否已经到记录集首
if((pRs->IsOpen())&&(pRs->GetRecordCount()!=0)&&(!pRs->IsBOF()))
{
//调用记录集对象的方法移动到前一记录
pRs->MovePrevious();
if(pRs->IsBOF())
{
//如果记录集指针已经到首记录,给出提示信息
MessageBox("首记录!");
return;
}
ShowDetail();
}
}
void CAdressDlg::OnButtonNaxt()
{
// TODO: Add your control notification handler code here
//判断记录集是否打开,是否有记录以及判断记录集指针是否已经到记录集末
if((pRs->IsOpen())&&(pRs->GetRecordCount()!=0)&&(!pRs->IsBOF()))
{
//调用记录集对象的方法移动到后一记录
pRs->MoveNext();
if(pRs->IsEOF())
{
//如果记录集指针已经到末记录,给出提示信息
MessageBox("末记录!");
return;
}
ShowDetail();
}
}
void CAdressDlg::OnButtonExit()
{
// TODO: Add your control notification handler code here
exit(1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -