HomeC&C++WainTutorialsSamplesTip&TrickTools


Now the application will be turned into a calculator. It can add two numbers and nothing else. The main purpose is to show how to create Edit controls and how to work with intergers in these.
First we will create two edit boxes in which the user can enter the numbers:
      CreateWindowEx(WS_EX_CLIENTEDGE,
                     "EDIT",
                     "", // Initial Text
                     WS_CHILD | WS_VISIBLE | ES_NUMBER |
                     WS_TABSTOP | WS_GROUP, // Style
                     5, 5, 120, 20, // position
                     hwndDlg, // Owner
                     (HMENU)(FirstEditId), // ID
                     InstanceHandle,  // The application
                     0);
      CreateWindowEx(WS_EX_CLIENTEDGE,
                     "EDIT",
                     "", // Initial Text
                     WS_CHILD | WS_VISIBLE | ES_NUMBER |
                     WS_TABSTOP,  // Style
                     5, 35, 120, 20, // position
                     hwndDlg, // Owner
                     (HMENU)(SecondEditId), // ID
                     InstanceHandle,  // The application
                     0);
This will create two identical edit boxes above each other. We use CreateWindowEx to create them as we want to use the WS_EX_CLIENTEDGE style to give them the rigth 3D look. The ES_NUMBER style ensures that the user can only enter numbers. WS_TABSTOP and WS_GROUP is used to enable the user to use TAB to switch between controls, WS_GROUP is only to be used in the first.
Then we create the edit control in which the result is shown:
      CreateWindowEx(WS_EX_CLIENTEDGE,
                     "EDIT",
                     "0", // Initial Text
                     WS_CHILD | WS_VISIBLE | ES_NUMBER |
                     ES_READONLY,  // Style
                     5, 65, 120, 20, // position
                     hwndDlg, // Owner
                     (HMENU)(ResultEditId), // ID
                     InstanceHandle,  // The application
                     0);
Note the use of ES_READONLY, this makes the edit box dimmed.
Now we must create a button for the user to press when he want to add the two numbers:
      CreateWindow("BUTTON",
                   "Add",                        // Button Text
                   WS_CHILD | WS_VISIBLE | WS_TABSTOP,  // Style
                   5, 90, 120, 30, // position
                   hwndDlg, // Owner
                   (HMENU)(ButtonId), // ID
                   InstanceHandle,  // The application
                   0);
Then we must handle the event received when the user press the button:
   case WM_COMMAND:
      if(HIWORD(wParam) == BN_CLICKED &&
         LOWORD(wParam) == ButtonId)
      {
         int First = GetDlgItemInt(hwndDlg,
                                   FirstEditId,
                                   0,
                                   FALSE);
         int Second = GetDlgItemInt(hwndDlg,
                                    SecondEditId,
                                    0,
                                    FALSE);
         int Result = First + Second;
         SetDlgItemInt(hwndDlg, ResultEditId, Result, FALSE);
      }
      break;
We simply use GetDlgItemInt to read the number and SetDlgItemInt to show the result.
The complete code for the application can be found here.