📄 addr_bookdlg.cpp
字号:
// addr_bookDlg.cpp : implementation file
//
#include "stdafx.h"
#include "addr_book.h"
#include "addr_bookDlg.h"
#include "APeople.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()
/////////////////////////////////////////////////////////////////////////////
// CAddr_bookDlg dialog
CAddr_bookDlg::CAddr_bookDlg(CWnd* pParent /*=NULL*/)
: CDialog(CAddr_bookDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CAddr_bookDlg)
// 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 CAddr_bookDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAddr_bookDlg)
DDX_Control(pDX, IDC_LIST2, m_list);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAddr_bookDlg, CDialog)
//{{AFX_MSG_MAP(CAddr_bookDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON6, OnButtonClose)
ON_BN_CLICKED(IDC_BUTTON8, OnButtonOpen)
ON_BN_CLICKED(IDC_BUTTON9, OnButtonSave)
ON_BN_CLICKED(IDC_BUTTON7, OnButtonSaveAs)
ON_BN_CLICKED(IDC_BUTTON1, OnButtonAdd)
ON_BN_CLICKED(IDC_BUTTON2, OnButtonModify)
ON_BN_CLICKED(IDC_BUTTON3, OnButtonDelete)
ON_BN_CLICKED(IDC_BUTTON4, OnButtonSort)
ON_BN_CLICKED(IDC_BUTTON5, OnButtonSearch)
ON_NOTIFY(NM_CLICK, IDC_LIST2, OnClickListMain)
ON_BN_CLICKED(IDC_BUTTON10, OnButtonCloseDB)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CAddr_bookDlg message handlers
BOOL CAddr_bookDlg::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
// 初始化ImageList
m_imagelist.Create(16,16,ILC_MASK,0,4);
HICON hIcon=AfxGetApp()->LoadIcon(IDI_ICON1);
m_imagelist.Add(hIcon);
// 初始化 ListCtrl
m_list.SetImageList(&m_imagelist,LVSIL_SMALL);
m_list.InsertColumn(0,"姓名");
m_list.InsertColumn(1,"学号");
m_list.InsertColumn(2,"手机");
m_list.InsertColumn(3,"成绩");
m_list.InsertColumn(4,"Email");
m_list.InsertColumn(5,"地址");
m_list.SetColumnWidth(0,80);
m_list.SetColumnWidth(1,80);
m_list.SetColumnWidth(2,80);
m_list.SetColumnWidth(3,80);
m_list.SetColumnWidth(4,160);
m_list.SetColumnWidth(5,160);
m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
CComboBox* pComboBox=(CComboBox*)GetDlgItem(IDC_COMBO1);
pComboBox->SetCurSel(0);
pComboBox=(CComboBox*)GetDlgItem(IDC_COMBO2);
pComboBox->SetCurSel(0);
pComboBox=(CComboBox*)GetDlgItem(IDC_COMBO3);
pComboBox->SetCurSel(0);
ShowData();
return TRUE; // return TRUE unless you set the focus to a control
}
void CAddr_bookDlg::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 CAddr_bookDlg::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 CAddr_bookDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CAddr_bookDlg::OnOK()
{
// TODO: Add extra validation here
//CDialog::OnOK();
}
void CAddr_bookDlg::OnCancel()
{
// TODO: Add extra cleanup here
//CDialog::OnCancel();
}
void CAddr_bookDlg::OnButtonClose()
{
// TODO: Add your control notification handler code here
CDialog::OnCancel();
}
void CAddr_bookDlg::OnButtonOpen()
{
// TODO: Add your control notification handler code here
CFileDialog dlg(TRUE);
if(dlg.DoModal()==IDOK)
{
m_addrbook.OpenFile(dlg.GetFileName());
ShowData();
}
}
void CAddr_bookDlg::OnButtonSave()
{
// TODO: Add your control notification handler code here
if(!m_addrbook.Save())
{
AfxMessageBox("保存失败");
return;
}
AfxMessageBox("保存完成");
}
void CAddr_bookDlg::OnButtonSaveAs()
{
// TODO: Add your control notification handler code here
CFileDialog dlg(FALSE);
if(dlg.DoModal()==IDOK)
{
m_addrbook.SaveToFile(dlg.GetFileName());
}
}
void CAddr_bookDlg::OnButtonAdd()
{
// TODO: Add your control notification handler code here
APeople aPeople;
CString s,s1;
CEdit* pEdit=(CEdit*)GetDlgItem(IDC_EDIT_NAME);
pEdit->GetWindowText(s);
if(s.GetLength()>sizeof(aPeople.name))
{
s1.Format("姓名最多不超过%d个字符!",aPeople.name);
AfxMessageBox(s1);
return;
}
if(s.GetLength()<2)
{
AfxMessageBox("姓名最小不小于2个字符!");
return;
}
memcpy(aPeople.name,s,s.GetLength());
pEdit=(CEdit*)GetDlgItem(IDC_EDIT_TEL);
pEdit->GetWindowText(s);
if(s.GetLength()>sizeof(aPeople.tel))
{
s1.Format("学号最多不超过%d个字符!",aPeople.tel);
AfxMessageBox(s1);
return;
}
if(s.GetLength()<2)
{
AfxMessageBox("学号最小不小于2个字符!");
return;
}
memcpy(aPeople.tel,s,s.GetLength());
pEdit=(CEdit*)GetDlgItem(IDC_EDIT_MOBILE);
pEdit->GetWindowText(s);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -