Windows c++界面开发初学(十三)win32
今天终于做完了最后一个部分–会议管理部分,包括了会议查询、删除和创建。
win32的学习也以此告一段落。下次再见可能就投入qt的怀抱了。
UI
选择显示会议的选项,可以选择显示所有赞助会议、所有参与会议以及所有赞助和参与会议。
选择查询条件的选项,可以选择以会议名称查询会议、会议时间查询会议的方式查询会议记录,需要点击查询按钮。
点击添加会议按钮跳转到创建会议对话框。
Sponsor默认为当前用户,参与用户的每个用户之间用英文逗号分隔开。
时间控件是工具箱中的date time picker,要设置其format属性为时间。
代码
//会议显示listView
HWND hListView = nullptr;//设置显示的会议
void setMeeting(int mode, std::string startTime = "", std::string endTime ="", std::string title = "") {ListView_DeleteAllItems(hListView);std::list meetings;string username = agendaService.getCurrentUser()->getName();//显示所有参与会议if (mode == 1) {meetings = agendaService.listAllParticipateMeetings(username);//显示所有赞助会议} else if (mode == 2) {meetings = agendaService.listAllSponsorMeetings(username);//显示所有赞助和参与会议} else if (mode == 0) {meetings = agendaService.listAllMeetings(username);//显示在某时间范围内的会议记录} else if (mode == 3) {meetings = agendaService.meetingQuery(username, startTime, endTime);//显示给定标题的会议记录} else {meetings = agendaService.meetingQuery(username, title);}int arrCount = 0;LVITEM vitem;vitem.mask = LVFIF_TEXT;for (auto iter : meetings) {vitem.pszText = StringToWchar(iter.getTitle());vitem.iItem = arrCount;//设置子项vitem.iSubItem = 0;ListView_InsertItem(hListView, &vitem);vitem.iSubItem = 1;vitem.pszText = StringToWchar(Date::dateToString(iter.getStartDate()));ListView_SetItem(hListView, &vitem);vitem.iSubItem = 2;vitem.pszText = StringToWchar(Date::dateToString(iter.getEndDate()));ListView_SetItem(hListView, &vitem);vitem.iSubItem = 3;string participators = "";int i = 0;for (i = 0; i < iter.getParticipator().size() - 1; i++) {participators += iter.getParticipator()[i] + ',';}if (iter.getParticipator().size() > 0)participators += iter.getParticipator()[i];vitem.pszText = StringToWchar(participators);ListView_SetItem(hListView, &vitem);vitem.iSubItem = 4;vitem.pszText = StringToWchar(iter.getSponsor());ListView_SetItem(hListView, &vitem);arrCount++;}
}//会议管理对话框
int queryCondition = -1;
INT_PTR CALLBACK DialogProcMeetingManagement(HWND hdlg, UINT msg, WPARAM wParam, LPARAM lParam) {HWND hComboBox1 = GetDlgItem(hdlg, IDC_COMBO1);HWND hComboBox2 = GetDlgItem(hdlg, IDC_COMBO2);switch (msg){case WM_INITDIALOG:{//设置ComboBox内容ComboBox_AddString(hComboBox1, L"显示所有参与会议");ComboBox_AddString(hComboBox1, L"显示所有赞助会议");ComboBox_AddString(hComboBox1, L"显示所有参与和赞助会议");ComboBox_AddString(hComboBox2, L"根据会议名查询");ComboBox_AddString(hComboBox2, L"根据会议时间查询");hListView = GetDlgItem(hdlg, IDC_LIST1);//设置listView的列信息包含哪些LVCOLUMN col;col.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;//列标题col.pszText = L"会议主题";//列宽col.cx = 80;// 子项索引,第一列无子项col.iSubItem = 0;ListView_InsertColumn(hListView, 0, &col);//第二列col.pszText = L"会议开始时间";col.cx = 100;col.iSubItem = 2;ListView_InsertColumn(hListView, 1, &col);//第三列col.pszText = L"会议结束时间";col.cx = 100;col.iSubItem = 3;ListView_InsertColumn(hListView, 2, &col);//第四列col.pszText = L"会议参与者";col.cx = 120;col.iSubItem = 4;ListView_InsertColumn(hListView, 3, &col);//第五列col.pszText = L"会议赞助者";col.cx = 80;col.iSubItem = 5;ListView_InsertColumn(hListView, 4, &col);setMeeting(0);}break;case WM_COMMAND:{switch (LOWORD(wParam)) {//用户管理按钮case IDC_BUTTON3:{EndDialog(hdlg, IDC_BUTTON3);DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG4), NULL, DialogProcUserManagement);}break;//退出登录按钮case IDC_BUTTON4:{if (MessageBox(hdlg, L"确定退出登录?", L"提示", MB_OKCANCEL) == IDOK) {EndDialog(hdlg, IDC_BUTTON4);DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG2), NULL, DialogProcSignIn);}}break;//选择显示会议的方式case IDC_COMBO1:{if (HIWORD(wParam) == CBN_SELCHANGE) {int index = SendMessage((HWND)lParam, CB_GETCURSEL, 0, 0);setMeeting(index); }}break;//选择查询条件case IDC_COMBO2:{if (HIWORD(wParam) == CBN_SELCHANGE) {queryCondition = SendMessage((HWND)lParam, CB_GETCURSEL, 0, 0);}}break;//查询按钮case IDC_BUTTON5:{if (queryCondition < 0) {MessageBox(hdlg, L"请选择查询方式!", L"提示", MB_OK);break;}if (queryCondition == 1) {SYSTEMTIME startTime = { 0 };HWND datepicker1 = GetDlgItem(hdlg, IDC_DATETIMEPICKER1);SendMessage(datepicker1, DTM_GETSYSTEMTIME, 0, (LPARAM)&startTime);SYSTEMTIME endTime = { 0 };HWND datepicker2 = GetDlgItem(hdlg, IDC_DATETIMEPICKER2);SendMessage(datepicker2, DTM_GETSYSTEMTIME, 0, (LPARAM)&endTime);char start[20], end[20];sprintf_s(start, "%04u-%02u-%02u/00:00", startTime.wYear, startTime.wMonth, startTime.wDay);sprintf_s(end, "%04u-%02u-%02u/23:59", endTime.wYear, endTime.wMonth, endTime.wDay);setMeeting(3, start, end);} else {WCHAR title[40];GetDlgItemText(hdlg, IDC_EDIT3, title, sizeof title);if (lstrlen(title) == 0) {MessageBox(hdlg, L"请输入会议标题!", L"提示", MB_OK);break;}setMeeting(4, "", "", WcharToString(title));}}break;//添加会议按钮case IDC_BUTTON6:{EndDialog(hdlg, IDC_BUTTON6);DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG6), NULL, DialogCreateMeeting);}break;//删除选中会议按钮case IDC_BUTTON7:{int selected = ListView_GetSelectionMark(hListView);int count = ListView_GetItemCount(hListView);WCHAR title[20];memset(title, 0, sizeof title);ListView_GetItemText(hListView, selected, 0, title, 20);if (agendaService.deleteMeeting(agendaService.getCurrentUser()->getName(), WcharToString(title))) {SendMessage(hListView, LVM_DELETEITEM, selected, 0);ListView_SetSelectionMark(hListView, -1);} else if (lstrlen(title)) {MessageBox(hdlg, L"当前用户非该会议的赞助者!", L"提示", MB_OK);}}break;//删除所有赞助会议按钮case IDC_BUTTON8:{if (agendaService.deleteAllMeetings(agendaService.getCurrentUser()->getName())) {setMeeting(0);SendMessage(hComboBox1, CB_SETCURSEL, 0, 0);}}break;}}break;case WM_SYSCOMMAND:{if (wParam == SC_CLOSE) {if (MessageBox(hdlg, L"确定退出程序?", L"提示", MB_OKCANCEL) == IDOK) {PostQuitMessage(0);}}}break;}return (INT_PTR)FALSE;
}//创建会议对话框
INT_PTR CALLBACK DialogCreateMeeting(HWND hdlg, UINT msg, WPARAM wParam, LPARAM lParam) {string sponsor = agendaService.getCurrentUser()->getName();switch (msg){case WM_INITDIALOG: {SetDlgItemText(hdlg, IDC_EDIT14, StringToWchar(sponsor));}break;case WM_COMMAND:{switch (LOWORD(wParam)) {//创建会议按钮case IDC_BUTTON18:{if (MessageBox(hdlg, L"确定创建会议?", L"提示", MB_OKCANCEL) == IDOK) {//设置参与者wchar_t participator[80];GetDlgItemText(hdlg, IDC_EDIT15, participator, sizeof participator);string participatorsName = WcharToString(participator);std::vector<string> participators;int count[6] = { 0 };int index = 0;for (int i = 0; i < participatorsName.length(); i++) {if (participatorsName[i] == ',') {count[index++] = i;}}count[index] = participatorsName.length();if (index == 0) {participators.push_back(participatorsName);} else {participators.push_back(participatorsName.substr(0, count[0]));for (int i = 0; i < index; i++) {participators.push_back(participatorsName.substr(count[i]+1, count[i+1]-count[i]-1));}}if (participators.empty()) {MessageBox(hdlg, L"参与会议人员为空或格式有误!", L"提示", MB_OK);break;}//设置会议时间SYSTEMTIME startDate = { 0 };HWND datepicker3 = GetDlgItem(hdlg, IDC_DATETIMEPICKER3);SendMessage(datepicker3, DTM_GETSYSTEMTIME, 0, (LPARAM)&startDate);SYSTEMTIME endDate = { 0 };HWND datepicker4 = GetDlgItem(hdlg, IDC_DATETIMEPICKER4);SendMessage(datepicker4, DTM_GETSYSTEMTIME, 0, (LPARAM)&endDate);SYSTEMTIME startTime = { 0 };HWND datepicker5 = GetDlgItem(hdlg, IDC_DATETIMEPICKER5);SendMessage(datepicker5, DTM_GETSYSTEMTIME, 0, (LPARAM)&startTime);SYSTEMTIME endTime = { 0 };HWND datepicker6 = GetDlgItem(hdlg, IDC_DATETIMEPICKER6);SendMessage(datepicker6, DTM_GETSYSTEMTIME, 0, (LPARAM)&endTime);char start[20], end[20];sprintf_s(start, "%04u-%02u-%02u/%02u:%02u", startDate.wYear, startDate.wMonth, startDate.wDay, startTime.wHour, startTime.wMinute);sprintf_s(end, "%04u-%02u-%02u/%02u:%02u", endDate.wYear, endDate.wMonth, endDate.wDay, endTime.wHour, endTime.wMinute);//设置会议标题wchar_t title[20];memset(title, 0, sizeof title);GetDlgItemText(hdlg, IDC_EDIT16, title, sizeof title);if (lstrlen(title) == 0) {MessageBox(hdlg, L"会议主题未输入!", L"提示", MB_OK);break;}if (agendaService.createMeeting(sponsor, WcharToString(title), start, end, participators)) {EndDialog(hdlg, SC_CLOSE);DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG1), NULL, DialogProcMeetingManagement);} else {MessageBox(hdlg, L"创建会议失败!请重试", L"提示", MB_OK);}}}break;}break;}case WM_SYSCOMMAND:{if (wParam == SC_CLOSE) {EndDialog(hdlg, SC_CLOSE);DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG1), NULL, DialogProcMeetingManagement);}}break;}return (INT_PTR)FALSE;
}
运行效果
初始登录后界面,当前用户为Emy
选择显示所有参与会议
选择显示所有赞助会议,由于这两个会议的sponsor都是Tom,因此没有赞助会议
选择根据会议标题查询会议记录,输入会议名称为“会议1”
选择根据会议时间查询会议记录,将会议开始时间设为8/28,结束时间设为8/29,查询得到会议3
选择会议1并点击删除选中会议,弹出错误提示“当前用户不是该会议的sponsor”
点击添加会议按钮,跳转到创建会议对话框,输入需要的信息后点击创建会议
可以看到正确显示了新创建的会议4
再创建一个会议2
选中会议2点击删除选中会议后顺利删除会议2,再点击删除所有赞助会议将会议4也删除完毕
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
