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

C++視窗程式 輸入及輸出功能 Windows program with Input / output function


D:\cppCodes\HelloWinIO3\HelloWinIO3.c++
  1 //++++++++++++++++++++++++++
  2  //      HelloWinIO3.cpp
  3  //++++++++++++++++++++++++++
  4  
  5  #include "HelloWinIO3.h"//Win32 mode , auto include xxx.h
  6  ///#include header
  7  #include <windows.h>
  8  
  9  ///declare CALLBACK
 10  LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
 11  BOOL CALLBACK MyDlgProc(HWND, UINT, WPARAM, LPARAM);
 12  
 13  ///declare func/var
 14  TCHAR szClassName[] = TEXT("MyWinIO3"); //Windows type
 15  HINSTANCE hInst;  //Instance code
 16  HWND hDlg;  //Modeless dialog frame
 17  TCHAR szName[32]; //dialog entry string
 18  TCHAR szNameSum[256]=""; //dialog entry string
 19  
 20  ///WinMain
 21  int WINAPI WinMain(HINSTANCE hInstCurr, HINSTANCE hPrevInstance, LPSTR lpszCmdParam, int nCmdShow)
 22  {
 23      ///declare
 24      MSG msg;
 25      BOOL bRet;
 26      WNDCLASSEX WndClassEx;
 27  
 28      ///hInst
 29      hInst = hInstCurr; // assign globe instance
 30  
 31      ///Register WndProc
 32      WndClassEx.cbSize          = sizeof(WNDCLASSEX);
 33      WndClassEx.style           = CS_HREDRAW | CS_VREDRAW;
 34      WndClassEx.lpfnWndProc     = WndProc;
 35      WndClassEx.cbClsExtra      = 0;
 36      WndClassEx.cbWndExtra      = 0;
 37      WndClassEx.hInstance       = hInstCurr;//from WinMain
 38      WndClassEx.hIcon           = LoadIcon(NULL, IDI_APPLICATION);
 39      WndClassEx.hCursor         = LoadCursor(NULL, IDC_ARROW);
 40      WndClassEx.hbrBackground   =(HBRUSH)GetStockObject(WHITE_BRUSH); //(HBRUSH)(COLOR_3DSHADOW+1);
 41      WndClassEx.lpszMenuName    = "IOMENU"; //xxx.rc need define the same name
 42      WndClassEx.lpszClassName   = szClassName; // = WndProc, Will run 1st CreatWindowEx
 43      WndClassEx.hIconSm         = LoadIcon(NULL, IDI_APPLICATION);
 44  
 45      ///register OK?
 46      if(!RegisterClassEx(&WndClassEx))
 47      {
 48          MessageBox(0, "Could Not Register Window", "Oh Oh...", MB_ICONEXCLAMATION | MB_OK);
 49          return -1;
 50      }
 51  
 52      ///CreateWindowEx
 53      HWND hWndc;
 54      hWndc = CreateWindow(
 55      szClassName,
 56      TEXT("Simple Windows I/O"),//message on title
 57      WS_OVERLAPPEDWINDOW, //window type
 58      CW_USEDEFAULT, // X loc.
 59      CW_USEDEFAULT, // Y loc.
 60      CW_USEDEFAULT, // width
 61      CW_USEDEFAULT, // heigth
 62      NULL, //parent window code,It is parent window when setting as NULL
 63      NULL, //function window code,set NULL when using type function
 64      hInstCurr, //just was registered and is current instance
 65      NULL);
 66  
 67      ///create OK?
 68      if (!hWndc)
 69      {
 70          MessageBox(0, "Could Not Create Window", "Oh Oh...", MB_ICONEXCLAMATION | MB_OK);
 71          return -1;
 72      }
 73  
 74      ///Show Window
 75      ShowWindow(hWndc, nCmdShow);//nCmdShow from WinMain(HINSTANCE hInstCurr, HINSTANCE hPrevInstance, LPSTR lpszCmdParam, int nCmdShow)
 76  
 77      ///UpdateWindow
 78      UpdateWindow(hWndc);// send WM_PAINT message to WinProc to plot window internal area
 79  
 80      ///GetMessage to 0(WM_QUIT)
 81      while ((bRet = GetMessage(&msg, NULL, 0, 0)) != 0)// =0 mean WM_QUIT
 82      {
 83          ///GetMsg OK?
 84          if ( bRet == -1) { break; } // -1 mean abnormal, must abort the program
 85  
 86          ///send message
 87          if (!hDlg || !IsDialogMessage(hDlg, &msg))
 88          {
 89              TranslateMessage(&msg);
 90              DispatchMessage(&msg);
 91          }
 92  
 93          ///+
 94      }//end while
 95  
 96      ///program quit
 97      return (int)msg.wParam;
 98  
 99      ///main_end
100  }//end main
101  
102  ///WndProc
103  LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
104  {
105      ///declare struct
106      int id;
107      static HMENU hMenu;
108      TCHAR  szBuf[256];
109      HDC hdc;
110      PAINTSTRUCT ps;
111  
112      ///switch msg
113      switch(msg)
114      {
115          ///WM CREATE
116          case WM_CREATE:
117          {
118              hMenu = GetMenu(hWnd);
119              break;
120          }
121  
122          ///WM INITMENU
123          case WM_INITMENU:
124          {
125              ///Dlg on?
126              if (IsWindow(hDlg))
127              {
128                  ///Dlg masked
129                  EnableMenuItem(hMenu,IDM_DLG, MF_BYCOMMAND | MF_GRAYED);
130                  EnableMenuItem(hMenu, IDM_CLOSEDLG, MF_BYCOMMAND | MF_ENABLED);
131  
132                  ///+
133              }//end if
134  
135              ///else
136              else
137              {
138                  ///Dlg stand-by
139                  EnableMenuItem(hMenu,IDM_DLG, MF_BYCOMMAND | MF_ENABLED);
140                  EnableMenuItem(hMenu, IDM_CLOSEDLG, MF_BYCOMMAND | MF_GRAYED);
141  
142                  ///+
143              }//end else
144  
145              ///CM_WINDOW_ARRANGE
146              DrawMenuBar(hWnd);
147  
148              ///+
149              break;
150          }//end Case
151  
152          ///WM PAINT
153          case WM_PAINT:
154          {
155              //if (lstrcmp(szName, TEXT("")) == 0) lstrcpy(szBuf, TEXT("Not yet input name"));
156              //else wsprintf(szBuf, TEXT("your name is %s"), szName);
157              wsprintf(szBuf, "%s", szNameSum);
158              hdc = BeginPaint(hWnd, &ps);
159              TextOut(hdc, 10, 10, szBuf, lstrlen(szBuf));
160              EndPaint(hWnd, &ps);
161              break;
162          }
163  
164          ///WM COMMAND
165          case WM_COMMAND:
166          {
167              ///switch message
168              switch(LOWORD(wp))
169              {
170                  ///IDM END
171                  case IDM_END:
172                  SendMessage(hWnd, WM_CLOSE, 0, 0);
173                  break;
174  
175                  ///IDM DLG
176                  case IDM_DLG:
177                  hDlg = CreateDialog(hInst, TEXT("MYDLG"), hWnd, (DLGPROC)MyDlgProc);
178                  ShowWindow(hDlg, SW_NORMAL);
179                  break;
180  
181                  ///IDM CLSDLG
182                  case IDM_CLOSEDLG:
183                  DestroyWindow(hDlg);
184                  break;
185  
186                  ///+
187              }//end Switch
188  
189              ///+
190              break;
191          }//end Case
192  
193          ///WM CLOSE
194          case WM_CLOSE:
195          id = MessageBox(hWnd, TEXT("Sure to quit the program ? "), TEXT("Exit"), MB_YESNO | MB_ICONQUESTION);
196          if (id == IDYES)
197          {
198              if (IsWindow(hDlg))
199              {
200                  MessageBox(hWnd, TEXT("close input box ! "), TEXT(" Release !"), MB_OK);
201                  DestroyWindow(hDlg);
202              }
203              DestroyWindow(hWnd);
204          }
205          break;
206  
207          ///WM DESTROY
208          case WM_DESTROY:
209          PostQuitMessage(0);
210          break;
211  
212          ///default
213          default:
214          return (DefWindowProc(hWnd, msg, wp, lp));
215  
216          ///+
217      }//end Switch
218  
219      ///+
220      return 0;
221  }//end CALLBACK
222  
223  ///MyDlgProc
224  BOOL CALLBACK MyDlgProc(HWND hDlg, UINT msg, WPARAM wp, LPARAM lp)
225  {
226      ///declare
227      static HWND hParent;
228  
229      ///switch msg
230      switch(msg)
231      {
232          ///WM_ DIALOG INIT
233          case WM_INITDIALOG:
234          hParent = GetParent(hDlg);
235          return TRUE;
236  
237          ///WM_ CLOSE
238          case WM_CLOSE:
239          DestroyWindow(hDlg);
240          return TRUE;
241  
242          ///WM_ COMMAND
243          case WM_COMMAND:
244          //{
245  
246          ///switch (LOWORD(wp)
247          switch (LOWORD(wp))
248          {
249              ///IDOK
250              case IDOK:
251              GetDlgItemText(hDlg, IDC_EDIT1, szName, (int)sizeof(szName) - 1);
252              wsprintf(szNameSum, "%s%s",szNameSum, szName);
253              SetDlgItemText(hDlg, IDC_EDIT1, TEXT(""));
254              InvalidateRect(hParent, NULL, TRUE);//update parent window's client area that must be redrawn
255              return TRUE;
256  
257              ///ID CANCEL
258              case IDCANCEL:
259              SetDlgItemText(hDlg, IDC_EDIT1, TEXT(""));
260              return TRUE;
261  
262              ///IDC_ CLOSE
263              case IDC_CLOSE:
264              DestroyWindow(hDlg);
265              return TRUE;
266  
267              ///default Bypass
268              default:
269              return FALSE;
270  
271              ///+
272          }//end Switch
273  
274          ///+
275          //}//end Case
276  
277          ///default Bypass
278          default:
279          //return (DefWindowProc(hDlg, Msg, wParam, lParam));//leave to system
280          return FALSE;
281  
282          ///+
283      }//end Switch
284  
285      ///+
286  }//end DlgProc
287  
288  
289  
290  //++++++++++++++++++++++++++
291  //      HelloWinIO3.h
292  //++++++++++++++++++++++++++
293  
294  
295  #define IDM_END                         102
296  #define IDM_DLG                         103
297  #define IDM_CLOSEDLG                    104
298  #define IDC_CLOSE                       1001
299  #define IDC_EDIT1                       1002
300  
301  
302  
303  //++++++++++++++++++++++++++
304  //      HelloWinIO3.rc
305  //++++++++++++++++++++++++++
306  
307  
308  #include "HelloWinIO3.h"
309  #include "afxres.h"
310  
311  IOMENU MENU
312  BEGIN
313      POPUP "program"
314      BEGIN
315          MENUITEM "close",                    IDM_END
316      END
317      POPUP "select"
318      BEGIN
319          MENUITEM "open Input Box",        IDM_DLG
320          MENUITEM "close Input Box",        IDM_CLOSEDLG
321      END
322  END
323  
324  
325  MYDLG DIALOGEX 110, 110, 200, 110
326  STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
327  CAPTION "Input Box"
328  FONT 10, "MS Shell Dlg", 400, 0, 0x1
329  BEGIN
330      DEFPUSHBUTTON   "OK",IDOK,10,70,50,14
331      PUSHBUTTON      "cancel",IDCANCEL,70,70,50,14
332      PUSHBUTTON      "close",IDC_CLOSE,130,70,50,14
333      LTEXT           "key in data",IDC_STATIC,10,15,40,15
334      EDITTEXT        IDC_EDIT1,50,15,138,14//,ES_AUTOHSCROLL
335  END
336  
337  
338  
339  

@ 下載程式碼 Download source code

沒有留言:

張貼留言