📄 telbookdlg.cpp
字号:
// TelBookDlg.cpp : implementation file
//
#include "stdafx.h"
#include "TelBook.h"
#include "TelBookDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
int CALLBACK ListCompareFunc(LPARAM lParam1, LPARAM lParam2,LPARAM lParamSort);
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()
/////////////////////////////////////////////////////////////////////////////
// CTelBookDlg dialog
CTelBookDlg::CTelBookDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTelBookDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTelBookDlg)
// 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 CTelBookDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTelBookDlg)
DDX_Control(pDX, IDC_LIST_CORDE, m_RecordList);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTelBookDlg, CDialog)
//{{AFX_MSG_MAP(CTelBookDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_ADD, OnAdd)
ON_BN_CLICKED(IDC_EXIT, OnExit)
ON_BN_CLICKED(IDC_ABOUT, OnAbout)
ON_BN_CLICKED(IDC_BUTTON_HELP, OnButtonHelp)
ON_BN_CLICKED(IDC_DELETE, OnDelete)
ON_BN_CLICKED(IDC_FIND, OnFind)
ON_BN_CLICKED(IDC_MODIFY, OnModify)
ON_WM_DESTROY()
ON_NOTIFY(NM_DBLCLK, IDC_LIST_CORDE, OnDblclkListCorde)
ON_WM_CTLCOLOR()
ON_NOTIFY(LVN_COLUMNCLICK, IDC_LIST_CORDE, OnColumnclickListCorde)
ON_NOTIFY(NM_RCLICK, IDC_LIST_CORDE, OnRclickListCorde)
ON_COMMAND(ID_COPY, OnCopy)
ON_COMMAND(ID_DELET, OnDelete)
ON_COMMAND(ID_MODIFY, OnModify)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTelBookDlg message handlers
BOOL CTelBookDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
/*注册热键*/
//RegisterHotKey(this->m_hWnd,ID_COPY,MOD_CONTROL,'C');
// 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_RecordList.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);//设置表的风格
/*设置表的字段*/
m_RecordList.InsertColumn(0,"姓名",LVCFMT_LEFT,80);
m_RecordList.InsertColumn(1,"电话号码",LVCFMT_LEFT,85);
m_RecordList.InsertColumn(2,"手机号码",LVCFMT_LEFT,85);
m_RecordList.InsertColumn(3,"QQ",LVCFMT_LEFT,70);
m_RecordList.InsertColumn(4,"E-mail",LVCFMT_LEFT,140);
m_RecordList.InsertColumn(5,"小灵通",LVCFMT_LEFT,85);
m_RecordList.InsertColumn(6,"网页",LVCFMT_LEFT,85);
m_RecordList.InsertColumn(7,"邮编",LVCFMT_LEFT,50);
m_RecordList.InsertColumn(8,"地址",LVCFMT_LEFT,200);
m_ImageList.Create(IDB_MAN, 16,0, RGB(0,0,0)); //设置列表控件的图表显示
m_RecordList.SetImageList(&m_ImageList,LVSIL_SMALL);
//db.Open(NULL,false,false,"Driver={Microsoft Access Driver (*.mdb)};DBQ=.\\record.mdb;");
db.OpenEx("Driver={Microsoft Access Driver (*.mdb)};DBQ=.\\record.mdb;",CDatabase::useCursorLib);
if(!db.IsOpen()){
MessageBox("Sorry!Can't open the DateBase!","错误",MB_OK |MB_DEFBUTTON2 |MB_ICONERROR);
exit(1);
}
m_pSet.m_pDatabase=&db;
m_pSet.Open(CRecordset::snapshot,"table1");//打开一个表
if(!m_pSet.IsOpen()){
MessageBox("Sorry!不能打开记录表","错误",MB_OK | MB_ICONERROR);
exit(1);
}
if(m_pSet.GetRecordCount()<=0)//判断数据库里是否有记录
return true;
while(!m_pSet.IsEOF( ))
m_pSet.MoveNext();//移动到最后一个记录,下的GetRecordCount()才能得到正确的记录数目,不能用MoveLast()
long int n=m_pSet.GetRecordCount();
CString str;
m_pSet.MoveFirst();
if(m_pSet.IsBOF())
m_pSet.MoveNext();
for(int i=0;i<n&&!m_pSet.IsEOF();i++){
AddItemToList(i);
m_pSet.MoveNext();
}
return TRUE; // return TRUE unless you set the focus to a control
}
void CTelBookDlg::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 CTelBookDlg::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 CTelBookDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CTelBookDlg::OnAdd()
{
// TODO: Add your control notification handler code here
CMyDlg AddDlg(NULL,"添加");
if( AddDlg.DoModal()== IDOK ){
m_pSet.m_strFilter="姓名=";
m_pSet.m_strFilter+="'"+AddDlg.m_name+"'";
m_pSet.Requery();
if(m_pSet.m_Name==AddDlg.m_name){
MessageBox("电话本中己有"+m_pSet.m_Name+"的记录!","提示",MB_ICONERROR);
return;
}
m_pSet.AddNew();
/*传递输入的数据*/
m_pSet.m_Name=AddDlg.m_name;
m_pSet.m_PhoNo=AddDlg.m_pho;
m_pSet.m_MobNo=AddDlg.m_mobi;
m_pSet.m_QQ=AddDlg.m_QQ;
m_pSet.m_E_mail=AddDlg.m_email;
m_pSet.m_Call_No=AddDlg.m_callNo;
m_pSet.m_Call=AddDlg.m_call;
m_pSet.m_Post=AddDlg.m_post;
m_pSet.m_Address=AddDlg.m_address;
if( m_pSet.Update() ){/*向数据库写入数据*/
m_pSet.Requery();
int i=m_RecordList.GetItemCount();
//HINSTANCE resource_handle = AfxGetResourceHandle();
//HICON hIcon = ::LoadIcon ( resource_handle, MAKEINTRESOURCE(IDI_BOOK) );
//m_ImageList.Add(hIcon); //设置列表控件的图表显示
m_RecordList.InsertItem(i,"",1);
m_RecordList.SetItemText(i,0,AddDlg.m_name);
m_RecordList.SetItemText(i,1,AddDlg.m_pho);
m_RecordList.SetItemText(i,2,AddDlg.m_mobi);
m_RecordList.SetItemText(i,3,AddDlg.m_QQ);
m_RecordList.SetItemText(i,4,AddDlg.m_email);
m_RecordList.SetItemText(i,5,AddDlg.m_callNo);
m_RecordList.SetItemText(i,6,AddDlg.m_call);
m_RecordList.SetItemText(i,7,AddDlg.m_post);
m_RecordList.SetItemText(i,8,AddDlg.m_address);
m_RecordList.SetItemData(i,i);
POINT point;
m_RecordList.GetItemPosition(i-1,&point);//获得的位置是第index条记录的下边位置,所以要减2
m_RecordList.Scroll(CSize(0,point.y));//滚动到指定位置
m_RecordList.SetFocus();
m_RecordList.SetItemState(i, LVNI_SELECTED,LVNI_SELECTED);//设置index项被选中
/*
//将指针移到最后一条记录,即刚添加的记录
m_pSet.MoveLast();
if(m_pSet.IsEOF())
m_pSet.MovePrev();
*/
/*
m_pSet.m_strFilter="姓名=";
m_pSet.m_strFilter+="'"+AddDlg.m_name+"'";
if( m_pSet.Requery() )
*/// AddItemToList(i);//从数据库中读出并显示刚添加的记录
}
}
}
void CTelBookDlg::AddItemToList(int nIndex)
{
CString str;
str.Format("%d",nIndex);
m_RecordList.InsertItem(nIndex,str);
m_pSet.GetFieldValue("姓名",str);
m_RecordList.SetItemText(nIndex,0,str);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -