当前位置:   article > 正文

通讯录-Win32GUI制作_通讯录gui

通讯录gui

使用Win32GUI制作,是对通讯录--C++的新提升。

CodeBlocks 出现undefined reference to `GetOpenFileNameA@'问题时解决方案:需要为编译器添加lib库:(添加libcomdlg32.a文件)

代码:

  1. #if defined(UNICODE) && !defined(_UNICODE)
  2. #define _UNICODE
  3. #elif defined(_UNICODE) && !defined(UNICODE)
  4. #define UNICODE
  5. #endif
  6. #include <tchar.h>
  7. #include <windows.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #define FILE_MENU_CREATE 1
  12. #define FILE_MENU_UPDATE 2
  13. #define FILE_MENU_READ 3
  14. #define FILE_MENU_DELETE 4
  15. #define FILE_MENU_IMPORT 5
  16. #define FILE_MENU_EXPORT 6
  17. #define FILE_MENU_EXIT 7
  18. #define CREATE 1
  19. #define UPDATE 2
  20. #define READ 3
  21. #define DEL 4
  22. /* Declare Windows procedure */
  23. LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
  24. void InitWindow(HWND);
  25. void InitDialog(HWND);
  26. void AddMenus(HWND);
  27. void registerDialogClass(HINSTANCE);
  28. void displayDialogCreate(HWND);
  29. void displayDialogCreateAddControls(HWND);
  30. void displayDialogRead(HWND);
  31. void displayDialogReadAddControls(HWND);
  32. void displayDialogUpdate(HWND);
  33. void displayDialogUpdateAddControls(HWND);
  34. void displayDialogDel(HWND);
  35. void displayDialogDelAddControls(HWND);
  36. struct Person
  37. {
  38. char num[30];
  39. char name[30];
  40. char gender[30];
  41. char age[30];
  42. char major[30];
  43. char phone[30];
  44. char address[50];
  45. };
  46. const int item=100;
  47. Person PersonBook[item];
  48. int numPerson;
  49. HMENU hMenu;
  50. HWND hNumber,hName,hAge,hGender,hClass,hPhone,hAddress,hMainWindow,hOut;
  51. HWND hwnd;
  52. /* Make the class name into a global variable */
  53. TCHAR szClassName[ ] = _T("CodeBlocksWindowsApp");
  54. int WINAPI WinMain (HINSTANCE hThisInstance,HINSTANCE hPrevInstance,LPSTR lpszArgument,int nCmdShow)
  55. {
  56. /* This is the handle for our window */
  57. MSG messages; /* Here messages to the application are saved */
  58. WNDCLASSEX wincl; /* Data structure for the windowclass */
  59. /* The Window structure */
  60. wincl.hInstance = hThisInstance;
  61. wincl.lpszClassName = szClassName;
  62. wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
  63. wincl.style = CS_DBLCLKS; /* Catch double-clicks */
  64. wincl.cbSize = sizeof (WNDCLASSEX);
  65. /* Use default icon and mouse-pointer */
  66. wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  67. wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
  68. wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
  69. wincl.lpszMenuName = NULL; /* No menu */
  70. wincl.cbClsExtra = 0; /* No extra bytes after the window class */
  71. wincl.cbWndExtra = 0; /* structure or the window instance */
  72. /* Use Windows's default colour as the background of the window */
  73. wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
  74. /* Register the window class, and if it fails quit the program */
  75. if (!RegisterClassEx (&wincl))
  76. {
  77. MessageBox ( NULL, TEXT ("This program requires Windows NT!"),szClassName, MB_ICONERROR) ;
  78. return 0;
  79. }
  80. registerDialogClass(hThisInstance);
  81. /* The class is registered, let's create the program*/
  82. hwnd = CreateWindowEx (
  83. 0, /* Extended possibilites for variation */
  84. szClassName, /* Classname */
  85. _T("通讯录"), /* Title Text */
  86. WS_OVERLAPPEDWINDOW, /* default window */
  87. CW_USEDEFAULT, /* Windows decides the position */
  88. CW_USEDEFAULT, /* where the window ends up on the screen */
  89. 544, /* The programs width */
  90. 375, /* and height in pixels */
  91. HWND_DESKTOP, /* The window is a child-window to desktop */
  92. NULL, /* No menu */
  93. hThisInstance, /* Program Instance handler */
  94. NULL /* No Window Creation data */
  95. );
  96. /* Make the window visible on the screen */
  97. ShowWindow (hwnd, nCmdShow);
  98. /* Run the message loop. It will run until GetMessage() returns 0 */
  99. while (GetMessage (&messages, NULL, 0, 0))
  100. {
  101. /* Translate virtual-key messages into character messages */
  102. TranslateMessage(&messages);
  103. /* Send message to WindowProcedure */
  104. DispatchMessage(&messages);
  105. }
  106. /* The program return-value is 0 - The value that PostQuitMessage() gave */
  107. return messages.wParam;
  108. }
  109. void open_file(HWND);
  110. void save_file(HWND);
  111. void total()
  112. {
  113. numPerson=0;
  114. for(int i=0;i<100;i++)
  115. {
  116. if(strcmp(PersonBook[i].name,"")==0)
  117. break;
  118. numPerson=i+1;
  119. }
  120. }
  121. /* This function is called by the Windows function DispatchMessage() */
  122. LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  123. {
  124. int val;
  125. switch (message) /* handle the messages */
  126. {
  127. case WM_COMMAND:
  128. switch(wParam)
  129. {
  130. case FILE_MENU_EXIT:
  131. val=MessageBoxW(hwnd,L"Are you sure?",L"Wait!",MB_YESNO|MB_ICONEXCLAMATION);
  132. if(val==IDYES)
  133. {
  134. DestroyWindow(hwnd);
  135. }
  136. else if(val==IDNO)
  137. {
  138. }
  139. break;
  140. case FILE_MENU_CREATE:
  141. displayDialogCreate(hwnd);
  142. //MessageBeep(MB_ICONINFORMATION);
  143. break;
  144. case FILE_MENU_READ:
  145. displayDialogRead(hwnd);
  146. break;
  147. case FILE_MENU_UPDATE:
  148. displayDialogUpdate(hwnd);
  149. break;
  150. case FILE_MENU_DELETE:
  151. displayDialogDel(hwnd);
  152. break;
  153. case FILE_MENU_IMPORT:
  154. open_file(hwnd);
  155. total();
  156. InitWindow(hwnd);
  157. break;
  158. case FILE_MENU_EXPORT:
  159. save_file(hwnd);
  160. break;
  161. }
  162. break;
  163. case WM_CREATE:
  164. InitDialog(hwnd);
  165. open_file(hwnd);
  166. total();
  167. InitWindow(hwnd);
  168. AddMenus(hwnd);
  169. break;
  170. case WM_DESTROY:
  171. PostQuitMessage (0); /* send a WM_QUIT to the message queue */
  172. break;
  173. default: /* for messages that we don't deal with */
  174. return DefWindowProc (hwnd, message, wParam, lParam);
  175. }
  176. return 0;
  177. }
  178. /* Initialization of Window Menu*/
  179. void AddMenus(HWND hwnd)
  180. {
  181. hMenu=CreateMenu();
  182. HMENU hFileMenu=CreateMenu();
  183. AppendMenu(hFileMenu,MF_STRING,FILE_MENU_CREATE,"添加");
  184. AppendMenu(hFileMenu,MF_STRING,FILE_MENU_UPDATE,"修改");
  185. AppendMenu(hFileMenu,MF_STRING,FILE_MENU_READ,"查询");
  186. AppendMenu(hFileMenu,MF_STRING,FILE_MENU_DELETE,"删除");
  187. AppendMenu(hFileMenu,MF_SEPARATOR,(UINT_PTR)NULL,NULL);
  188. AppendMenu(hFileMenu,MF_STRING,FILE_MENU_IMPORT,"导入");
  189. AppendMenu(hFileMenu,MF_STRING,FILE_MENU_EXPORT,"导出");
  190. AppendMenu(hFileMenu,MF_SEPARATOR,(UINT_PTR)NULL,NULL);
  191. AppendMenu(hFileMenu,MF_STRING,FILE_MENU_EXIT,"退出");
  192. AppendMenu(hMenu,MF_POPUP,(UINT_PTR)hFileMenu,"菜单");
  193. AppendMenu(hMenu,MF_POPUP,(UINT_PTR)NULL,"设置");
  194. AppendMenu(hMenu,MF_POPUP,(UINT_PTR)NULL,"帮助");
  195. SetMenu(hwnd,hMenu);
  196. }
  197. /* Initialization of Window Message*/
  198. void InitWindow(HWND hwnd)
  199. {
  200. int x,y,padding;
  201. int cxScreen, cyScreen ;
  202. cxScreen = GetSystemMetrics (SM_CXSCREEN) ;
  203. cyScreen = GetSystemMetrics (SM_CYSCREEN) ;
  204. x=10;
  205. y=50;
  206. padding=2;
  207. int cell_width=(cxScreen-50)/12;
  208. int cell_height=20;
  209. CreateWindow("Static","通讯录 v2.0.1",WS_VISIBLE|WS_CHILD|ES_CENTER,0,10,cxScreen,30,hwnd,NULL,NULL,NULL);
  210. CreateWindow("Static","序号",WS_VISIBLE|WS_CHILD|ES_CENTER,x+(cell_width+padding)*0,y,cell_width,cell_height,hwnd,NULL,NULL,NULL);
  211. CreateWindow("Static","学号",WS_VISIBLE|WS_CHILD|ES_CENTER,x+(cell_width+padding)*1,y,cell_width,cell_height,hwnd,NULL,NULL,NULL);
  212. CreateWindow("Static","姓名",WS_VISIBLE|WS_CHILD|ES_CENTER,x+(cell_width+padding)*2,y,cell_width,cell_height,hwnd,NULL,NULL,NULL);
  213. CreateWindow("Static","性别",WS_VISIBLE|WS_CHILD|ES_CENTER,x+(cell_width+padding)*3,y,cell_width,cell_height,hwnd,NULL,NULL,NULL);
  214. CreateWindow("Static","年龄",WS_VISIBLE|WS_CHILD|ES_CENTER,x+(cell_width+padding)*4,y,cell_width,cell_height,hwnd,NULL,NULL,NULL);
  215. CreateWindow("Static","班级",WS_VISIBLE|WS_CHILD|ES_CENTER,x+(cell_width+padding)*5,y,cell_width,cell_height,hwnd,NULL,NULL,NULL);
  216. CreateWindow("Static","电话",WS_VISIBLE|WS_CHILD|ES_CENTER,x+(cell_width+padding)*6,y,cell_width,cell_height,hwnd,NULL,NULL,NULL);
  217. CreateWindow("Static","地址",WS_VISIBLE|WS_CHILD|ES_CENTER,x+(cell_width+padding)*7,y,cell_width*4,cell_height,hwnd,NULL,NULL,NULL);
  218. //CreateWindow("Static","操作",WS_VISIBLE|WS_CHILD|ES_CENTER,x+(cell_width+padding)*11,y,cell_width,cell_height,hwnd,NULL,NULL,NULL);
  219. int num=(cyScreen*0.716666)/22;
  220. char str[10],str1[10];
  221. for(int i=1;i<=num;i++)
  222. {
  223. itoa(i,str,10);
  224. CreateWindow("Static",str,WS_VISIBLE|WS_CHILD|ES_LEFT, x+(cell_width+padding)*0,y+(cell_height+padding)*i,cell_width,cell_height,hwnd,NULL,NULL,NULL);
  225. CreateWindow("Static",(LPSTR)PersonBook[i-1].num,WS_VISIBLE|WS_CHILD|ES_LEFT, x+(cell_width+padding)*1,y+(cell_height+padding)*i,cell_width,cell_height,hwnd,NULL,NULL,NULL);
  226. CreateWindow("Static",(LPSTR)PersonBook[i-1].name,WS_VISIBLE|WS_CHILD|ES_LEFT, x+(cell_width+padding)*2,y+(cell_height+padding)*i,cell_width,cell_height,hwnd,NULL,NULL,NULL);
  227. CreateWindow("Static",(LPSTR)PersonBook[i-1].gender,WS_VISIBLE|WS_CHILD|ES_LEFT, x+(cell_width+padding)*3,y+(cell_height+padding)*i,cell_width,cell_height,hwnd,NULL,NULL,NULL);
  228. CreateWindow("Static",(LPSTR)PersonBook[i-1].age,WS_VISIBLE|WS_CHILD|ES_LEFT, x+(cell_width+padding)*4,y+(cell_height+padding)*i,cell_width,cell_height,hwnd,NULL,NULL,NULL);
  229. CreateWindow("Static",(LPSTR)PersonBook[i-1].major,WS_VISIBLE|WS_CHILD|ES_LEFT, x+(cell_width+padding)*5,y+(cell_height+padding)*i,cell_width,cell_height,hwnd,NULL,NULL,NULL);
  230. CreateWindow("Static",(LPSTR)PersonBook[i-1].phone,WS_VISIBLE|WS_CHILD|ES_LEFT, x+(cell_width+padding)*6,y+(cell_height+padding)*i,cell_width,cell_height,hwnd,NULL,NULL,NULL);
  231. CreateWindow("Static",(LPSTR)PersonBook[i-1].address,WS_VISIBLE|WS_CHILD|ES_LEFT, x+(cell_width+padding)*7,y+(cell_height+padding)*i,cell_width*4,cell_height,hwnd,NULL,NULL,NULL);
  232. //CreateWindow("Button","修改",WS_VISIBLE|WS_CHILD|ES_CENTER, x+(cell_width+padding)*11,y+(cell_height+padding)*i,cell_width/2,cell_height,hwnd,NULL,NULL,NULL);
  233. //CreateWindow("Button","删除",WS_VISIBLE|WS_CHILD|ES_CENTER, x+(cell_width+padding)*11+cell_width/2,y+(cell_height+padding)*i,cell_width/2,cell_height,hwnd,NULL,NULL,NULL);
  234. }
  235. itoa(num,str,10);
  236. itoa(numPerson,str1,10);
  237. char s1[30];
  238. strcpy(s1,"");
  239. strcat(s1,"每页 ");
  240. strcat(s1,str);
  241. strcat(s1," 条数据");
  242. char s2[30];
  243. strcpy(s2,"");
  244. strcat(s2,"总共 ");
  245. strcat(s2,str1);
  246. strcat(s2," 条数据");
  247. CreateWindow("Static","",WS_VISIBLE|WS_CHILD|ES_LEFT,0,y+(cell_height+padding)*(num+1)+8,cxScreen,30,hwnd,NULL,NULL,NULL);
  248. //CreateWindow("Static",s1,WS_VISIBLE|WS_CHILD|ES_LEFT,10,y+(cell_height+padding)*(num+1)+8,cxScreen/12,30,hwnd,NULL,NULL,NULL);
  249. CreateWindow("Static",s2,WS_VISIBLE|WS_CHILD|ES_LEFT,20+cxScreen/12,y+(cell_height+padding)*(num+1)+8,cxScreen/12,30,hwnd,NULL,NULL,NULL);
  250. //CreateWindow("Button","上一页",WS_VISIBLE|WS_CHILD|ES_LEFT,cxScreen/2+(cxScreen/12)*2,y+(cell_height+padding)*(num+1)+8,cxScreen/12,30,hwnd,NULL,NULL,NULL);
  251. //CreateWindow("Button","第",WS_VISIBLE|WS_CHILD|ES_CENTER,cxScreen/2+(cxScreen/12)*3,y+(cell_height+padding)*(num+1)+8,cxScreen/36,30,hwnd,NULL,NULL,NULL);
  252. //CreateWindow("Edit","1",WS_VISIBLE|WS_CHILD|ES_CENTER,cxScreen/2+(cxScreen/12)*3+cxScreen/36,y+(cell_height+padding)*(num+1)+8,cxScreen/36,30,hwnd,NULL,NULL,NULL);
  253. //CreateWindow("Button","页",WS_VISIBLE|WS_CHILD|ES_CENTER,cxScreen/2+(cxScreen/12)*3+cxScreen/18,y+(cell_height+padding)*(num+1)+8,cxScreen/36,30,hwnd,NULL,NULL,NULL);
  254. //CreateWindow("Button","下一页",WS_VISIBLE|WS_CHILD|ES_LEFT,cxScreen/2+(cxScreen/12)*4,y+(cell_height+padding)*(num+1)+8,cxScreen/12,30,hwnd,NULL,NULL,NULL);
  255. CreateWindow("Static","仅供学习之用——小浣熊制作",WS_VISIBLE|WS_CHILD|ES_CENTER,0,y+(cell_height+padding)*(num+1)+42,cxScreen,50,hwnd,NULL,NULL,NULL);
  256. //CreateWindow("Static","vf",WS_VISIBLE|WS_CHILD|WS_HSCROLL|WS_VSCROLL,x,y+(cell_width+padding)*11,12,100,hwnd,NULL,NULL,NULL);
  257. }
  258. /* Initialization of Window Message*/
  259. void InitDialog(HWND hwnd)
  260. {
  261. MessageBox(hwnd,"欢迎使用通讯录v2.0","通讯录",MB_OK);
  262. MessageBox(hwnd,"请先导入通讯录文件!","通讯录",MB_OK);
  263. }
  264. LRESULT CALLBACK DialogProcedure(HWND hWnd,UINT msg,WPARAM wp,LPARAM lp)
  265. {
  266. int val;
  267. int flag=0;
  268. int i;
  269. switch(msg)
  270. {
  271. /*case WM_CREATE:
  272. addControls(hWnd);*/
  273. case WM_COMMAND:
  274. switch(wp)
  275. {
  276. case CREATE:
  277. char number[30],name[30],gender[30],age[30],sclass[30],phone[30],address[30];
  278. GetWindowText(hNumber,number,30);
  279. GetWindowText(hName,name,30);
  280. GetWindowText(hGender,gender,30);
  281. GetWindowText(hAge,age,30);
  282. GetWindowText(hClass,sclass,30);
  283. GetWindowText(hPhone,phone,30);
  284. GetWindowText(hAddress,address,30);
  285. if(strcmp(number,"")==0||strcmp(name,"")==0||strcmp(gender,"")==0||strcmp(age,"")==0||strcmp(sclass,"")==0||strcmp(phone,"")==0||strcmp(address,"")==0)
  286. {
  287. val=MessageBox(hwnd,"请填写完整!",NULL,MB_ABORTRETRYIGNORE|MB_ICONERROR);
  288. switch(val)
  289. {
  290. case IDABORT:
  291. DestroyWindow(hWnd);
  292. break;
  293. case IDRETRY:
  294. return 0;
  295. case IDIGNORE:
  296. break;
  297. }
  298. }
  299. else{
  300. strcpy(PersonBook[numPerson].num,number);
  301. strcpy(PersonBook[numPerson].name,name);
  302. strcpy(PersonBook[numPerson].gender,gender);
  303. strcpy(PersonBook[numPerson].age,age);
  304. strcpy(PersonBook[numPerson].major,sclass);
  305. strcpy(PersonBook[numPerson].phone,phone);
  306. strcpy(PersonBook[numPerson].address,address);
  307. numPerson++;
  308. MessageBox(hwnd,"录入成功!","添加联系人",MB_OK);
  309. }
  310. break;
  311. case READ:
  312. //char number[30],name[30],gender[30],age[30],sclass[30],phone[30],address[30];
  313. GetWindowText(hNumber,number,30);
  314. for(i=0;i<numPerson;i++)
  315. {
  316. if(strcmp(number,PersonBook[i].num)==0){
  317. flag=1;break;
  318. }
  319. }
  320. if(flag==1)
  321. {
  322. //SetWindowText(hNumber,number);
  323. SetWindowText(hName,PersonBook[i].name);
  324. SetWindowText(hGender,PersonBook[i].gender);
  325. SetWindowText(hAge,PersonBook[i].age);
  326. SetWindowText(hClass,PersonBook[i].major);
  327. SetWindowText(hPhone,PersonBook[i].phone);
  328. SetWindowText(hAddress,PersonBook[i].address);
  329. }
  330. else{
  331. val=MessageBox(hWnd,"查无此人!",NULL,MB_ABORTRETRYIGNORE|MB_ICONERROR);
  332. switch(val)
  333. {
  334. case IDABORT:
  335. DestroyWindow(hwnd);
  336. break;
  337. case IDRETRY:
  338. return 0;
  339. case IDIGNORE:
  340. break;
  341. }
  342. }
  343. break;
  344. case UPDATE:
  345. //MessageBox(hwnd,"修改成功!","修改联系人",MB_OK);
  346. GetWindowText(hNumber,number,30);
  347. GetWindowText(hName,name,30);
  348. GetWindowText(hGender,gender,30);
  349. GetWindowText(hAge,age,30);
  350. GetWindowText(hClass,sclass,30);
  351. GetWindowText(hPhone,phone,30);
  352. GetWindowText(hAddress,address,30);
  353. for(i=0;i<numPerson;i++)
  354. {
  355. if(strcmp(number,PersonBook[i].num)==0){
  356. flag=1;break;
  357. }
  358. }
  359. if(strcmp(number,"")==0||strcmp(name,"")==0||strcmp(gender,"")==0||strcmp(age,"")==0||strcmp(sclass,"")==0||strcmp(phone,"")==0||strcmp(address,"")==0)
  360. {
  361. val=MessageBox(hwnd,"请填写完整!",NULL,MB_ABORTRETRYIGNORE|MB_ICONERROR);
  362. switch(val)
  363. {
  364. case IDABORT:
  365. DestroyWindow(hWnd);
  366. break;
  367. case IDRETRY:
  368. return 0;
  369. case IDIGNORE:
  370. break;
  371. }
  372. }
  373. else{
  374. strcpy(PersonBook[i].num,number);
  375. strcpy(PersonBook[i].name,name);
  376. strcpy(PersonBook[i].gender,gender);
  377. strcpy(PersonBook[i].age,age);
  378. strcpy(PersonBook[i].major,sclass);
  379. strcpy(PersonBook[i].phone,phone);
  380. strcpy(PersonBook[i].address,address);
  381. MessageBox(hwnd,"修改成功!","修改联系人",MB_OK);
  382. }
  383. break;
  384. case DEL:
  385. //MessageBox(hwnd,"删除成功!","删除联系人",MB_OK);
  386. GetWindowText(hNumber,number,30);
  387. GetWindowText(hName,name,30);
  388. GetWindowText(hGender,gender,30);
  389. GetWindowText(hAge,age,30);
  390. GetWindowText(hClass,sclass,30);
  391. GetWindowText(hPhone,phone,30);
  392. GetWindowText(hAddress,address,30);
  393. for(i=0;i<numPerson;i++)
  394. {
  395. if(strcmp(number,PersonBook[i].num)==0){
  396. flag=1;break;
  397. }
  398. }
  399. if(strcmp(number,"")==0)
  400. {
  401. val=MessageBox(hwnd,"请填写完整!",NULL,MB_ABORTRETRYIGNORE|MB_ICONERROR);
  402. switch(val)
  403. {
  404. case IDABORT:
  405. DestroyWindow(hWnd);
  406. break;
  407. case IDRETRY:
  408. return 0;
  409. case IDIGNORE:
  410. break;
  411. }
  412. }
  413. else{
  414. numPerson--;
  415. strcpy(PersonBook[i].num,PersonBook[numPerson].num);
  416. strcpy(PersonBook[i].name,PersonBook[numPerson].name);
  417. strcpy(PersonBook[i].gender,PersonBook[numPerson].gender);
  418. strcpy(PersonBook[i].age,PersonBook[numPerson].age);
  419. strcpy(PersonBook[i].major,PersonBook[numPerson].major);
  420. strcpy(PersonBook[i].phone,PersonBook[numPerson].phone);
  421. strcpy(PersonBook[i].address,PersonBook[numPerson].address);
  422. strcpy(PersonBook[numPerson].num,"");
  423. strcpy(PersonBook[numPerson].name,"");
  424. strcpy(PersonBook[numPerson].gender,"");
  425. strcpy(PersonBook[numPerson].age,"");
  426. strcpy(PersonBook[numPerson].major,"");
  427. strcpy(PersonBook[numPerson].phone,"");
  428. strcpy(PersonBook[numPerson].address,"");
  429. MessageBox(hwnd,"删除成功!","删除联系人",MB_OK);
  430. }
  431. break;
  432. }
  433. break;
  434. case WM_CLOSE:
  435. EnableWindow(hwnd,true);
  436. DestroyWindow(hWnd);
  437. InitWindow(hwnd);
  438. break;
  439. default:
  440. return DefWindowProcW(hWnd,msg,wp,lp);
  441. }
  442. }
  443. void registerDialogClass(HINSTANCE hInst)
  444. {
  445. WNDCLASSW dialog={0};
  446. dialog.hbrBackground=(HBRUSH)COLOR_WINDOW;
  447. dialog.hCursor=LoadCursor(NULL,IDC_CROSS);
  448. dialog.hInstance=hInst;
  449. dialog.lpszClassName=L"myDialogClass";
  450. dialog.lpfnWndProc=DialogProcedure;
  451. RegisterClassW(&dialog);
  452. }
  453. void displayDialogCreate(HWND hWnd)
  454. {
  455. char* szStr = "添加联系人";
  456. WCHAR wszClassName[256];
  457. memset(wszClassName,0,sizeof(wszClassName));
  458. MultiByteToWideChar(CP_ACP,0,szStr,strlen(szStr)+1,wszClassName,
  459. sizeof(wszClassName)/sizeof(wszClassName[0]));
  460. HWND hDlg=CreateWindowW(L"myDialogClass",wszClassName,WS_VISIBLE|WS_OVERLAPPEDWINDOW,100,100,640,480,hWnd,NULL,NULL,NULL);
  461. //CreateWindowW(L"Button",L"Close",WS_VISIBLE|WS_CHILD,20,20,100,40,hDlg,(HMENU)1,NULL,NULL);
  462. displayDialogCreateAddControls(hDlg);
  463. EnableWindow(hWnd,false);
  464. }
  465. void displayDialogCreateAddControls(HWND hWnd)
  466. {
  467. int cxScreen, cyScreen ;
  468. cxScreen = GetSystemMetrics (SM_CXSCREEN) ;
  469. cyScreen = GetSystemMetrics (SM_CYSCREEN) ;
  470. CreateWindow("Static","学号 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.1,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
  471. hNumber=CreateWindow("Edit","", WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.1,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
  472. CreateWindow("Static","姓名 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.2,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
  473. hName=CreateWindow("Edit","", WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.2,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
  474. CreateWindow("Static","性别 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.3,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
  475. hGender=CreateWindow("Edit","", WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.3,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
  476. CreateWindow("Static","年龄 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.4,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
  477. hAge=CreateWindow("Edit","", WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.4,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
  478. CreateWindow("Static","班级 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.5,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
  479. hClass=CreateWindow("Edit","", WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.5,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
  480. CreateWindow("Static","电话 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.6,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
  481. hPhone=CreateWindow("Edit","", WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.6,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
  482. CreateWindow("Static","地址 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.7,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
  483. hAddress=CreateWindow("Edit","", WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.7,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
  484. CreateWindow("Button","添加",WS_VISIBLE|WS_CHILD,cxScreen*0.2,cyScreen*0.8,cxScreen*0.2,cyScreen*0.05,hWnd,(HMENU)CREATE,NULL,NULL);
  485. //SendMessageW(hBut,BM_SETIMAGE,IMAGE_BITMAP,(LPARAM)hGenerateImage);
  486. //hOut=CreateWindow("Edit","",WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.5,cyScreen*0.1,cxScreen*0.3,cyScreen*0.3,hWnd,NULL,NULL,NULL);
  487. //CreateWindowW(L"Static",NULL,WS_VISIBLE|WS_CHILD,350,60,100,100,hWnd,NULL,NULL,NULL);
  488. //SendMessageW(hLogo,STM_SETIMAGE,IMAGE_BITMAP,(LPARAM)hLogoImage);
  489. }
  490. void displayDialogRead(HWND hWnd)
  491. {
  492. char* szStr = "查询联系人";
  493. WCHAR wszClassName[256];
  494. memset(wszClassName,0,sizeof(wszClassName));
  495. MultiByteToWideChar(CP_ACP,0,szStr,strlen(szStr)+1,wszClassName,
  496. sizeof(wszClassName)/sizeof(wszClassName[0]));
  497. HWND hDlg=CreateWindowW(L"myDialogClass",wszClassName,WS_VISIBLE|WS_OVERLAPPEDWINDOW,100,100,640,480,hWnd,NULL,NULL,NULL);
  498. //CreateWindowW(L"Button",L"Close",WS_VISIBLE|WS_CHILD,20,20,100,40,hDlg,(HMENU)1,NULL,NULL);
  499. displayDialogReadAddControls(hDlg);
  500. EnableWindow(hWnd,false);
  501. }
  502. void displayDialogReadAddControls(HWND hWnd)
  503. {
  504. int cxScreen, cyScreen ;
  505. cxScreen = GetSystemMetrics (SM_CXSCREEN) ;
  506. cyScreen = GetSystemMetrics (SM_CYSCREEN) ;
  507. CreateWindow("Static","学号 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.1,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
  508. hNumber=CreateWindow("Edit","", WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.1,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
  509. CreateWindow("Static","姓名 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.2,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
  510. hName=CreateWindow("Static","", WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.2,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
  511. CreateWindow("Static","性别 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.3,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
  512. hGender=CreateWindow("Static","", WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.3,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
  513. CreateWindow("Static","年龄 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.4,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
  514. hAge=CreateWindow("Static","", WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.4,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
  515. CreateWindow("Static","班级 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.5,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
  516. hClass=CreateWindow("Static","", WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.5,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
  517. CreateWindow("Static","电话 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.6,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
  518. hPhone=CreateWindow("Static","", WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.6,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
  519. CreateWindow("Static","地址 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.7,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
  520. hAddress=CreateWindow("Static","", WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.7,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
  521. CreateWindow("Button","查询",WS_VISIBLE|WS_CHILD,cxScreen*0.2,cyScreen*0.8,cxScreen*0.2,cyScreen*0.05,hWnd,(HMENU)READ,NULL,NULL);
  522. }
  523. void displayDialogUpdate(HWND hWnd)
  524. {
  525. char* szStr = "修改联系人";
  526. WCHAR wszClassName[256];
  527. memset(wszClassName,0,sizeof(wszClassName));
  528. MultiByteToWideChar(CP_ACP,0,szStr,strlen(szStr)+1,wszClassName,
  529. sizeof(wszClassName)/sizeof(wszClassName[0]));
  530. HWND hDlg=CreateWindowW(L"myDialogClass",wszClassName,WS_VISIBLE|WS_OVERLAPPEDWINDOW,100,100,640,480,hWnd,NULL,NULL,NULL);
  531. //CreateWindowW(L"Button",L"Close",WS_VISIBLE|WS_CHILD,20,20,100,40,hDlg,(HMENU)1,NULL,NULL);
  532. displayDialogUpdateAddControls(hDlg);
  533. EnableWindow(hWnd,false);
  534. }
  535. void displayDialogUpdateAddControls(HWND hWnd)
  536. {
  537. int cxScreen, cyScreen ;
  538. cxScreen = GetSystemMetrics (SM_CXSCREEN) ;
  539. cyScreen = GetSystemMetrics (SM_CYSCREEN) ;
  540. CreateWindow("Static","学号 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.1,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
  541. hNumber=CreateWindow("Edit","", WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.1,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
  542. CreateWindow("Static","姓名 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.2,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
  543. hName=CreateWindow("Edit","", WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.2,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
  544. CreateWindow("Static","性别 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.3,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
  545. hGender=CreateWindow("Edit","", WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.3,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
  546. CreateWindow("Static","年龄 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.4,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
  547. hAge=CreateWindow("Edit","", WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.4,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
  548. CreateWindow("Static","班级 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.5,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
  549. hClass=CreateWindow("Edit","", WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.5,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
  550. CreateWindow("Static","电话 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.6,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
  551. hPhone=CreateWindow("Edit","", WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.6,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
  552. CreateWindow("Static","地址 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.7,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
  553. hAddress=CreateWindow("Edit","", WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.7,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
  554. CreateWindow("Button","查询",WS_VISIBLE|WS_CHILD,cxScreen*0.2,cyScreen*0.8,cxScreen*0.08,cyScreen*0.05,hWnd,(HMENU)READ,NULL,NULL);
  555. CreateWindow("Button","修改",WS_VISIBLE|WS_CHILD,cxScreen*0.32,cyScreen*0.8,cxScreen*0.08,cyScreen*0.05,hWnd,(HMENU)UPDATE,NULL,NULL);
  556. }
  557. void displayDialogDel(HWND hWnd)
  558. {
  559. char* szStr = "删除联系人";
  560. WCHAR wszClassName[256];
  561. memset(wszClassName,0,sizeof(wszClassName));
  562. MultiByteToWideChar(CP_ACP,0,szStr,strlen(szStr)+1,wszClassName,
  563. sizeof(wszClassName)/sizeof(wszClassName[0]));
  564. HWND hDlg=CreateWindowW(L"myDialogClass",wszClassName,WS_VISIBLE|WS_OVERLAPPEDWINDOW,100,100,640,480,hWnd,NULL,NULL,NULL);
  565. //CreateWindowW(L"Button",L"Close",WS_VISIBLE|WS_CHILD,20,20,100,40,hDlg,(HMENU)1,NULL,NULL);
  566. displayDialogDelAddControls(hDlg);
  567. EnableWindow(hWnd,false);
  568. }
  569. void displayDialogDelAddControls(HWND hWnd)
  570. {
  571. int cxScreen, cyScreen ;
  572. cxScreen = GetSystemMetrics (SM_CXSCREEN) ;
  573. cyScreen = GetSystemMetrics (SM_CYSCREEN) ;
  574. CreateWindow("Static","学号 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.1,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
  575. hNumber=CreateWindow("Edit","", WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.1,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
  576. CreateWindow("Static","姓名 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.2,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
  577. hName=CreateWindow("Static","", WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.2,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
  578. CreateWindow("Static","性别 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.3,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
  579. hGender=CreateWindow("Static","", WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.3,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
  580. CreateWindow("Static","年龄 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.4,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
  581. hAge=CreateWindow("Static","", WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.4,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
  582. CreateWindow("Static","班级 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.5,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
  583. hClass=CreateWindow("Static","", WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.5,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
  584. CreateWindow("Static","电话 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.6,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
  585. hPhone=CreateWindow("Static","", WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.6,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
  586. CreateWindow("Static","地址 :", WS_VISIBLE|WS_CHILD|ES_RIGHT,cxScreen*0.1,cyScreen*0.7,cxScreen*0.1,cyScreen*0.08,hWnd,NULL,NULL,NULL);
  587. hAddress=CreateWindow("Static","", WS_VISIBLE|WS_CHILD|WS_BORDER,cxScreen*0.2,cyScreen*0.7,cxScreen*0.2,cyScreen*0.05,hWnd,NULL,NULL,NULL);
  588. CreateWindow("Button","查询",WS_VISIBLE|WS_CHILD,cxScreen*0.2,cyScreen*0.8,cxScreen*0.08,cyScreen*0.05,hWnd,(HMENU)READ,NULL,NULL);
  589. CreateWindow("Button","删除",WS_VISIBLE|WS_CHILD,cxScreen*0.32,cyScreen*0.8,cxScreen*0.08,cyScreen*0.05,hWnd,(HMENU)DEL,NULL,NULL);
  590. }
  591. void display_file(char* path)
  592. {
  593. FILE *file;
  594. file=fopen(path,"rb");
  595. fseek(file,0,SEEK_END);
  596. int _size=ftell(file);
  597. rewind(file);
  598. //char *data=new char[_size+1];
  599. fread(PersonBook,_size,1,file);
  600. //PersonBook[_size]='\0';
  601. /*char str[10];
  602. itoa(_size,str,10);
  603. MessageBox(NULL,str,"",MB_OK);*/
  604. //SetWindowText(hEdit,data);
  605. fclose(file);
  606. }
  607. void open_file(HWND hWnd)
  608. {
  609. OPENFILENAME ofn;
  610. char file_name[100];
  611. ZeroMemory(&ofn,sizeof(OPENFILENAME));
  612. ofn.lStructSize=sizeof(OPENFILENAME);
  613. ofn.hwndOwner=hWnd;
  614. ofn.lpstrFile=file_name;
  615. ofn.lpstrFile[0]='\0';
  616. ofn.nMaxFile=100;
  617. ofn.lpstrFilter="All files\0*.*\0Source Files\0*.CPP\0Text Files\0*.TXT\0";
  618. ofn.nFilterIndex=1;
  619. GetOpenFileName(&ofn);
  620. display_file(ofn.lpstrFile);
  621. MessageBox(NULL,ofn.lpstrFile,"",MB_OK);
  622. }
  623. void write_file(char* path)
  624. {
  625. FILE *file;
  626. file=fopen(path,"w");
  627. /*int _size=GetWindowTextLength(hEdit);
  628. char *data=new char[_size+1];
  629. GetWindowText(hEdit,data,_size+1);*/
  630. fwrite(PersonBook,230*item,1,file);
  631. fclose(file);
  632. }
  633. void save_file(HWND hWnd)
  634. {
  635. OPENFILENAME ofn;
  636. char file_name[100];
  637. ZeroMemory(&ofn,sizeof(OPENFILENAME));
  638. ofn.lStructSize=sizeof(OPENFILENAME);
  639. ofn.hwndOwner=hWnd;
  640. ofn.lpstrFile=file_name;
  641. ofn.lpstrFile[0]='\0';
  642. ofn.nMaxFile=100;
  643. ofn.lpstrFilter="All files\0*.*\0Source Files\0*.CPP\0Text Files\0*.TXT\0";
  644. ofn.nFilterIndex=1;
  645. GetSaveFileName(&ofn);
  646. write_file(ofn.lpstrFile);
  647. //MessageBox(NULL,ofn.lpstrFile,"",MB_OK);
  648. }

截图

主界面

 删除联系人

 添加联系人

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/109265
推荐阅读
相关标签
  

闽ICP备14008679号