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

極簡風C++視窗程式 輸入及輸出功能 use DialogBox to build the ultra compact, extreme concise Windows program with Input / output function


D:\cppCodes\HelloWinIO2\HelloWinIO2.c++
  1 //++++++++++++++++++++++++++
  2  //      HelloWinIO2.cpp
  3  //++++++++++++++++++++++++++
  4  
  5  #include "HelloWinIO2.h"//Win32 mode , auto include xxx.h
  6  ///#include headers
  7  #include <windows.h>
  8  
  9  ///declare CALLBACK
 10  BOOL MainDlgProc( HWND , UINT , WPARAM , LPARAM );
 11  
 12  ///declare func/var
 13  TCHAR szName[32]; //dialog entry string
 14  
 15  ///WinMain
 16  int WINAPI WinMain(HINSTANCE hInstCurr, HINSTANCE hPrevInstance, LPSTR lpszCmdParam, int nCmdShow)
 17  {
 18      ///declare
 19      //MSG msg;
 20      BOOL bRet;
 21      //WNDCLASSEX WndClassEx;
 22      MSG msg;
 23      
 24      ///Create DialogBox
 25      HWND hDlg = CreateDialog( hInstCurr, TEXT("MYDLG"), 0, (DLGPROC)MainDlgProc );
 26      
 27      ///create OK?
 28      if (!hDlg)
 29      {
 30          MessageBox(0, "Could Not Create Window", "Oh Oh...", MB_ICONEXCLAMATION | MB_OK);
 31          return -1;
 32      }
 33      
 34      ///Show Window
 35      //ShowWindow(hWndc, nCmdShow);//nCmdShow from WinMain(HINSTANCE hInstCurr, HINSTANCE hPrevInstance, LPSTR lpszCmdParam, int nCmdShow)
 36      ShowWindow( hDlg, nCmdShow );
 37      
 38      ///GetMessage to 0(WM_QUIT)
 39      while ((bRet = GetMessage(&msg, NULL, 0, 0)) != 0)// =0 mean WM_QUIT
 40      {
 41          ///GetMsg OK?
 42          if ( bRet == -1) { break; } // -1 mean abnormal, must abort the program
 43          
 44          ///send message
 45          //if (!IsWindow(hDlg)  || !IsDialogMessage(hDlg, &msg))// enable dialogbox table
 46          if (!hDlg || !IsDialogMessage(hDlg, &msg))// enable dialogbox table
 47          {
 48              TranslateMessage(&msg);// convert msg to chacter information
 49              DispatchMessage(&msg);// send chacter information to WndProc
 50          }
 51          
 52          ///+
 53      }//end while
 54      
 55      ///program quit
 56      return (int)msg.wParam;
 57      
 58      ///main_end
 59  }//end main
 60  
 61  ///DlgProc
 62  BOOL MainDlgProc( HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam )
 63  {
 64      ///switch msg
 65      switch(Msg)
 66      {
 67          ///WM_ Close
 68          case WM_CLOSE:
 69          DestroyWindow(hDlg);// send WM_DESTROY and next cycle msg will switch to WM_DESTROY and PostQuitMessage(0);
 70          
 71          return TRUE;
 72          
 73          ///WM_ DESTROY
 74          case WM_DESTROY:
 75          {
 76              PostQuitMessage(0);
 77          }
 78          return TRUE;
 79          
 80          ///WM_ COMMAND
 81          case WM_COMMAND:
 82          {
 83              ///switch LOWORD
 84              switch (LOWORD(wParam))
 85              {
 86                  ///IDOK
 87                  case IDOK:
 88                  GetDlgItemText(hDlg, IDC_EDIT1, szName, (int)sizeof(szName) - 1);
 89                  SetDlgItemText(hDlg, IDC_EDIT2, szName);
 90                  return TRUE;
 91                  
 92                  ///ID CANCEL
 93                  case IDCANCEL:
 94                  SetDlgItemText(hDlg, IDC_EDIT1, TEXT(""));
 95                  return TRUE;
 96                  
 97                  ///IDC_ CLOSE
 98                  case IDC_CLOSE:
 99                  DestroyWindow(hDlg);
100                  return TRUE;
101                  
102                  ///default Bypass
103                  default:
104                  return FALSE;
105                  
106                  ///+
107              }//end Switch
108              
109              ///+
110          }//end Case
111          
112          ///default Bypass
113          default:
114          //return (DefWindowProc(hDlg, Msg, wParam, lParam));//leave to system
115          return FALSE;
116          
117          ///+
118      }//end Switch
119      
120      ///+
121  }//end DlgProc
122  
123  
124  
125  //++++++++++++++++++++++++++
126  //      HelloWinIO2.h
127  //++++++++++++++++++++++++++
128  
129  #define IDC_CLOSE                       1001
130  #define IDC_EDIT1                       1002
131  #define IDC_EDIT2                       1003
132  
133  
134  
135  
136  //++++++++++++++++++++++++++
137  //      HelloWinIO2.rc
138  //++++++++++++++++++++++++++
139  
140  #include "HelloWinIO2.h"
141  #include "afxres.h"
142  
143  
144  MYDLG DIALOGEX 110, 110, 200, 110
145  STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU //|WS_TABSTOP//|DS_CONTROL
146  CAPTION "Input/Output box"
147  FONT 10, "MS Shell Dlg", 400, 0, 0x1
148  BEGIN
149      DEFPUSHBUTTON   "OK",IDOK,10,70,50,14
150      PUSHBUTTON      "cancel",IDCANCEL,70,70,50,14
151      PUSHBUTTON      "close",IDC_CLOSE,130,70,50,14
152  
153      LTEXT           "Input : ",IDC_STATIC,10,15,40,15
154      EDITTEXT        IDC_EDIT1,45,15,138,14,ES_AUTOHSCROLL
155  
156      LTEXT           "Output : ",IDC_STATIC,10,40,40,15
157      EDITTEXT        IDC_EDIT2,45,40,138,14,WS_DISABLED
158  
159  END
160  

@ 下載程式碼 Download source code

沒有留言:

張貼留言