📄 addressbookdbview.cpp
字号:
// AddressBookDBView.cpp : implementation of the CAddressBookDBView class
//
#include "stdafx.h"
#include "AddressBookDB.h"
#include "AddressBookDBDoc.h"
#include "AddressBookDBView.h"
#include "ContactDialog.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAddressBookDBView
IMPLEMENT_DYNCREATE(CAddressBookDBView, CListView)
BEGIN_MESSAGE_MAP(CAddressBookDBView, CListView)
//{{AFX_MSG_MAP(CAddressBookDBView)
ON_COMMAND(ID_MENUITEM_INSERT, OnMenuitemInsert)
ON_COMMAND(ID_MENUITEM_DELETE, OnMenuitemDelete)
ON_COMMAND(ID_MENUITEM_UPDATE, OnMenuitemUpdate)
ON_COMMAND(ID_MENUITEM_SELECT, OnMenuitemSelect)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CListView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CListView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CListView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CAddressBookDBView construction/destruction
CAddressBookDBView::CAddressBookDBView()
{
// TODO: add construction code here
}
CAddressBookDBView::~CAddressBookDBView()
{
}
BOOL CAddressBookDBView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CListView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CAddressBookDBView drawing
void CAddressBookDBView::OnDraw(CDC* pDC)
{
CAddressBookDBDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
void CAddressBookDBView::OnInitialUpdate()
{
CListView::OnInitialUpdate();
// TODO: You may populate your ListView with items by directly accessing
// its list control through a call to GetListCtrl().
DWORD dwStyle=GetWindowLong(GetListCtrl().GetSafeHwnd(),GWL_STYLE);
dwStyle&=~LVS_TYPEMASK;
dwStyle|=LVS_REPORT;
SetWindowLong(GetListCtrl().GetSafeHwnd(),GWL_STYLE,dwStyle);
SetRedraw();
GetListCtrl().InsertColumn(0,"Name",LVCFMT_LEFT,100);
GetListCtrl().InsertColumn(1,"Gender",LVCFMT_CENTER,200);
GetListCtrl().InsertColumn(2,"Phonenumber",LVCFMT_CENTER,200);
CAddressBookDBDoc* pDoc = GetDocument();
pDoc->UpdateAllViews(NULL);
}
/////////////////////////////////////////////////////////////////////////////
// CAddressBookDBView printing
BOOL CAddressBookDBView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CAddressBookDBView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CAddressBookDBView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CAddressBookDBView diagnostics
#ifdef _DEBUG
void CAddressBookDBView::AssertValid() const
{
CListView::AssertValid();
}
void CAddressBookDBView::Dump(CDumpContext& dc) const
{
CListView::Dump(dc);
}
CAddressBookDBDoc* CAddressBookDBView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CAddressBookDBDoc)));
return (CAddressBookDBDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CAddressBookDBView message handlers
void CAddressBookDBView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
{
// TODO: Add your specialized code here and/or call the base class
CAddressBookDBDoc* pDoc = GetDocument();
CListCtrl& listCtrl=GetListCtrl();
listCtrl.DeleteAllItems();
pDoc->Select();
for(int i=0;i<pDoc->contacts.GetSize();i++){
CContact contact=pDoc->contacts.GetAt(i);
listCtrl.InsertItem(i,contact.m_name);
if(contact.m_gender){
listCtrl.SetItemText(i,1,"女");
}
else{
listCtrl.SetItemText(i,1,"男");
}
listCtrl.SetItemText(i,2,contact.m_phonenumber);
}
}
void CAddressBookDBView::OnMenuitemInsert()
{
// TODO: Add your command handler code here
CAddressBookDBDoc* pDoc = GetDocument();
CContactDialog dlg;
if(dlg.DoModal()==IDOK){
CContact contact;
contact.m_name=dlg.m_name;
contact.m_gender=dlg.m_gender;
contact.m_phonenumber=dlg.m_phonenumber;
pDoc->Insert(contact);
pDoc->UpdateAllViews(NULL);
}
}
void CAddressBookDBView::OnMenuitemDelete()
{
// TODO: Add your command handler code here
CAddressBookDBDoc* pDoc = GetDocument();
CListCtrl& listCtrl=GetListCtrl();
POSITION p=listCtrl.GetFirstSelectedItemPosition();
int index=listCtrl.GetNextSelectedItem(p);
CContact contact=pDoc->contacts.GetAt(index);
pDoc->Remove(contact);
pDoc->UpdateAllViews(NULL);
}
void CAddressBookDBView::OnMenuitemUpdate()
{
// TODO: Add your command handler code here
CAddressBookDBDoc* pDoc = GetDocument();
CListCtrl& listCtrl=GetListCtrl();
POSITION p=listCtrl.GetFirstSelectedItemPosition();
int index=listCtrl.GetNextSelectedItem(p);
CContact contact=pDoc->contacts.GetAt(index);
CContactDialog dlg;
dlg.m_name=contact.m_name;
dlg.m_gender=contact.m_gender;
dlg.m_phonenumber=contact.m_phonenumber;
if(dlg.DoModal()==IDOK){
contact.m_name=dlg.m_name;
contact.m_gender=dlg.m_gender;
contact.m_phonenumber=dlg.m_phonenumber;
pDoc->Update(contact);
pDoc->UpdateAllViews(NULL);
}
}
void CAddressBookDBView::OnMenuitemSelect()
{
// TODO: Add your command handler code here
CAddressBookDBDoc* pDoc = GetDocument();
CContactDialog dlg;
if(dlg.DoModal()==IDOK){
if(dlg.m_name==""){
pDoc->m_sql="select ID,NAME,GENDER,PHONENUMBER from CONTACT";
}
else{
pDoc->m_sql="select ID,NAME,GENDER,PHONENUMBER from CONTACT where NAME like '"+dlg.m_name+"%'";
}
}
pDoc->UpdateAllViews(NULL);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -