/
main.cpp
413 lines (384 loc) · 9.43 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#ifdef _UNICODE
#pragma comment(linker,"/manifestdependency:\"type='win32' \
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
#endif
#include "stdafx.h"
#include "resource.h"
#include "Course.h"
#include "utils.h"
#include <windows.h>
#include <stdio.h>
#include <commctrl.h>
#pragma comment(lib, "comctl32.lib")
#define PROC_PARAMS HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam
#define PROC_ARGS hwnd, message, wParam, lParam
#define DEF_PROC(method) LRESULT CALLBACK method(PROC_PARAMS)
INT_PTR CALLBACK mainDlgProc(PROC_PARAMS);
INT_PTR CALLBACK courseDlgProc(PROC_PARAMS);
void initMainDlg(HINSTANCE hInstance);
void initTable(HWND hDlg);
void onCourseClick(LPNMITEMACTIVATE pInfo);
bool onCourseUpdate();
void deleteCourse(int courseIndex, int dayIndex);
void clearCourse();
void updateCell(Course* course, int courseIndex, int dayIndex);
void showCourseDlg();
void hideCourseDlg();
void setCourseDlgText(int id, CSTR text);
void readFile();
void saveFile();
void openDir();
CSTR FILE_PATH = L"data.txt";
HWND hCourseDlg; // 课程设置对话框
HWND hCourseList; // 课程列表控件
int curDayIndex;
int curCourseIndex;
#define Course_LENGTH 4
#define DAY_LENGTH 7
Course* courses[Course_LENGTH][DAY_LENGTH];
CSTR const TIME[] = {
L"上午第一大节\n7:50 - 10:10",
L"上午第二大节\n10:30 - 12:00",
L"下午\n14:40 - 17:00",
L"晚上\n19:40 - 22:00"
};
/**
* 程序的入口
* HINSTANCE hInstance 程序执行模块的句柄(内存中的.exe文件)
* HINSTANCE hPrevInstance 在Win32程序中总为NULL
* LPSTR lpCmdLine 一个字符串的命令行参数,不包括程序名
* int nCmdShow 可能传递给 ShowWindow() 的参数
*/
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow)
{
MSG messages;
initMainDlg(hInstance);
readFile();
while (GetMessage (&messages, NULL, 0, 0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}
/*初始化主对话框*/
void initMainDlg(HINSTANCE hInstance)
{
HWND hMainDlg = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_MAIN), GetDesktopWindow(), mainDlgProc);
//设置对话框的图标
SendMessage(hMainDlg, WM_SETICON, ICON_SMALL, (LPARAM)LoadIcon(NULL, IDI_APPLICATION));
/*显示时间*/
Char timeBuf[32]; // 时间字符缓冲区
timeStr(timeBuf);
SetWindowText(GetDlgItem(hMainDlg, IDC_TIME), timeBuf);
//初始化列表控件
initTable(hMainDlg);
//显示主对话框
ShowWindow(hMainDlg, SW_SHOW);
hCourseDlg = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_COURSE), GetDesktopWindow(), courseDlgProc);
hideCourseDlg();
}
/**
* 主对话框过程
*/
INT_PTR CALLBACK mainDlgProc(PROC_PARAMS)
{
switch(message)
{
case WM_CTLCOLORSTATIC:
return (INT_PTR)((HBRUSH)GetStockObject(WHITE_BRUSH));
case WM_COMMAND:
if(HIWORD(wParam) == 0) switch(LOWORD(wParam))
{
case IDM_SAVE:
saveFile();
break;
case IDM_LOAD:
readFile();
break;
case IDM_OPENDIR:
openDir();
break;
case IDM_CLEAR:
clearCourse();
break;
}
break;
case WM_NOTIFY:
if(LOWORD(wParam) == IDC_TABLE && ((LPNMHDR)lParam)->code == NM_DBLCLK)
onCourseClick((LPNMITEMACTIVATE)lParam);
break;
case WM_SYSCOMMAND:
if(wParam == SC_CLOSE)
PostQuitMessage(0); //退出
break;
}
return 0;
}
/**
* 课程设置对话框过程
*/
INT_PTR CALLBACK courseDlgProc(PROC_PARAMS)
{
switch(message)
{
case WM_INITDIALOG:
// 设置对话框的图标
SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)LoadIcon(NULL, IDI_APPLICATION));
break;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case ID_OK:
if(onCourseUpdate())
hideCourseDlg();
break;
case ID_CANCEL:
hideCourseDlg();
break;
case ID_DELETE:
deleteCourse(curCourseIndex, curDayIndex);
hideCourseDlg();
break;
}
break;
case WM_SYSCOMMAND:
if(wParam == SC_CLOSE)
hideCourseDlg(); //隐藏
break;
}
return 0;
}
/**
* 初始化列表控件
*/
void initTable(HWND hDlg)
{
HWND table = GetDlgItem(hDlg, IDC_TABLE);
hCourseList = table;
ListView_SetBkColor(table, 0xECF0F1);
ListView_SetExtendedListViewStyle(table, LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
HIMAGELIST hImageList = ImageList_Create(1, 100, ILC_COLOR16 | ILC_MASK, 1, 1);
ListView_SetImageList(table, hImageList, LVSIL_STATE);
// 绘制表头
LV_COLUMN column = {LVCF_TEXT | LVCF_WIDTH | LVCF_FMT, LVCFMT_CENTER, 160};
column.pszText = L"时间";
ListView_InsertColumn(table, 1, &column);
CSTR headers = L"一二三四五六天";
Char headerBuffer[] = L"星期*";
for (int i = 0; i < DAY_LENGTH;)
{
headerBuffer[2] = headers[i];
column.pszText = headerBuffer;
ListView_InsertColumn(table, ++i, &column);
}
// 插入行头
LVITEM item = {LVIF_IMAGE | LVIF_TEXT, 0, 0};
while (item.iItem < Course_LENGTH)
{
ListView_InsertItem(table, &item);
item.pszText = (STR)TIME[item.iItem];
ListView_SetItem(table, &item);
++item.iItem;
}
}
/**
* 课程单元格双击事件
*/
void onCourseClick(LPNMITEMACTIVATE pInfo)
{
if (pInfo->iSubItem == 0)
return;
curDayIndex = pInfo->iSubItem - 1;
curCourseIndex = pInfo->iItem;
Char buffer[3];
Course* &course = courses[curCourseIndex][curDayIndex];
if (course)
{
// 同步对话框中的内容
setCourseDlgText(IDC_NAME, course->title);
setCourseDlgText(IDC_TEACHER, course->teacher);
setCourseDlgText(IDC_CLASSROOM, course->classRoom);
wsprintf(buffer, L"%d", course->startWeek);
setCourseDlgText(IDC_WEEK_START, buffer);
wsprintf(buffer, L"%d", course->endWeek);
setCourseDlgText(IDC_WEEK_END, buffer);
}
else
{
setCourseDlgText(IDC_NAME, nullptr);
setCourseDlgText(IDC_TEACHER, nullptr);
setCourseDlgText(IDC_CLASSROOM, nullptr);
setCourseDlgText(IDC_WEEK_START, nullptr);
setCourseDlgText(IDC_WEEK_END, nullptr);
}
showCourseDlg();
}
/**
* 完成修改课程
*/
bool onCourseUpdate()
{
Char buffer[16];
CSTR error = nullptr;
Course* &course = courses[curCourseIndex][curDayIndex];
auto getText = [&buffer](int id) -> CSTR {
GetWindowText(GetDlgItem(hCourseDlg, id), buffer, 16);
trim(buffer);
return buffer;
};
if(!course)
course = new Course;
course->setTitle(getText(IDC_TITLE));
if(!*course->title)
error = L"请输入标题";
if(!error)
{
course->setTeacher(getText(IDC_TEACHER));
if(!*course->teacher)
error = L"请输入教师";
}
if(!error)
{
course->setClassRoome(getText(IDC_CLASSROOM));
if(!*course->classRoom)
error = L"请输入教室";
}
if(!error)
{
course->startWeek = _wtoi(getText(IDC_WEEK_START));
if(course->startWeek == 0)
error = L"起始周不能为空或0";
}
if(!error)
{
course->endWeek = _wtoi(getText(IDC_WEEK_END));
if(course->endWeek == 0)
error = L"结束周不能为空或0";
}
if (error)
{
SetWindowText(GetDlgItem(hCourseDlg, IDC_ERROR), error);
return false;
}
else
{
updateCell(course, curCourseIndex, curDayIndex);
return true;
}
}
/*删除课程*/
void deleteCourse(int courseIndex, int dayIndex)
{
Course* &course = courses[courseIndex][dayIndex];
if (course)
{
delete course;
course = nullptr;
updateCell(course, courseIndex, dayIndex);
}
}
/*清空课程*/
void clearCourse()
{
for (int courseIndex = 0; courseIndex < Course_LENGTH; ++courseIndex)
{
for (int dayIndex = 0; dayIndex < DAY_LENGTH; ++dayIndex)
{
deleteCourse(courseIndex, dayIndex);
}
}
}
/**
* 更新单元格显示
*/
void updateCell(Course* course, int courseIndex, int dayIndex)
{
LVITEM item = {LVIF_TEXT, courseIndex, dayIndex + 1};
if (course)
{
Char buffer[32];
course->toString(buffer);
item.pszText = buffer;
}
ListView_SetItem(hCourseList, &item);
}
/*显示课程设置对话框*/
void showCourseDlg()
{
setCourseDlgText(IDC_ERROR, L"请用/表示空");
ShowWindow(hCourseDlg, SW_SHOW);
SetActiveWindow(hCourseDlg);
}
/*隐藏课程设置对话框*/
void hideCourseDlg()
{
ShowWindow(hCourseDlg, SW_HIDE);
}
/*设置课程对话框文本*/
void setCourseDlgText(int id, CSTR text)
{
SetWindowText(GetDlgItem(hCourseDlg, id), text);
}
/**
* 从文件读取数据
*/
void readFile()
{
FILE *fp = _wfopen(FILE_PATH, L"r,ccs=UNICODE");
if(!fp)
return;
for (int courseIndex = 0; courseIndex < Course_LENGTH; ++courseIndex)
{
if(feof(fp))
break;
Char buffer[16];
for (int dayIndex = 0; dayIndex < DAY_LENGTH; ++dayIndex)
{
Course* &course = courses[courseIndex][dayIndex];
fwscanf(fp, L"%s", buffer);
if (*buffer != Course::Null)
{
if(!course)
course = new Course;
course->setTitle(buffer);
fwscanf(fp, L"%s%s%d%d", course->classRoom, course->teacher,
&course->startWeek, &course->endWeek);
updateCell(course, courseIndex, dayIndex);
}
}
}
fclose(fp);
}
/**
* 数据写入到文件
*/
void saveFile()
{
FILE *fp = _wfopen(FILE_PATH, L"w,ccs=UNICODE");
if(!fp)
return;
for (int courseIndex = 0; courseIndex < Course_LENGTH; ++courseIndex)
{
for (Course* &course: courses[courseIndex])
{
if(course)
{
fwprintf(fp, L"%s\t%s\t%s\t%d\t%d\n",
course->title, course->classRoom, course->teacher,
course->startWeek, course->endWeek);
} else
fwprintf(fp, L"%c\n", Course::Null);
}
fwprintf(fp, L"\n");
}
fclose(fp);
}
/*打开文件夹*/
void openDir()
{
Char buf[128];
GetCurrentDirectory(sizeof(buf), buf);
ShellExecute(NULL, L"open", L"explorer", buf, NULL, SW_SHOWNORMAL);
}