12.12.2012 Views

Teach Yourself Borland C++ in 14 Days - portal

Teach Yourself Borland C++ in 14 Days - portal

Teach Yourself Borland C++ in 14 Days - portal

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

The <strong>C++</strong>Builder IDE Explored: Projects and Forms<br />

Step 2: Write Code for the File|Open and<br />

File|Save As Menu Items<br />

So far, so good. Now let’s write the code to implement the File | Open and File | Save As<br />

menu items. <strong>C++</strong>Builder provides a slick way of writ<strong>in</strong>g menu handlers with a m<strong>in</strong>imum<br />

amount of fuss. Keep <strong>in</strong> m<strong>in</strong>d that we haven’t created the MDI child form yet, but we know<br />

enough about it to write the code for the menu handlers. Here goes:<br />

1. On the ma<strong>in</strong> form, choose File | Open from the menu. An event handler is created<br />

for that menu item, and the Code Editor is displayed.<br />

2. Type code so that the event handler looks like this:<br />

NOTE<br />

void __fastcall TMa<strong>in</strong>Form::Open1Click(TObject *Sender)<br />

{<br />

if (OpenDialog->Execute())<br />

{<br />

TChild* child = new TChild(this);<br />

child->SetParent(this);<br />

child->Image->Picture->LoadFromFile(OpenDialog->FileName);<br />

child->ClientWidth = child->Image->Picture->Width;<br />

child->ClientHeight = child->Image->Picture->Height;<br />

child->Caption = OpenDialog->FileName;<br />

child->Show();<br />

}<br />

}<br />

This code first executes the File Open dialog box and gets a filename. If the Cancel<br />

button on the File Open dialog box is clicked, the function returns without do<strong>in</strong>g<br />

anyth<strong>in</strong>g further. If the OK button on the File Open dialog box is clicked, a new<br />

TChild object is created (TChild will be the name of the MDI child class we’re<br />

go<strong>in</strong>g to create later). The image file is loaded <strong>in</strong>to the Image component on the<br />

child form; then the MDI child’s client area is sized to match the size of the image.<br />

F<strong>in</strong>ally, the Caption property is set to the filename selected and the child w<strong>in</strong>dow is<br />

displayed.<br />

Remember our earlier discussion about call<strong>in</strong>g delete for all objects<br />

created with new? Notice that I appear to be violat<strong>in</strong>g that rule <strong>in</strong> the<br />

preced<strong>in</strong>g code. In reality I am not, because VCL will take the responsibility<br />

of free<strong>in</strong>g the memory allocated for the MDI child w<strong>in</strong>dows.<br />

Notice that the s<strong>in</strong>gle parameter <strong>in</strong> the TChild constructor is this. That<br />

tells VCL that the Owner of the MDI child is the MDI form w<strong>in</strong>dow.<br />

When the MDI form is destroyed (when the application closes), it will<br />

be sure to delete all of its MDI child objects.<br />

207<br />

6

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!