Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ programming in Windows environment

Similar presentations


Presentation on theme: "C++ programming in Windows environment"— Presentation transcript:

1 C++ programming in Windows environment
Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 34./0. C++ programming in Windows environment Visual components, menus, controls Exposition of task Completing the menu Writing program code for Exit menu item Creating controls Writing further event handlers

2 Department of Information Engineering INFORMATION TECHNOLOGY dr
Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 34./1. Task: Let us make a C++ Builder program for conversion of a byte given in binary form to decimal and back. The program has a help in English and Hungarian that gives the minimal information needed for usage. A previous idea of program window is the next:

3 1. Place a MainMenu icon on the form!
Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 34./2. Completing the menu 1. Place a MainMenu icon on the form! 2. Double click on the component to display the menu editor: 3. Give the name and caption for the selected main menu item in the Object Inspector: 4. Give the name and caption for the submenu items of Selection main menu item:

4 Department of Information Engineering INFORMATION TECHNOLOGY dr
Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 34./3. 5. Select the second item of main menu and name it to Help in the Object Inspector: 6. To create the submenu in Help select the empty submenu item and name it to Description in the Object Inspector:

5 9. Having accomplished these steps the menu is ready.
Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 34./4. 7. For creation of submenu of Description mark the Description and push the right mouse button! A popup menu appears, choose the Create Submenu item: 8. After selecting the originating submenu item give English name to it in the Object Inspector, and Hungarian to the empty menu item below it! 9. Having accomplished these steps the menu is ready.

6 Writing program code for Exit menu item
Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 34./5. Writing program code for Exit menu item 10. Double click on the Exit menu item of the menu editor! The code editor comes to the top and the cursor flashes in the newly created event handler function skeleton of the Exit-click. 11. Write the code of method call for exiting: The program can be run and the Exit menu item can be tried. 12. Place on the form the necessary other components: one radio group, eight check boxes, seven buttons and two labels for displaying the result!  

7 13. The raw form of the user interface is the next:
Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 34./6. Creating controls 13. The raw form of the user interface is the next: 14. For creation of two radio buttons in the radio group select the radio group component then choose the Items property in the Object Inspector. A double click on it will bring up the String List editor window. Give the caption strings for the two radio buttons:

8 15. Give the Caption of radio group: Direction of conversion
Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 34./7. 15. Give the Caption of radio group: Direction of conversion 16. Give the captions for check boxes, buttons and labels: 17. Choose a bigger font size for displaying the decimal value (Label1->Font and Label2->Font = 10, Bold ):

9 int decimal = 0; Writing further event handlers
Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 34./8. Writing further event handlers 18. Create a variable for storing the decimal value and set it to zero! For this insert the next line into the Unit1.cpp file after the TForm1 * Form1; line: int decimal = 0; 19. Write the program code for decreasing and increasing buttons! For these double click on, e.g. Button1 and decrease the value of decimal with the value represented by the button! Avoid decreasing under zero! Similarly avoid increasing above 255! Display the changed value as the new value of Label E.g.: the appropriate event handler function for Button1: void __fastcall TForm1::Button1Click(TObject *Sender) { decimal -= 1; if (decimal < 0) decimal = 0; Label2->Caption = decimal; } Compile and run the program! The decreasing and increasing work!

10 void __fastcall TForm1::Button7Click(TObject *Sender) {
Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 34./9. 20. We have arrived at the most interesting part: The event handler of Conversion button has to be accomplished. This button will perform the conversion from binary to decimal, or in opposite direction, depending on the status of radio buttons. The binary input value is given by the status of check boxes. Every check box corresponds to one of the bits of a byte, if it is checked then the value of the bit is 1, else 0. The solution: void __fastcall TForm1::Button7Click(TObject *Sender) { if (RadioGroup1->ItemIndex==0) //Bin-> Dec {decimal = 0; if (CheckBox1->Checked) decimal += 1; if (CheckBox2->Checked) decimal += 2; if (CheckBox3->Checked) decimal += 4; if (CheckBox4->Checked) decimal += 8; if (CheckBox5->Checked) decimal += 16; if (CheckBox6->Checked) decimal += 32; if (CheckBox7->Checked) decimal += 64; if (CheckBox8->Checked) decimal += 128; Label2->Caption= decimal; } //continued

11 if (buff >= 128) {CheckBox8->Checked=true; buff -= 128;}
Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 34./10. else //Dec -> Bin {int buff= decimal; if (buff >= 128) {CheckBox8->Checked=true; buff -= 128;} else CheckBox8->Checked = false; if (buff >= 64) {CheckBox7->Checked=true; buff -= 64;} else CheckBox7->Checked = false; if (buff >= 32) {CheckBox6->Checked=true; buff -= 32;} else CheckBox6->Checked = false; if (buff >= 16) {CheckBox5->Checked=true; buff -= 16;} else CheckBox5->Checked = false; if (buff >= 8) {CheckBox4->Checked=true; buff -= 8;} else CheckBox4->Checked = false; if (buff >= 4) {CheckBox3->Checked=true; buff -= 4;} else CheckBox3->Checked = false; if (buff >= 2) {CheckBox2->Checked=true; buff -= 2;} else CheckBox2->Checked = false; if (buff >= 1) {CheckBox1->Checked=true; buff -= 1;} else CheckBox1->Checked = false; }

12 __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 34./11. 21. The program is not yet perfect because there is not any radio button selected! Do select one in the constructor of the Form! __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { RadioGroup1->ItemIndex = 0; } 22. The Calculation does not work from the menu items. Selecting this menu point accomplishing the same task is required than is made with the Conversion button. How can be this achieved? There is a possibility to cut out the body of the Conversion event handler and insert into an individual function and then the Calculation menu item and the Conversion button could call this function But this is unnecessary because the Calculation can call the event handler of the Conversion as shown in the next: void __fastcall TForm1::CalculationClick(TObject *Sender) { Button7Click(Application);

13 void __fastcall TForm1::EnglishClick(TObject *Sender) {
Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 34./12. 23. The event handlers of Help submenu items are missing yet. The English and Hungarian description of the work of program can be shown in two similar message windows: void __fastcall TForm1::EnglishClick(TObject *Sender) { ShowMessage("The program makes conversions between binary\ and decimal numbers. Check Boxes represent one of the bits\ of a byte. The Conversion can be started choosing the\ Selection/Calculation menu item or clicking on the Conversion button."); } The giving of Hungarian help can be made likewise.

14 having automatically generated names can be renamed to more pleasant
Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 34./13. 24. At last it may be carried out some refinement actions: The window of the application may have own title overwriting the Form1->Caption property. The icon of Borland can be changed to an icon designed by us. The components having automatically generated names can be renamed to more pleasant names, and other refinements can be made, e.g. it can be checked if the program can work without mouse, with keyboard only It could be experienced that it can.


Download ppt "C++ programming in Windows environment"

Similar presentations


Ads by Google