C++Builder 5 Developers Guide

   

Here's the chance to create a program with a purpose. It will let you load and display an image from disk.

Choose File, New Application from the main menu. C++Builder will create a new project and generate code to create an empty form. Now the VCL will help you create an application with only a tiny amount of code.

On the Component Palette, select the Additional tab, and then click the Image component. If you aren't sure, its icon has the sky, a hill, and water at the foot of the hill. If you hold your mouse pointer over each icon, a ToolTip will display to tell you what it is.

After selecting the Image component by clicking it, move your mouse to the form and click time to place the component on the form. You will not see much, but a square outline will appear. This component displays graphical images.

Under the Properties tab in the Object Inspector, go to the stretch attribute and select True.

From the Component Palette, go to the Dialogs tab and choose the OpenDialog component. If necessary, scroll through the Component Palette with the left and right arrows. The OpenDialog component looks like an open yellow folder. Select it with your left mouse button and place it anywhere in the top right corner of the form. The component now is part of your form and is used for displaying a dialog box in which you can choose files.

Now we want to set an attribute to this component. Go to the Object Inspector and look for the attribute named Filter , located under the Properties tab. Enter the following text into the edit box for that attribute:

BMP files*.bmp

From the Standard tab of the Component Palette, select two Button components . Instead of placing one Button component at a time, you can place multiple Button components by holding down the Shift key and pressing the Button icon from the Component Palette.

Click the form and a Button component appears. Click the form again and another Button component appears.

Click one time on the Button1 component on the form. This selects the button. Now you can change the properties of the button. In the Object Inspector, click inside the Caption property value (currently reading Button1 ) and replace the existing text with the words Get Picture . This will change the label on the button.

Go to the Win32 tab in the Component Palette and select the status bar. It looks like a gray bar with a grip. Hold your mouse over each component a couple of seconds to see the hint; it will read StatusBar . A status bar is often located at the bottom of most Windows applications to display the status of an application.

Place this on the form. You will see that the component automatically repositions to the bottom of the form.

Now double-click the Button component on the form (Object Inspector will display Button1 ) to set its OnClick event. Enter the code inside the braces:

void __fastcall TForm1::Button1Click(TObject *Sender) { if(OpenDialog1->Execute()) Image1->Picture->LoadFromFile(OpenDialog1->FileName); StatusBar1->SimpleText = OpenDialog1->FileName; }

The window into which you just entered your code is called the Source Code Editor.

Now go back to the form. You can use the Speedbar tool button that shows the hint Toggle Form/Unit. It looks like a form and a piece of paper with arrows pointing toward the form on both sides. If you hold your cursor over this button, it will display Toggle Form/Unit (F12). This tells you that another way to toggle between forms and code is with the F12 key. Press F12 now to set focus on the form.

Click one time on Button2 (Object Inspector will display Button2 ) on the form and set the caption to Close . Double-click that button and enter the code between the braces:

void __fastcall TForm1::Button2Click(TObject *Sender) { Close(); }

Now focus on your form again by moving the source code editor out of the way and clicking your form. Doesn't look like much, does it? Soon you will see how much C++Builder has done for you with just a little bit of code!

NOTE

Before running this application, it is a good idea to arrange your buttons and other components nicely to give a clean look. Use the white arrow selector on the left side of the Component Palette or the Object Tree View Window to select any component. You will not be able to move the StatusBar component at the bottom of the form, so leave it alone.

Press the green Run arrow or choose Run from the Project menu. C++Builder should compile the program successfully if you do not have any errors ( otherwise , you'll have to fix anything you typed wrong). Your program should appear with the form displaying the two buttons with the captions Get Picture and Close. Choose the Get Picture button.

An Open dialog box will appear asking for a file with the .BMP extension. Go to your Windows directory under your C: drive and select SETUP.BMP or another file that has a .BMP extension.

After selecting the file, press OK. You should see a picture of the Windows Setup bitmap. At the bottom of the form, the name of the file will be in the status bar. You have written your first application that really works, and with as little code as possible!

First you placed a T Image component on the form. This component enables you to display .BMP files within your program, requiring no programming on your part.

If you want to change the filter settings at design time, on Form1 click on the TOpenDialog component to select it. Then, press F11 to go to the Object Inspector. In the Filter property, double-click within the text area and a table will come up that lets you change the filter.

Now you can write a completely different program using different components. This will give you a second example of RAD technology within C++Builder.

NOTE

Go ahead and close the project. You do not have to save it. Choose File, Close All from the main menu and answer No.

However, if you choose to save the project, you will be prompted to save the main form ( Unit1.cpp ) and the project ( Project1.bpr ) in separate dialogs.

You might want to save the project and main form for each project together, using a separate directory for each project. This can be easily done using the standard Windows dialogs; simply navigate to where you want the new project's directory to be located; on the dialog toolbar click the new directory button and give the directory a name. Then double-click into the directory and save the main form, and then when the next dialog (for the project) appears, if it is not already positioned on the new directory, navigate there and save the project file.

From the main menu, select File, New Application. A new project will be created. Let's save this project by selecting File, Save Project As from the main menu.

Give the form's source code a name. Change the default Unit1.cpp to Mainform.cpp .

After saving the form's source code, the project source code will appear. By default, it is Project1.bpr ; name it Project2.bpr .

Place two ListBox components on the form. The list boxes are in the Standard tab in the Component Palette. Don't worry about placing them in a specified location, but do align them next to each other. C++Builder creates them as ListBox1 and ListBox2 .

Drop an EditBox and two Button s below the list boxes. They are also located in the Standard tab in the Component Palette. Align them any way you want. C++Builder creates the buttons as Button1 and Button2 . It also creates an Editbox named Edit1 .

Select Button1 . In the Object Inspector, select the Caption property. Change the caption to ADD .

Select Button2 . In the Object Inspector, select the Caption property. Change the caption to REMOVE .

Select Edit1 . In the Object Inspector, select the Text property. Remove the string within the Text property.

Drop a Label component under the Edit1 edit box. In the Object Inspector, select the Caption property. Enter Friends ' Names .

To select the form itself, click anywhere on the form, but not on any component. You can also do this inside the Object Inspector by selecting Form1 in the drop-down box. Next, select the Events tab in the Object Inspector and look for the OnShow event. Double-click inside the property setting for this event. C++Builder will now create the following code for the event handler:

void __fastcall TForm1::FormShow(TObject *Sender) { }

This event is triggered when the form is shown at runtime. This means that when you run this program, Windows will create the form and execute any code within the event. Type the code in the braces inside the event handler:

void __fastcall TForm1::FormShow(TObject *Sender) { ListBox1->Items->Add("David Sexton"); ListBox1->Items->Add("Randy Kelly"); ListBox1->Items->Add("John Kirksey"); ListBox1->Items->Add("Bob Martling"); }

Switch back to the form and double-click Button1 . This is the button with the Add caption. An OnClick event will be created. Type the code in the braces into the event handler:

void __fastcall TForm1::Button1Click(TObject *Sender) { String GetListItem = ListBox1->Items->Strings[ListBox1->ItemIndex]; ListBox2->Items->Add(GetListItem); }

Switch back to the form, and double-click Button2 . This is the button with the Remove caption. C++Builder will create an event handler. Type the code in the braces into the event handler:

void __fastcall TForm1::Button2Click(TObject *Sender) { ListBox2->Items->Delete(ListBox2->ItemIndex); }

Switch back to the form and select the Edit1 edit box. From the Object Inspector, choose the Events tab. Find the OnKeyPress event. Double-click in the empty area for this event, and C++Builder will create the event handler. Type the code in the braces into the event handler:

void __fastcall TForm1::Edit1KeyPress(TObject *Sender, char &Key) { if (Key==13) { ListBox2->Items->Add(Edit1->Text); ListBox1->Items->Add(Edit1->Text); } }

Now save the project. Remember to always save your work! Select File, Save All from the main menu. After saving the project, make the project. Press Ctrl+F9 or choose Project, Make. This will create the executable. If there are any errors, check for typos.

Press the green arrow to run it and see what happens. You can also choose Run from the Project menu as well. I will explain how the code works in a minute.

The application will appear as a regular window with two list boxes displaying names. There should also be two buttons on the form.

The edit box (which is our Edit1 component) will have the cursor in it ready for us to type. Enter your friends' names.

As soon as you press Enter, each name will be added not only to the first list box but also to the second list box.

Select one of the names and press the Add button. The name you selected will appear in the next list box beside it. If you press Remove, the name will disappear only from the second list box. Select the names and add them to the other list box; then remove the names.

You added an event handler for FormShow . This executes after the form's creation, but before it is shown. In that event handler you will see that there are some strings to be added inside ListBox1 . The strings are added to the Items property of the list box.

The next event is under Button1 . You created a string from the String class named GetListItem , which equals the item that was selected by the user . How did this event know that the item was selected? It didn't. It read the item's index. If there was no selection, it would be null. The next line adds the string from the index of ListBox1 .

Button2 's event is smaller than the first. It gets the index of the item inside ListBox2 and deletes it.

For our third event, you used an OnKeyPress for the Edit1 edit box. When someone enters data and presses a key, the event is triggered, executing the code inside it. This particular event scans for the Enter key, which is equal to 13 . You could also have used the VK_ENTER value that C++Builder defines as the Enter key. In any case, the if statement checks the passed parameter of Key to see if this is true. If it is, the code inside the if statement executes and adds the string within the edit box to both list boxes.

You created three event handlers, and the code is pretty small. You also put several components on the form without any code at all. Thus, you have a working program with minimal code.

This took just a few minutes of effort. After you get used to the Component Palette, the Object Inspector, and the IDE, you'll be able to do this with much larger and more complex programs. If you compare the time required to develop applications using other environments, such as Visual C++ or Microsoft Foundation Classes (MFC), you will see that C++Builder is far superior to the others.

Explore the menu items under C++Builder. The online help can also guide you through the menu items, Object Inspector, and some of the other important options within the IDE.


     
Top

Категории