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

視窗版 "Hello World !" first C++ program for Windows version "Hello Windows World !"


D:\cppCodes\HelloWindows\HelloWindows.c++
  1 //++++++++++++++++++++++++++
  2  //      HelloWindows.cpp
  3  //++++++++++++++++++++++++++
  4  
  5  
  6  ///#include headers
  7  #include <windows.h>
  8  
  9  ///declare CALLBACK
 10  LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
 11  
 12  ///declare func/var
 13  TCHAR szClassName[] = TEXT("hellowindows");//for RegisterClassEx then CreateWindow
 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  
 23      ///Register WndProc
 24      WndClassEx.cbSize          = sizeof(WNDCLASSEX);
 25      WndClassEx.style           = CS_HREDRAW | CS_VREDRAW;
 26      WndClassEx.lpfnWndProc     = WndProc;
 27      WndClassEx.cbClsExtra      = 0;
 28      WndClassEx.cbWndExtra      = 0;
 29      WndClassEx.hInstance       = hInstCurr;//from WinMain
 30      WndClassEx.hIcon           = LoadIcon(NULL, IDI_APPLICATION);
 31      WndClassEx.hCursor         = LoadCursor(NULL, IDC_ARROW);
 32      WndClassEx.hbrBackground   =(HBRUSH)GetStockObject(WHITE_BRUSH); //(HBRUSH)(COLOR_3DSHADOW+1);
 33      WndClassEx.lpszMenuName    =NULL; // "MYMENU"; //xxx.rc need define the same name
 34      WndClassEx.lpszClassName   = szClassName; // = WndProc, Will run 1st CreatWindowEx
 35      WndClassEx.hIconSm         = LoadIcon(NULL, IDI_APPLICATION);
 36  
 37      ///register OK?
 38      if(!RegisterClassEx(&WndClassEx))
 39      {
 40          MessageBox(0, "Could Not Register Window", "Oh Oh...", MB_ICONEXCLAMATION | MB_OK);
 41          return -1;
 42      }
 43  
 44      ///Create Window
 45      HWND hWndc;
 46      hWndc = CreateWindow(
 47      szClassName,// registered class name
 48      TEXT("First Windows Program"),//message on title
 49      WS_OVERLAPPEDWINDOW, //window type
 50      CW_USEDEFAULT, // X loc.
 51      CW_USEDEFAULT, // Y loc.
 52      CW_USEDEFAULT, // width
 53      CW_USEDEFAULT, // heigth
 54      NULL, //parent window code,It is parent window when setting as NULL
 55      NULL, //function window code,set NULL when using type function
 56      hInstCurr, //just was registered and is current instance
 57      NULL);
 58  
 59      ///create OK?
 60      if (!hWndc)
 61      {
 62          MessageBox(0, "Could Not Create Window", "Oh Oh...", MB_ICONEXCLAMATION | MB_OK);
 63          return -1;
 64      }
 65  
 66      ///Show Window
 67      ShowWindow(hWndc, nCmdShow);//nCmdShow from WinMain(HINSTANCE hInstCurr, HINSTANCE hPrevInstance, LPSTR lpszCmdParam, int nCmdShow)
 68  
 69      ///Update Window
 70      UpdateWindow(hWndc);// send WM_PAINT message to WinProc to plot window internal area
 71  
 72      ///GetMessage to 0(WM_QUIT)
 73      while ((bRet = GetMessage(&msg, NULL, 0, 0)) != 0)// =0 mean WM_QUIT
 74      {
 75          ///GetMsg OK?
 76          if ( bRet == -1) { break; } // -1 mean abnormal, must abort the program
 77  
 78          ///send message
 79          TranslateMessage(&msg);// convert msg to chacter information
 80          DispatchMessage(&msg);// send chacter information to WndProc
 81  
 82          ///+
 83      }//end while
 84  
 85      ///program quit
 86      return (int)msg.wParam;
 87  
 88      ///main_end
 89  }//end main
 90  
 91  ///WndProc
 92  LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
 93  {
 94      ///declare struct
 95      PAINTSTRUCT ps;
 96      HDC hdc;// Handle of Device Context which is a data struct exist in GDI(Graphic Devices Interface)
 97  
 98      ///switch msg
 99      switch(msg)
100      {
101          ///WM PAINT (15)
102          case WM_PAINT://wondow message 15 is defined in winuser.h
103          {
104              ///BeginPaint
105              hdc = BeginPaint(hWnd, &ps);// pass hdc into GDI to run painting
106  
107              ///Windows Display
108              //LPCTSTR lpszName = TEXT("Hello Windows world !");
109              LPCTSTR lpszName = "Hello Windows world !";
110              //char caName[] = "Hello Windows world !";
111              TextOut(hdc, 0, 0, lpszName, lstrlen(lpszName));
112              TextOut(hdc, 0, 20, lpszName, lstrlen(lpszName));
113              TextOut(hdc, 0, 40, lpszName, lstrlen(lpszName));
114  
115              ///EndPaint
116              EndPaint(hWnd, &ps);
117  
118              ///+
119              break;
120          }//end WM_PAINT
121  
122          ///WM DESTROY
123          case WM_DESTROY:// =2
124          PostQuitMessage(0);
125          break;
126  
127          ///default
128          default:
129          return (DefWindowProc(hWnd, msg, wp, lp));//leave to system
130  
131          ///+
132      }//end Switch
133  
134      ///+
135      return 0;// with msg procedure
136  }//end CALLBACK
137  

@ 下載程式碼 Download source code

沒有留言:

張貼留言