This Blog is dedicated to Sir Johnson Lin
Learn flow chart from C language , master C with the chart!

Windows c++ program : MDI text editor. 視窗版 c 語言程式 : 多文件/多視窗文字編輯器



D:\cppCodes\EditorMDI\EditorMDI.c++
  1 //++++++++++++++++++++++++++
  2  //      EditorMDI.cpp
  3  //++++++++++++++++++++++++++
  4  
  5  #include "EditorMDI.h"//Win32 mode , auto include xxx.h
  6  ///#include header
  7  #include <windows.h>
  8  #include <commctrl.h>
  9  
 10  ///define ID/IDC
 11  #define ID_STATUSBAR       4997
 12  #define ID_TOOLBAR         4998
 13  #define ID_MDI_CLIENT      4999
 14  #define ID_MDI_FIRSTCHILD  50000
 15  #define IDC_CHILD_EDIT      2000
 16  
 17  ///declare CALLBACK
 18  LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
 19  LRESULT CALLBACK MDIChildWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
 20  
 21  ///declare vairable
 22  char g_szAppName[] = "MyMDIWindow";
 23  char g_szChild[] = "MyMDIChild";
 24  HINSTANCE g_hInst;
 25  HWND g_hMDIClient, g_hStatusBar, g_hToolBar;
 26  HWND g_hMainWindow;
 27  HWND u_hEdit; // For ChangeFont(u_hEdit, 32);
 28  int fontscale = 28;
 29  
 30  ///ChangeFont
 31  BOOL ChangeFont(HWND x_hEdit, int Scale)
 32  {
 33      HFONT hfont;
 34      LOGFONT logFont;
 35      memset(&logFont, 0, sizeof(logFont));
 36      logFont.lfHeight = -1*Scale; // see PS
 37      logFont.lfWeight = FW_BOLD;
 38      strcpy(logFont.lfFaceName, "Courier New");
 39      hfont=CreateFontIndirect(&logFont);
 40      
 41      SendMessage(x_hEdit, WM_SETFONT,(WPARAM)hfont,MAKELPARAM(TRUE, 0));
 42      return TRUE;
 43  }
 44  
 45  ///LoadFile
 46  BOOL LoadFile(HWND hEdit, LPSTR pszFileName)
 47  {
 48      HANDLE hFile;
 49      BOOL bSuccess = FALSE;
 50      
 51      hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
 52      if(hFile != INVALID_HANDLE_VALUE)
 53      {
 54          DWORD dwFileSize;
 55          dwFileSize = GetFileSize(hFile, NULL);
 56          if(dwFileSize != 0xFFFFFFFF)
 57          {
 58              LPSTR pszFileText;
 59              pszFileText = LPSTR(GlobalAlloc(GPTR, dwFileSize + 1));
 60              if(pszFileText != NULL)
 61              {
 62                  DWORD dwRead;
 63                  if(ReadFile(hFile, pszFileText, dwFileSize, &dwRead, NULL))
 64                  {
 65                      pszFileText[dwFileSize] = 0; // eof terminator
 66                      if(SetWindowText(hEdit, pszFileText)) bSuccess = TRUE; // post string to client
 67                  }
 68                  GlobalFree(pszFileText);
 69              }
 70          }
 71          CloseHandle(hFile);
 72      }
 73      return bSuccess;
 74  }
 75  
 76  ///SaveFile
 77  BOOL SaveFile(HWND hEdit, LPSTR pszFileName)
 78  {
 79      HANDLE hFile;
 80      BOOL bSuccess = FALSE;
 81      
 82      hFile = CreateFile(pszFileName, GENERIC_WRITE, 0, NULL,
 83      CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
 84      if(hFile != INVALID_HANDLE_VALUE)
 85      {
 86          DWORD dwTextLength;
 87          dwTextLength = GetWindowTextLength(hEdit);
 88          if(dwTextLength > 0)// avoid blank text file
 89          {
 90              LPSTR pszText;
 91              pszText = LPSTR(GlobalAlloc(GPTR, dwTextLength + 1));
 92              if(pszText != NULL)
 93              {
 94                  if(GetWindowText(hEdit, pszText, dwTextLength + 1))
 95                  {
 96                      DWORD dwWritten;
 97                      if(WriteFile(hFile, pszText, dwTextLength, &dwWritten, NULL))
 98                      bSuccess = TRUE;
 99                  }
100                  GlobalFree(pszText);
101              }
102          }
103          CloseHandle(hFile);
104      }
105      return bSuccess;
106  }
107  
108  ///DoFlOpSv
109  BOOL DoFileOpenSave(HWND hwnd, LPSTR pszFileName, BOOL bSave)
110  {
111      OPENFILENAME ofn;
112      
113      ZeroMemory(&ofn, sizeof(ofn));
114      pszFileName[0] = 0;
115      
116      ofn.lStructSize = sizeof(ofn);
117      ofn.hwndOwner = hwnd;
118      ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0\0";
119      ofn.lpstrFile = pszFileName;
120      ofn.nMaxFile = MAX_PATH;
121      ofn.lpstrDefExt = "txt";
122      
123      if(bSave)
124      {
125          ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
126          if(!GetSaveFileName(&ofn))
127          return FALSE;
128      }
129      else
130      {
131          ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
132          if(!GetOpenFileName(&ofn))
133          return FALSE;
134      }
135      return TRUE;
136  }
137  
138  ///WinMain
139  int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdParam, int nCmdShow)
140  {
141      ///declare struct
142      MSG  Msg;
143      WNDCLASSEX WndClassEx;
144      
145      ///InitCommonControls
146      InitCommonControls();//define in MinGW\include\commctrl.h
147      
148      ///setting
149      g_hInst = hInstance;
150      
151      ///Register WndProc
152      WndClassEx.cbSize          = sizeof(WNDCLASSEX);
153      WndClassEx.style           = CS_HREDRAW | CS_VREDRAW;
154      WndClassEx.lpfnWndProc     = WndProc;
155      WndClassEx.cbClsExtra      = 0;
156      WndClassEx.cbWndExtra      = 0;
157      WndClassEx.hInstance       = hInstance;
158      WndClassEx.hIcon           = LoadIcon(NULL, IDI_APPLICATION);
159      WndClassEx.hCursor         = LoadCursor(NULL, IDC_ARROW);
160      WndClassEx.hbrBackground   = (HBRUSH)(COLOR_3DSHADOW+1);
161      WndClassEx.lpszMenuName    = "MAINMENU"; //xxx.rc need define the same name
162      WndClassEx.lpszClassName   = g_szAppName; // = WndProc, Will run 1st CreatWindowEx
163      WndClassEx.hIconSm         = LoadIcon(NULL, IDI_APPLICATION);
164      
165      ///register ?
166      if(!RegisterClassEx(&WndClassEx))
167      {
168          MessageBox(0, "Could Not Register Window", "Oh Oh...", MB_ICONEXCLAMATION | MB_OK);
169          return -1;
170      }
171      
172      ///Register MDIChildWndProc
173      WndClassEx.lpfnWndProc     = MDIChildWndProc;
174      WndClassEx.lpszMenuName    = NULL;
175      WndClassEx.lpszClassName   = g_szChild;
176      WndClassEx.hbrBackground   = (HBRUSH)(COLOR_3DFACE+1);
177      
178      ///register MDI?
179      if(!RegisterClassEx(&WndClassEx))
180      {
181          MessageBox(0, "Could Not Register Child Window", "Oh Oh...", MB_ICONEXCLAMATION | MB_OK);
182          return -1;
183      }
184      
185      ///Create WindowEx
186      g_hMainWindow = CreateWindowEx
187      (
188        WS_EX_APPWINDOW,
189        g_szAppName,     //WinProc=g_szAppName,MDIChildWndProc=g_szChild
190        "MDI File Editor",
191        WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
192        CW_USEDEFAULT,
193        CW_USEDEFAULT,
194        CW_USEDEFAULT,
195        CW_USEDEFAULT,
196        0,
197        0,
198        hInstance,
199        NULL
200      );
201      
202      ///create =NULL?
203      if (g_hMainWindow == NULL)
204      {
205          MessageBox(0, "No Window", "Oh Oh...", MB_ICONEXCLAMATION | MB_OK);
206          return -1;
207      }
208      
209      ///Show Window
210      ShowWindow(g_hMainWindow, nCmdShow);
211      
212      ///Update Window
213      UpdateWindow(g_hMainWindow);
214      
215      ///Polling GetMsg
216      while(GetMessage(&Msg, NULL, 0, 0) > 0) // =0 mean WM_QUIT, =-1 abnormal
217      {
218          ///Msg for MDI?
219          if (!TranslateMDISysAccel(g_hMDIClient, &Msg))
220          {
221              ///Msg for WndProc
222              TranslateMessage(&Msg);// if no, will diable queue from keyboard
223              DispatchMessage(&Msg);// if no , will disable all mouse,key active, Call WndProc
224              
225              ///+
226          }//end if
227          
228          ///+
229      }//end while
230      
231      ///program quit
232      MessageBox(0, "PGM is closed ! ", "Ending",MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
233      return Msg.wParam;
234      
235      ///main_end
236  }//end main
237  
238  ///WndProc
239  LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) //Dispatchmsg(&Msg); will call it indirectly
240  {
241      ///switch msg
242      switch(msg)
243      {
244          ///WM_ CREATE
245          case WM_CREATE:
246          {
247              CLIENTCREATESTRUCT ccs;
248              int iStatusWidths[] = {200, 300, -1};
249              TBADDBITMAP tbab;
250              TBBUTTON tbb[9];
251              
252              // Find window menu where children will be listed
253              ccs.hWindowMenu  = GetSubMenu(GetMenu(hwnd), 2);
254              ccs.idFirstChild = ID_MDI_FIRSTCHILD;
255              g_hMDIClient = CreateWindowEx(WS_EX_CLIENTEDGE, "mdiclient",NULL,WS_CHILD | WS_CLIPCHILDREN | WS_VSCROLL | WS_HSCROLL,CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,hwnd, (HMENU)ID_MDI_CLIENT, g_hInst, (LPVOID)&ccs);
256              ShowWindow(g_hMDIClient, SW_SHOW);
257              
258              g_hStatusBar = CreateWindowEx(0, STATUSCLASSNAME, NULL,WS_CHILD | WS_VISIBLE | SBARS_SIZEGRIP, 0, 0, 0, 0,
259              hwnd, (HMENU)ID_STATUSBAR, g_hInst, NULL);
260              SendMessage(g_hStatusBar, SB_SETPARTS, 3, (LPARAM)iStatusWidths);
261              SendMessage(g_hStatusBar, SB_SETTEXT, 2, (LPARAM)"Toolbar & Statusbar Example");
262              
263              g_hToolBar = CreateWindowEx(0, TOOLBARCLASSNAME, NULL,
264                          WS_CHILD | WS_VISIBLE, 0, 0, 0, 0,
265                          hwnd, (HMENU)ID_TOOLBAR, g_hInst, NULL);
266              
267              // Send the TB_BUTTONSTRUCTSIZE msg, which is required for
268              // backward compatibility.
269              SendMessage(g_hToolBar, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);
270              
271              tbab.hInst = HINST_COMMCTRL;
272              tbab.nID = IDB_STD_SMALL_COLOR;
273              SendMessage(g_hToolBar, TB_ADDBITMAP, 0, (LPARAM)&tbab);
274              
275              ZeroMemory(tbb, sizeof(tbb));
276              
277              tbb[0].iBitmap = STD_FILENEW;
278              tbb[0].fsState = TBSTATE_ENABLED;
279              tbb[0].fsStyle = TBSTYLE_BUTTON;
280              tbb[0].idCommand = CM_FILE_NEW;
281              
282              tbb[1].iBitmap = STD_FILEOPEN;
283              tbb[1].fsState = TBSTATE_ENABLED;
284              tbb[1].fsStyle = TBSTYLE_BUTTON;
285              tbb[1].idCommand = CM_FILE_OPEN;
286              
287              tbb[2].iBitmap = STD_FILESAVE;
288              tbb[2].fsStyle = TBSTYLE_BUTTON;
289              tbb[2].idCommand = CM_FILE_SAVE;
290              
291              tbb[3].fsStyle = TBSTYLE_SEP;
292              
293              tbb[4].iBitmap = STD_CUT;
294              tbb[4].fsStyle = TBSTYLE_BUTTON;
295              tbb[4].idCommand = CM_EDIT_CUT;
296              
297              tbb[5].iBitmap = STD_COPY;
298              tbb[5].fsStyle = TBSTYLE_BUTTON;
299              tbb[5].idCommand = CM_EDIT_COPY;
300              
301              tbb[6].iBitmap = STD_PASTE;
302              tbb[6].fsStyle = TBSTYLE_BUTTON;
303              tbb[6].idCommand = CM_EDIT_PASTE;
304              
305              tbb[7].fsStyle = TBSTYLE_SEP;
306              
307              tbb[8].iBitmap = STD_UNDO;
308              tbb[8].fsStyle = TBSTYLE_BUTTON;
309              tbb[8].idCommand = CM_EDIT_UNDO;
310              
311              SendMessage(g_hToolBar, TB_ADDBUTTONS, 9, (LPARAM)&tbb);
312              
313              return 0;
314          }
315          //needn't break
316          
317          ///WM_ Command
318          case WM_COMMAND:
319          {
320              ///switch LOWORD (wParam)
321              switch(LOWORD(wParam)) //a window macros to single out low 16 bits of 32 bits
322              {                      //wParam was sent by Dispatchmsg()
323                  
324                  ///CM_ FILE_ EXIT
325                  case CM_FILE_EXIT:
326                  {
327                      PostMessage(hwnd, WM_CLOSE, 0, 0);
328                  }
329                  break;
330                  
331                  ///CM_ FILE_ NEW
332                  case CM_FILE_NEW:
333                  {
334                      MDICREATESTRUCT mcs;
335                      HWND hChild;
336                      mcs.szTitle = "[Untitled]";
337                      mcs.szClass = g_szChild;
338                      mcs.hOwner  = g_hInst;
339                      mcs.x = mcs.cx = CW_USEDEFAULT;
340                      mcs.y = mcs.cy = CW_USEDEFAULT;
341                      mcs.style = MDIS_ALLCHILDSTYLES;
342                      
343                      hChild = (HWND)SendMessage(g_hMDIClient, WM_MDICREATE, 0, (LONG)&mcs);
344                      if(!hChild){ MessageBox(hwnd, "MDI Child creation failed.", "Oh Oh...", MB_ICONEXCLAMATION | MB_OK);}
345                  }
346                  break;
347                  
348                  ///CM_ FILE_ OPEN
349                  case CM_FILE_OPEN:
350                  {
351                      MDICREATESTRUCT mcs;
352                      HWND hChild;
353                      char szFileName[MAX_PATH];
354                      
355                      if(!DoFileOpenSave(hwnd, szFileName, FALSE)) break;
356                      
357                      mcs.szTitle = szFileName;
358                      mcs.szClass = g_szChild;
359                      mcs.hOwner  = g_hInst;
360                      mcs.x = mcs.cx = CW_USEDEFAULT;
361                      mcs.y = mcs.cy = CW_USEDEFAULT;
362                      mcs.style = MDIS_ALLCHILDSTYLES;
363                      
364                      hChild = (HWND)SendMessage(g_hMDIClient, WM_MDICREATE, 0, (LONG)&mcs);
365                      
366                      if(!hChild)
367                      { MessageBox(hwnd, "MDI Child creation failed.", "Oh Oh...",MB_ICONEXCLAMATION | MB_OK);}
368                  }
369                  break;
370                  
371                  ///CM_ WINDOW_ TILEHORZ
372                  case CM_WINDOW_TILEHORZ:
373                  {
374                      PostMessage(g_hMDIClient, WM_MDITILE, MDITILE_HORIZONTAL, 0);
375                  }
376                  break;
377                  
378                  ///CM_ WINDOW_ TILEVERT
379                  case CM_WINDOW_TILEVERT:
380                  {
381                      PostMessage(g_hMDIClient, WM_MDITILE, MDITILE_VERTICAL, 0);
382                  }
383                  break;
384                  
385                  ///CM_ WINDOW_ CASCADE
386                  case CM_WINDOW_CASCADE:
387                  {
388                      PostMessage(g_hMDIClient, WM_MDICASCADE, 0, 0);
389                  }
390                  break;
391                  
392                  ///CM_ WINDOW_ ARRANGE
393                  case CM_WINDOW_ARRANGE:
394                  {
395                      PostMessage(g_hMDIClient, WM_MDIICONARRANGE, 0, 0);
396                  }
397                  break;
398                  
399                  ///CM_ About
400                  case CM_HELP_ABOUT:
401                  MessageBox (NULL, "MDI Text Editor for Windows\n - Win32 API" , "About...", 0);
402                  break;
403                  
404                  ///default
405                  default:
406                  {
407                      if(LOWORD(wParam) >= ID_MDI_FIRSTCHILD)
408                      {
409                          DefFrameProc(hwnd, g_hMDIClient, msg, wParam,lParam);
410                      }
411                      else
412                      {
413                          HWND hChild;
414                          hChild = (HWND)SendMessage(g_hMDIClient, WM_MDIGETACTIVE,0,0);
415                          if(hChild)
416                          {
417                              SendMessage(hChild, WM_COMMAND, wParam, lParam);
418                          }
419                      }
420                  }
421                  
422                  ///+
423              }//end Switch
424              
425              ///+
426          }//end Case
427          break;
428          
429          ///WM_ SIZE
430          case WM_SIZE:
431          {
432              RECT rectClient, rectStatus, rectTool;
433              UINT uToolHeight, uStatusHeight, uClientAlreaHeight;
434              
435              SendMessage(g_hToolBar, TB_AUTOSIZE, 0, 0);
436              SendMessage(g_hStatusBar, WM_SIZE, 0, 0);
437              
438              GetClientRect(hwnd, &rectClient);
439              GetWindowRect(g_hStatusBar, &rectStatus);
440              GetWindowRect(g_hToolBar, &rectTool);
441              
442              uToolHeight = rectTool.bottom - rectTool.top;
443              uStatusHeight = rectStatus.bottom - rectStatus.top;
444              uClientAlreaHeight = rectClient.bottom;
445              
446              MoveWindow(g_hMDIClient, 0, uToolHeight, rectClient.right, uClientAlreaHeight - uStatusHeight - uToolHeight, TRUE);
447          }
448          break;
449          
450          ///WM_ CLOSE
451          case WM_CLOSE:
452          {
453              DestroyWindow(hwnd);
454          }
455          break;
456          
457          ///WM_ DESTROY
458          case WM_DESTROY:
459          {
460              PostQuitMessage(0);
461          }
462          break;
463          
464          ///default
465          default:
466          return DefFrameProc(hwnd, g_hMDIClient, msg,wParam,lParam);
467          
468          ///+
469      }//end Switch
470      
471      ///+
472      return 0;
473  }//end CALLBACK
474  
475  ///MDIChildWP
476  LRESULT CALLBACK MDIChildWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) //child frame open, be called while mouse move within the frame
477  {
478      ///switch msg
479      switch(msg)
480      {
481          ///WM_ create
482          case WM_CREATE:
483          {
484              char szFileName[MAX_PATH];
485              HWND hEdit;
486              
487              hEdit = CreateWindowEx(
488              WS_EX_CLIENTEDGE,
489              "EDIT",
490              "",
491              WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL | ES_MULTILINE | ES_WANTRETURN,
492              CW_USEDEFAULT,
493              CW_USEDEFAULT,
494              CW_USEDEFAULT,
495              CW_USEDEFAULT,
496              hwnd,
497              (HMENU)IDC_CHILD_EDIT,
498              g_hInst,
499              NULL
500              );
501              
502              //Sendmsg(hEdit,WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),MAKELPARAM(TRUE, 0));
503              
504              ChangeFont(hEdit, 28);
505              u_hEdit = hEdit;// pass to ChildProc
506              
507              GetWindowText(hwnd, szFileName, MAX_PATH);
508              if(*szFileName != '[')
509              {
510                  if(!LoadFile(hEdit, szFileName))
511                  {
512                      MessageBox(hwnd, "Couldn't Load File.", "Error.", MB_OK | MB_ICONEXCLAMATION);
513                      return -1; //cancel window creation
514                  }
515              }
516          }
517          break;
518          
519          ///WM_ SIZE
520          case WM_SIZE:
521          {
522              if(wParam != SIZE_MINIMIZED) MoveWindow(GetDlgItem(hwnd, IDC_CHILD_EDIT), 0, 0, LOWORD(lParam),HIWORD(lParam), TRUE);
523          }
524          break;
525          
526          ///WM_MDI ACTIVATE
527          case WM_MDIACTIVATE:
528          {
529              HMENU hMenu, hFileMenu;
530              BOOL EnableFlag;
531              char szFileName[MAX_PATH];
532              hMenu = GetMenu(g_hMainWindow);
533              if(hwnd == (HWND)lParam){EnableFlag = TRUE;}//being activated
534              else{ EnableFlag = FALSE;}    //being de-activated
535              
536              EnableMenuItem(hMenu, 1, MF_BYPOSITION | (EnableFlag ? MF_ENABLED : MF_GRAYED));
537              EnableMenuItem(hMenu, 2, MF_BYPOSITION | (EnableFlag ? MF_ENABLED : MF_GRAYED));
538              
539              hFileMenu = GetSubMenu(hMenu, 0);
540              EnableMenuItem(hFileMenu, CM_FILE_SAVE, MF_BYCOMMAND | (EnableFlag ? MF_ENABLED : MF_GRAYED));
541              EnableMenuItem(hFileMenu, CM_FILE_SAVEAS, MF_BYCOMMAND | (EnableFlag ? MF_ENABLED : MF_GRAYED));
542              
543              DrawMenuBar(g_hMainWindow);
544              
545              SendMessage(g_hToolBar, TB_ENABLEBUTTON, CM_FILE_SAVE, MAKELONG(EnableFlag, 0));
546              SendMessage(g_hToolBar, TB_ENABLEBUTTON, CM_EDIT_UNDO, MAKELONG(EnableFlag, 0));
547              SendMessage(g_hToolBar, TB_ENABLEBUTTON, CM_EDIT_CUT, MAKELONG(EnableFlag, 0));
548              SendMessage(g_hToolBar, TB_ENABLEBUTTON, CM_EDIT_COPY, MAKELONG(EnableFlag, 0));
549              SendMessage(g_hToolBar, TB_ENABLEBUTTON, CM_EDIT_PASTE, MAKELONG(EnableFlag, 0));
550              
551              GetWindowText(hwnd, szFileName, MAX_PATH);
552              SendMessage(g_hStatusBar, SB_SETTEXT, 0, (LPARAM)(EnableFlag ? szFileName : ""));
553          }
554          break;
555          
556          ///WM_SET FOCUS
557          case WM_SETFOCUS:
558          {
559              SetFocus(GetDlgItem(hwnd, IDC_CHILD_EDIT));
560          }
561          break;
562          
563          ///WM_ Command
564          case WM_COMMAND:
565          {
566              ///switch LOWORD (wParam)
567              switch(LOWORD(wParam)) //a window macros to single out low 16 bits of 32 bits
568              {                      //wParam was sent by Dispatchmsg()
569                  
570                  ///CM_ FILE_ SAVE
571                  case CM_FILE_SAVE:
572                  {
573                      char szFileName[MAX_PATH];
574                      
575                      GetWindowText(hwnd, szFileName, MAX_PATH);
576                      if(*szFileName != '[')
577                      {
578                          if(!SaveFile(GetDlgItem(hwnd, IDC_CHILD_EDIT), szFileName))
579                          {
580                              MessageBox(hwnd, "Couldn't Save File.", "Error.", MB_OK | MB_ICONEXCLAMATION);
581                              return 0;
582                          }
583                      }
584                      else
585                      {
586                          PostMessage(hwnd, WM_COMMAND,MAKEWPARAM(CM_FILE_SAVEAS, 0), 0);
587                      }
588                  }
589                  return 0;
590                  
591                  ///CM_ FILE_ SAVEAS
592                  case CM_FILE_SAVEAS:
593                  {
594                      char szFileName[MAX_PATH];
595                      
596                      if(DoFileOpenSave(hwnd, szFileName, TRUE))
597                      {
598                          if(!SaveFile(GetDlgItem(hwnd, IDC_CHILD_EDIT), szFileName))
599                          {
600                              MessageBox(hwnd, "Couldn't Save File.", "Error.", MB_OK | MB_ICONEXCLAMATION);
601                              return 0;
602                          }
603                          else
604                          { SetWindowText(hwnd, szFileName);}
605                      }
606                  }
607                  return 0;
608                  
609                  ///CM_ FILE_ UNDO
610                  case CM_EDIT_UNDO:
611                  {
612                      SendDlgItemMessage(hwnd, IDC_CHILD_EDIT, EM_UNDO, 0, 0);
613                  }
614                  break;
615                  
616                  ///CM_ FILE_ CUT
617                  case CM_EDIT_CUT:
618                  {
619                      SendDlgItemMessage(hwnd, IDC_CHILD_EDIT, WM_CUT, 0, 0);
620                  }
621                  break;
622                  
623                  ///CM_ FILE_ COPY
624                  case CM_EDIT_COPY:
625                  {
626                      SendDlgItemMessage(hwnd, IDC_CHILD_EDIT, WM_COPY, 0, 0);
627                  }
628                  break;
629                  
630                  ///CM_ FILE_ paste
631                  case CM_EDIT_PASTE:
632                  {
633                      SendDlgItemMessage(hwnd, IDC_CHILD_EDIT, WM_PASTE, 0, 0);
634                  }
635                  break;
636                  
637                  ///CM_ FILE_ FONTL
638                  case CM_FONT_FONTL:
639                  {
640                      u_hEdit = GetDlgItem(hwnd, IDC_CHILD_EDIT);
641                      fontscale++;
642                      ChangeFont(u_hEdit, fontscale);
643                  }
644                  break;
645                  
646                  ///CM_ FILE_ FONTS
647                  case CM_FONT_FONTS:
648                  {
649                      u_hEdit = GetDlgItem(hwnd, IDC_CHILD_EDIT);
650                      fontscale--;
651                      ChangeFont(u_hEdit, fontscale);
652                  }
653                  break;
654                  
655                  ///+
656              }//end Switch
657              
658              ///+
659          }//end Case
660          return 0;
661          
662          ///+
663      }//end Switch
664      
665      ///+
666      return DefMDIChildProc(hwnd, msg, wParam, lParam);
667  }// Call back end
668  

@ 下載程式碼 Download source code

沒有留言:

張貼留言