📄 fund2dlg.cpp
字号:
// Fund2Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "Fund2.h"
#include "Fund2Dlg.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()
/////////////////////////////////////////////////////////////////////////////
// CFund2Dlg dialog
CFund2Dlg::CFund2Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CFund2Dlg::IDD, pParent)
{
m_StuIndex=0;
//{{AFX_DATA_INIT(CFund2Dlg)
m_address = _T("");
m_birthday = _T("");
m_email = _T("");
m_name = _T("");
m_phone = _T("");
m_photoroute = _T("");
m_hobby = _T("");
m_sex = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CFund2Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CFund2Dlg)
DDX_Control(pDX, IDC_STATICPHOTO, m_Pic);
DDX_Text(pDX, IDC_EDITADDRESS, m_address);
DDX_Text(pDX, IDC_EDITBIRTHDAY, m_birthday);
DDX_Text(pDX, IDC_EDITEMAIL, m_email);
DDX_Text(pDX, IDC_EDITNAME, m_name);
DDX_Text(pDX, IDC_EDITPHONE, m_phone);
DDX_Text(pDX, IDC_EDITPHOTO, m_photoroute);
DDX_Text(pDX, IDC_EDITHOBBY, m_hobby);
DDX_Text(pDX, IDC_EDITSEX, m_sex);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CFund2Dlg, CDialog)
//{{AFX_MSG_MAP(CFund2Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnPrevious)
ON_BN_CLICKED(IDC_BUTTON2, OnNext)
ON_BN_CLICKED(IDC_BUTTON4, OnLoad)
ON_BN_CLICKED(IDC_BUTTON5, OnApply)
ON_BN_CLICKED(IDC_BUTTON3, OnSave)
ON_WM_LBUTTONDBLCLK()
ON_BN_CLICKED(IDC_BUTTON6, OnChangePhoto)
ON_BN_CLICKED(IDC_BUTTON7, OnAdd)
ON_BN_CLICKED(IDC_BUTTON8, OnDel)
ON_BN_CLICKED(IDC_BUTTON9, OnButton9)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFund2Dlg message handlers
BOOL CFund2Dlg::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
return TRUE; // return TRUE unless you set the focus to a control
}
void CFund2Dlg::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 CFund2Dlg::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 CFund2Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CFund2Dlg::OnPrevious()
{
// TODO: Add your control notification handler code here
if(!m_StuIndex) return;
UpdateData();
StuInfo stuinfo;
memset(&stuinfo,0,sizeof(StuInfo));
strcpy(stuinfo.name,m_name);
strcpy(stuinfo.birthday,m_birthday);
strcpy(stuinfo.address,m_address);
strcpy(stuinfo.phone,m_phone);
strcpy(stuinfo.email,m_email);
strcpy(stuinfo.photofile,m_photoroute);
strcpy(stuinfo.sex,m_sex);
strcpy(stuinfo.hobby,m_hobby);
*m_StuIndex=stuinfo;
if(m_StuIndex==m_Records.begin())
return;
m_StuIndex--;
Display();
}
void CFund2Dlg::OnNext()
{
if(!m_StuIndex) return;
UpdateData();
StuInfo stuinfo;
memset(&stuinfo,0,sizeof(StuInfo));
strcpy(stuinfo.name,m_name);
strcpy(stuinfo.birthday,m_birthday);
strcpy(stuinfo.address,m_address);
strcpy(stuinfo.phone,m_phone);
strcpy(stuinfo.email,m_email);
strcpy(stuinfo.photofile,m_photoroute);
strcpy(stuinfo.sex,m_sex);
strcpy(stuinfo.hobby,m_hobby);
*m_StuIndex=stuinfo;
// TODO: Add your control notification handler code here
if(m_StuIndex==m_Records.end()-1)
return;
m_StuIndex++;
Display();
}
void CFund2Dlg::OnLoad()
{
static char BASED_CODE szFilters[]=
"Student info files(*.sif)|*.sif|All Files (*.*)|*.*||";
CFileDialog fileDlg (TRUE, _T("记录文件"), NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
szFilters, this);
if( fileDlg.DoModal ()==IDOK)
{
CString pathName = fileDlg.GetPathName();
CFile file;
if(!file.Open(pathName,CFile::modeRead)) return;
StuInfo stuinfo;
m_Records.clear();
while(file.Read(&stuinfo,sizeof(stuinfo))){
m_Records.push_back(stuinfo);
}
m_StuIndex=m_Records.begin();
Display();
file.Close();
Filename=pathName;
}
else
return;
}
void CFund2Dlg::OnApply()
{
// TODO: Add your control notification handler code here
CFile file;
if(!file.Open(Filename,CFile::modeCreate|CFile::modeWrite)) return;
StuInfo stuinfo;
vector<StuInfo>::iterator it;
for(it=m_Records.begin();it!=m_Records.end();++it)
{
if (it==(--m_StuIndex))
continue;
stuinfo=*it;
file.Write(&stuinfo,sizeof(StuInfo));
}
file.Close();
}
void CFund2Dlg::OnSave()
{
static char BASED_CODE szFilters[]=
"Student info files(*.sif)|*.sif|All Files (*.*)|*.*||";
CFileDialog fileDlg (FALSE, _T("记录文件"), NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
szFilters, this);
if( fileDlg.DoModal ()==IDOK )
{
CString pathName = fileDlg.GetPathName();
CFile file;
if(!file.Open(pathName,CFile::modeCreate|CFile::modeWrite)) return;
StuInfo stuinfo;
vector<StuInfo>::iterator it;
for(it=m_Records.begin();it!=m_Records.end();++it)
{
stuinfo=*it;
file.Write(&stuinfo,sizeof(StuInfo));
}
file.Close();
Filename=pathName;
}
else
return;
}
void CFund2Dlg::OnLButtonDblClk(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CDialog::OnLButtonDblClk(nFlags, point);
}
void CFund2Dlg::OnChangePhoto()
{
static char BASED_CODE szFilters[]=
"Student photo files(*.bmp)|*.bmp|All Files (*.*)|*.*||";
CFileDialog fileDlg (TRUE, _T("照片文件"), NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
szFilters, this);
if( fileDlg.DoModal ()==IDOK )
{
CString pathName = fileDlg.GetPathName();
LoadBmp(pathName);
}
else
return;
}
void CFund2Dlg::OnAdd()
{
//Add a record into the list.
UpdateData();
StuInfo stuinfo;
memset(&stuinfo,0,sizeof(StuInfo));
strcpy(stuinfo.name,m_name);
strcpy(stuinfo.birthday,m_birthday);
strcpy(stuinfo.address,m_address);
strcpy(stuinfo.phone,m_phone);
strcpy(stuinfo.email,m_email);
strcpy(stuinfo.photofile,m_photoroute);
strcpy(stuinfo.sex,m_sex);
strcpy(stuinfo.hobby,m_hobby);
m_Records.push_back(stuinfo);
m_StuIndex=m_Records.end()-1;
}
void CFund2Dlg::Display()
{
StuInfo stuinfo=*m_StuIndex;
m_name=stuinfo.name;
m_birthday=stuinfo.birthday;
m_address=stuinfo.address;
m_phone=stuinfo.phone;
m_email=stuinfo.email;
m_photoroute=stuinfo.photofile;
m_sex=stuinfo.sex;
m_hobby=stuinfo.hobby;
UpdateData(FALSE);
if(!m_photoroute.IsEmpty())
{
LoadBmp(m_photoroute);
}
else
{
m_photoroute="default.bmp";
LoadBmp(m_photoroute);
m_photoroute="";
UpdateData(FALSE);
}
}
void CFund2Dlg::LoadBmp(CString pathname)
{
UpdateData();
HANDLE hBitmap = LoadImage(NULL, pathname, IMAGE_BITMAP,150, 200,
LR_LOADFROMFILE);
m_Pic.SetBitmap((HBITMAP)hBitmap);
m_Pic.UpdateWindow();
m_photoroute=pathname;
UpdateData(FALSE);
}
void CFund2Dlg::OnDel()
{
if(m_Records.begin()==m_Records.end()-1)
{
AfxMessageBox("这是最后的一条记录!");
StuInfo stuinfo1;
memset(&stuinfo1,0,sizeof(StuInfo));
strcpy(stuinfo1.name,"");
strcpy(stuinfo1.birthday,"");
strcpy(stuinfo1.address,"");
strcpy(stuinfo1.phone,"");
strcpy(stuinfo1.email,"");
m_photoroute="";
strcpy(stuinfo1.photofile,m_photoroute);
strcpy(stuinfo1.sex,"");
strcpy(stuinfo1.hobby,"");
*m_StuIndex=stuinfo1;
Display();
return;
}
if(!m_StuIndex) return;
m_Records.erase(m_StuIndex);
if(m_StuIndex!=m_Records.begin())
m_StuIndex--;
else if(m_StuIndex<m_Records.end())
{}
else
m_StuIndex=0;
Display();
}
void CFund2Dlg::OnButton9()
{
// TODO: Add your control notification handler code here
OnOK();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -