📄 myaddrview.cpp
字号:
// MyAddrView.cpp : implementation of the CMyAddrView class
//
#include "stdafx.h"
#include "MyAddr.h"
#include "MyAddrDoc.h"
#include "MyAddrView.h"
#include "hints.h"
#include "DbChangeDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMyAddrView
IMPLEMENT_DYNCREATE(CMyAddrView, CListView)
BEGIN_MESSAGE_MAP(CMyAddrView, CListView)
//{{AFX_MSG_MAP(CMyAddrView)
ON_COMMAND(ID_OPENDB, OnOpendb)
ON_COMMAND(ID_POP_DELETE, OnPopDelete)
ON_COMMAND(ID_POP_NEWRECORD, OnPopNewrecord)
ON_COMMAND(ID_POP_UPDATE, OnPopUpdate)
ON_COMMAND(ID_REFRASH, OnRefrash)
ON_WM_LBUTTONDBLCLK()
ON_WM_RBUTTONDOWN()
ON_UPDATE_COMMAND_UI(ID_OPENDB, OnUpdateOpendb)
ON_UPDATE_COMMAND_UI(ID_REFRASH, OnUpdateRefrash)
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CMyAddrView construction/destruction
UINT CMyAddrView::m_ColumnLabelID [NUM_COLUMNS] =
{
IDS_TABLE_HEAD_NAME,
IDS_TABLE_HEAD_WPHONE,
IDS_TABLE_HEAD_MPHONE,
IDS_TABLE_HEAD_HPHONE,
IDS_TABLE_HEAD_NOTE,
};
int CMyAddrView::m_ColumnFormat [NUM_COLUMNS] =
{
LVCFMT_LEFT, LVCFMT_LEFT, LVCFMT_LEFT, LVCFMT_LEFT
};
CMyAddrView::CMyAddrView()
{
// TODO: add construction code here
}
CMyAddrView::~CMyAddrView()
{
}
BOOL CMyAddrView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
cs.style |= LVS_REPORT |
LVS_SINGLESEL |
LVS_SHOWSELALWAYS |
LVS_ICON ;
return CListView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMyAddrView drawing
void CMyAddrView::OnDraw(CDC* pDC)
{
CMyAddrDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
void CMyAddrView::OnInitialUpdate()
{
CListView::OnInitialUpdate();
CListCtrl &lc = GetListCtrl();
int Column;
LV_COLUMN LVColumn; // column info of one column in list control
CMyAddrDoc *pDoc = (CMyAddrDoc*) GetDocument();
DWORD dwStyle = ListView_GetExtendedListViewStyle(GetListCtrl());
//Add the full row select and grid line style to the existing extended styles
dwStyle |= LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES |
LVS_EX_HEADERDRAGDROP | LVS_EX_TRACKSELECT;
ListView_SetExtendedListViewStyle (GetListCtrl(),dwStyle);
lc.DeleteAllItems ();// regular cleanup
m_pImageList = new CImageList();
m_pImageList->Create(IDB_BITMAP1, 16, 1, RGB(0,0,0));
GetListCtrl().SetImageList(m_pImageList, LVSIL_SMALL);
//initialize the columns (insert columns)
m_ColumnWidth [COL_NAME ] = 100;
m_ColumnWidth [COL_WPHONE ] = 100;
m_ColumnWidth [COL_MPHONE ] = 100;
m_ColumnWidth [COL_HPHONE ] = 100;
m_ColumnWidth [COL_NOTE ] = 200;
// set header and format for all visible columns
LVColumn.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
{
for (Column = 0; (Column < NUM_COLUMNS); Column++)
{
CString HeaderString;
// fill header data
HeaderString.LoadString (m_ColumnLabelID[Column]);
LVColumn.iSubItem = Column;
LVColumn.pszText = (LPTSTR) (LPCTSTR) HeaderString;
LVColumn.cx = m_ColumnWidth [Column];
LVColumn.fmt = m_ColumnFormat [Column];
lc.InsertColumn (Column, &LVColumn);
}
}
CString strDbPath;
//SetDatabasePath("");
strDbPath = GetDatabasePath();
if(!strDbPath.IsEmpty())
{
pDoc->OnNewDocument(); pDoc->OnOpenDocument(strDbPath);
}
else
{
AfxMessageBox(_T("Please specify new database location"), MB_OK | MB_ICONEXCLAMATION);
CFileDialog dlg(TRUE,".mdb", "Address Book.mdb",
OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
0,
this);
if (dlg.DoModal() == IDOK)
{
pDoc->OnNewDocument();
SetDatabasePath(dlg.GetPathName());
pDoc->OnOpenDocument(dlg.GetPathName());
if (pDoc->m_bFileOpen)
pDoc->UpdateAllViews(NULL, HINT_DB_OPENED, NULL);
}
}
SetFocus ();
lc.SetItem (0, 0, LVIF_STATE, NULL, 0, LVIS_FOCUSED , LVIS_FOCUSED, 0);
// TODO: You may populate your ListView with items by directly accessing
// its list control through a call to GetListCtrl().
}
/////////////////////////////////////////////////////////////////////////////
// CMyAddrView printing
BOOL CMyAddrView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMyAddrView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMyAddrView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CMyAddrView diagnostics
#ifdef _DEBUG
void CMyAddrView::AssertValid() const
{
CListView::AssertValid();
}
void CMyAddrView::Dump(CDumpContext& dc) const
{
CListView::Dump(dc);
}
CMyAddrDoc* CMyAddrView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyAddrDoc)));
return (CMyAddrDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMyAddrView message handlers
void CMyAddrView::OnOpendb()
{
// TODO: Add your command handler code here
CMyAddrDoc *pDoc = (CMyAddrDoc*) GetDocument();
SetDatabasePath("");
CFileDialog dlg(TRUE,".mdb", "Address Book.mdb",
OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
0,
this);
if (dlg.DoModal() == IDOK)
{
pDoc->OnNewDocument();
SetDatabasePath(dlg.GetPathName());
pDoc->OnOpenDocument(dlg.GetPathName());
if (pDoc->m_bFileOpen)
pDoc->UpdateAllViews(NULL, HINT_DB_OPENED, NULL);
}
}
void CMyAddrView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
{
// TODO: Add your specialized code here and/or call the base class
// TODO: Add your specialized code here and/or call the base class
CListCtrl &lc= GetListCtrl();
CMyAddrDoc *pDoc = (CMyAddrDoc*)GetDocument();
CMyRecordSet *pRS;
pRS = new CMyRecordSet(pDoc->m_pDB);
int i=0;
switch(lHint)
{
case HINT_DB_OPENED:
lc.DeleteAllItems();
pRS->Open(dbOpenDynaset,pRS->GetDefaultSQL(),0);
pRS->MoveFirst();
while (!pRS->IsEOF())
{
int ItemIndex = lc.InsertItem(LVIF_TEXT | LVIF_PARAM, i, pRS->m_Name, 0, 0, 0, 0);
int SubitemIndex = 1;
lc.SetItem (ItemIndex, SubitemIndex++, LVIF_TEXT, pRS->m_Address, 0, 0, 0, 0);
lc.SetItem (ItemIndex, SubitemIndex++, LVIF_TEXT, pRS->m_WorkPhone, 0, 0, 0, 0);
lc.SetItem (ItemIndex, SubitemIndex++, LVIF_TEXT, pRS->m_HomePhone, 0, 0, 0, 0);
lc.SetItem (ItemIndex, SubitemIndex++, LVIF_TEXT, pRS->m_Notes, 0, 0, 0, 0);
pRS->MoveNext();
}
break;
case HINT_NEW_RECORD:
CDbChangeDlg dlg;
CMyRecordSet *pRS1;
pRS1 = new CMyRecordSet(pDoc->m_pDB);
ASSERT(pRS1 != NULL);
try
{
pRS1->Open();
long numRec = pRS1->GetRecordCount();
}
catch (CDaoException* e)
{
delete pRS1;
pRS1 = NULL;
TCHAR szCause[255];
CString strFormatted = _T("The data file could not be opened because of this error: \n");
e->GetErrorMessage(szCause, 255);
strFormatted += szCause;
AfxMessageBox(strFormatted, MB_OK | MB_ICONEXCLAMATION);
e->Delete();
return ;
}
if(dlg.DoModal()==IDOK)
{
pRS1->AddNew();
if(dlg.m_Name.IsEmpty())
{
AfxMessageBox("请嵌入姓名");return ;
}
pRS1->m_Address = dlg.m_Address;
pRS1->m_EmailAddress = dlg.m_Email;
pRS1->m_HomePhone = dlg.m_hPhone;
pRS1->m_MobilePhone = dlg.m_mPhone;
pRS1->m_Notes = dlg.m_note;
pRS1->m_WorkPhone = dlg.m_oPhone;
pRS1->m_Name = dlg.m_Name;
pRS1->Update();
// refresh
pRS1->Edit();
pRS1->Update();
pDoc->UpdateAllViews(NULL,HINT_DB_OPENED, NULL);
}
delete pRS1;
break;
}
delete pRS;
}
void CMyAddrView::OnPopDelete()
{
CListCtrl& lc = GetListCtrl ();
CMyAddrDoc *pDoc = (CMyAddrDoc*) GetDocument();
// CMyRecordSet *pRS;
int nItem = lc.GetNextItem (-1, LVNI_SELECTED);
if(nItem==-1) { AfxMessageBox("清选择要删除的纪录");return;}
/* pRS = new CDaoRecordsetAccess(pDoc->m_pDB);
try
{
pRS->Open();
}
catch(CDaoException* e)
{
AfxMessageBox(e->m_pErrorInfo->m_strDescription);
e->Delete();
}
*/
// Verify deleting record with current Name
CString str = lc.GetItemText(nItem, 0);
if ( (AfxMessageBox("Delete the record with the following Name: " +
str, MB_YESNO | MB_ICONQUESTION | MB_DEFBUTTON2)) == IDYES ) {
CString sql(_T("Name = "));
sql += "'";
sql += str;
sql += "'";
pDoc->m_pSet->m_strFilter =sql;
// requery the database
pDoc->m_pSet->Requery();
// delete the returned record
pDoc->m_pSet->Delete();
lc.DeleteItem(nItem);
}
// delete pRS;
}
void CMyAddrView::OnPopNewrecord()
{
// TODO: Add your command handler code here
CMyAddrDoc *pDoc = GetDocument ();
if(pDoc->m_bFileOpen)
pDoc->UpdateAllViews(NULL,HINT_NEW_RECORD,NULL);
else
AfxMessageBox("请确定数据库的位置\n“文件”|“打开数据库”\n来定位数据库的位置");
}
void CMyAddrView::OnPopUpdate()
{
// TODO: Add your command handler code here
// TODO: Add your command handler code here
CMyAddrDoc *pDoc = GetDocument ();
CListCtrl& lc = GetListCtrl ();
CMyRecordSet *pRS ;
CDbChangeDlg dlg;
pRS = new CMyRecordSet(pDoc->m_pDB);
try
{
pRS->Open();
}
catch(CDaoException* e)
{
AfxMessageBox(e->m_pErrorInfo->m_strDescription);
e->Delete();
}
if(pRS)
{
int index = lc.GetNextItem(-1,LVNI_SELECTED);
if(index==-1) AfxMessageBox("清选择更新纪录");
CString str = lc.GetItemText(index,0);
CString sql(_T("[Name] = "));
sql += "'";
sql += str;
sql += "'";
pRS->m_strFilter =sql;
pRS->Requery();
//pRS->Edit();
dlg.m_Address = pRS->m_Address;
dlg.m_Email = pRS->m_EmailAddress;
dlg.m_hPhone = pRS->m_HomePhone;
dlg.m_mPhone = pRS->m_MobilePhone;
dlg.m_Name = pRS->m_Name;
dlg.m_note = pRS->m_Notes;
dlg.m_oPhone = pRS->m_WorkPhone;
if(dlg.DoModal()==IDOK)
{
pRS->Edit();
pRS->m_Address = dlg.m_Address;
pRS->m_EmailAddress = dlg.m_Email;
pRS->m_HomePhone = dlg.m_hPhone;
pRS->m_MobilePhone = dlg.m_mPhone;
pRS->m_Notes = dlg.m_note;
pRS->m_WorkPhone = dlg.m_oPhone;
pRS->m_Name = dlg.m_Name;
pRS->Update();
lc.DeleteItem(index);
int ItemIndex = lc.InsertItem(LVIF_TEXT | LVIF_PARAM, index, pRS->m_Name, 0, 0, 0, 0);
int SubitemIndex = 1;
lc.SetItem (ItemIndex, SubitemIndex++, LVIF_TEXT, pRS->m_Address, 0, 0, 0, 0);
lc.SetItem (ItemIndex, SubitemIndex++, LVIF_TEXT, pRS->m_WorkPhone, 0, 0, 0, 0);
lc.SetItem (ItemIndex, SubitemIndex++, LVIF_TEXT, pRS->m_HomePhone, 0, 0, 0, 0);
lc.SetItem (ItemIndex, SubitemIndex++, LVIF_TEXT, pRS->m_Notes, 0, 0, 0, 0);
//SetFocus();
lc.SetItemState (index, LVIS_FOCUSED | LVIS_SELECTED, LVIS_FOCUSED | LVIS_SELECTED);
}
}
delete pRS;
}
void CMyAddrView::OnRefrash()
{
// TODO: Add your command handler code here
CMyAddrDoc *pDoc = GetDocument ();
pDoc->UpdateAllViews(NULL,HINT_DB_OPENED, NULL);
}
void CMyAddrView::OnLButtonDblClk(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CListCtrl& lc = GetListCtrl();
int nItem = lc.GetNextItem (-1, LVNI_SELECTED);
if (nItem != -1) OnPopUpdate();
CListView::OnLButtonDblClk(nFlags, point);
}
void CMyAddrView::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CMenu PopMenu;
CMenu *pMenu;
CPoint pt;
pt= point ;
PopMenu.LoadMenu(IDR_POP_MENU);
ClientToScreen (&pt);
// show the menu (returns, when menu is closed again!)
pMenu = PopMenu.GetSubMenu (0);
pMenu->TrackPopupMenu (TPM_LEFTALIGN | TPM_RIGHTBUTTON,
pt.x, pt.y, this);
//CListView::OnRButtonDown(nFlags, pt);
}
void CMyAddrView::OnUpdateOpendb(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
CMyAddrDoc *pDoc = (CMyAddrDoc*) GetDocument();
pCmdUI->Enable(!pDoc->m_bFileOpen);
}
void CMyAddrView::OnUpdateRefrash(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
CMyAddrDoc *pDoc = GetDocument ();
pCmdUI->Enable(pDoc->m_bFileOpen);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -