// WorkerDlg.cpp: 实现文件
//
#include "stdafx.h"
#include "Worker.h"
#include "WorkerDlg.h"
#include "afxdialogex.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CWorkerDlg 对话框
CWorkerDlg::CWorkerDlg(CWnd* pParent /*=NULL*/)
: CDialog(IDD_WORKER_DIALOG, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CWorkerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CWorkerDlg, CDialog)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_ADD, &CWorkerDlg::OnBnClickedAdd)
ON_BN_CLICKED(IDC_DEL, &CWorkerDlg::OnBnClickedDel)
ON_BN_CLICKED(IDC_MOD, &CWorkerDlg::OnBnClickedMod)
ON_BN_CLICKED(IDC_Save, &CWorkerDlg::OnBnClickedSave)
ON_BN_CLICKED(IDCANCEL, &CWorkerDlg::OnBnClickedCancel)
// ON_NOTIFY(LVN_COLUMNCLICK, IDC_LIST, &CWorkerDlg::OnLvnColumnclickList)
ON_NOTIFY(LVN_COLUMNCLICK, IDC_LIST, &CWorkerDlg::OnLvnColumnclickList)
//ON_NOTIFY(HDN_ITEMCLICK, 0, &CWorkerDlg::OnHdnItemclickList)
ON_NOTIFY(NM_CLICK, IDC_LIST, &CWorkerDlg::OnNMClickList)
//ON_EN_UPDATE(IDC_Numb, &CWorkerDlg::OnEnUpdateNumb)
END_MESSAGE_MAP()
// CWorkerDlg 消息处理程序
BOOL CWorkerDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
paintList();
loadItems();
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。 对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void CWorkerDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使图标在工作区矩形中居中
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;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CWorkerDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
//--------------------------------------------
//初始化List Control
VOID CWorkerDlg::paintList()
{
CListCtrl *pList = (CListCtrl*)GetDlgItem(IDC_LIST);
pList->InsertColumn(0, _T("工号"), LVCFMT_LEFT, 80);//LVCFMT_LEFT:左对齐 或者用0 ,1,2
pList->InsertColumn(1, _T("姓名"), LVCFMT_LEFT, 100);
pList->InsertColumn(2, _T("工资"), LVCFMT_LEFT, 140);
pList->InsertColumn(3, _T("入职日期"), LVCFMT_LEFT, 180);
pList->SetBkColor(RGB(205, 226, 252));
pList->SetTextColor(RGB(0, 0, 0));
//pList->SetExtendedStyle(LVS_EX_CHECKBOXES);//在列表前面显示复选框‘与选中一行一起就不显示了.
pList->SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);//选中一行|加网格线
}
//加载文件数据
BOOL CWorkerDlg::loadItems()
{
CFile file;
if (!file.Open(_T("./Worker.dat"), CFile::modeRead))//是否打开成功
{
return FALSE;
}
CListCtrl * pList = (CListCtrl*)GetDlgItem(IDC_LIST);
pList->DeleteAllItems();
sWorker worker;
CString str;
int i = 0;
while (file.Read(&worker, sizeof(worker)) == sizeof(worker))
{
str.Format(_T("%d"), worker.nNumb);
pList->InsertItem(i, str);
pList->SetItemText(i, 1, worker.sName);
str.Format(_T("%0.2f"), worker.fGongzi);
pList->SetItemText(i, 2, str);
str = worker.tDate.Format(_T("%Y年%m月%d日"));
pList->SetItemText(i, 3, str);
}
file.Close();
cSort(pList, 0, pList->GetItemCount());
bSave = FALSE;
return TRUE;
}
// 检查工号是否存在 /返回TRUE-不可插入(已存在)
BOOL CWorkerDlg::CheckNumber(CString szNumber)
{
CListCtrl * pList = (CListCtrl*)GetDlgItem(IDC_LIST);
CString str;
GetDlgItemText(IDC_Numb, str);
int nCount = pList->GetItemCount();
for (int i = 0; i < nCount; i++)
{
if (pList->GetItemText(i,0)==szNumber)
{
AfxMessageBox(_T("该工号已存在,请重新输入"));
SetDlgItemText(IDC_Numb, _T(""));//清空输入框
SetDlgItemText(IDC_Name, _T(""));
SetDlgItemText(IDC_Gongzi, _T(""));
return TRUE;
}
}
ret�