community.borland.com

Article #25648: Peep Show Picture Viewer

Problem:
Creating a graphics viewing application for viewing bitmap, icon, and metafiles (*.bmp, *.ico, *.mtf).

Solution:
The information in this article applies to:

Creating the Main Form

Step 1: Select File|New Application from the File Menu.

Step 2: For the main form, change the Name property to 'MainForm'.

Step 3: Change the Caption property to 'Peep Show'.

Step 4: Change the Height to 450 and the Width to 575 (or other suitable values for you display resolution).

Step 5: Change the FormStyle to fsMDIForm.

Adding a Menu

Step 1: Click the Standard tab of the Component palette and click the MainMenu button.

Step 2: Click on the form to place a MainMenu component on the form. (position unimportant  doesn't 
              show at runtime)

Step 3: Change the Name property to MainMenu.

Step 4: Double-click the MainMenu component to display the Menu Desinger.

Step 5: Place your cursor over the Menu Designer and click you right mouse button. Choose Insert 
              from Template from the context menu. The Insert Template dialog box appears. 

Step 6: Chosse MDI Frame Menu and click OK.  The menu is displayed in the Menu Designer.

Step 7: Click the system close box on the Menu Designer to close it.

Configuring the File|Open and File|Save Dialog Boxes

Step 1: Click the Dialogs tab on the Component palette. Choose an OpenPictureDialog component 
              and place it on the form. The OpenPictureDialog component's icon can be placed anywhere 
              on the form.

Step 2: Change the Name property of the Open dialog box to OpenPictureDialog.

Step 3: Change the title property to Open a Picture for Viewing.

Step 4: Add a SavePictureDialog component.

Step 5: Change the Name property of the component to SavePictureDialog and the Title property 
              to Save a Picture.

Coding the File|Open and File|Save As Menu Items

Step 1: On the main form, choose File|Open from the menu. An event handler is created for that 
              menu item and the Code Editor is  displayed.

Step 2: Type code so that the event handler looks like this:
                 
             procedure TmainForm.Open1Click(Sender: Tobject);
             var
                 Child : TChild;	
             begin
                  If OpenPictureDialog.Execute then begin
	  Child := Tchild.Create(Self);
	  with Child.Image.Picture do begin
	     LoadFromFile(OpenPicturedialog.FileName);
	     Child.Clientwidth := Width;
                       Child.ClientHeight := Height;
	  end;
	  Child.Caption := ExtractFileName(OpenPictureDialog.FileName);
	  Child.Show;
               end;
            end;

Step 3: Press F12 to switch back to the form. Now choose File|Save As from the menu. The File|Save 
              As event handler is displayed.

Step 4: Type code so that the File|Save As event handler looks like this:
            
            procedure TmainForm>SaveAs1Click(Sender: Tobject);
            begin
                if SavePictureDialog>Execute then
	 with ActiveMDIChild as Tchild do
	    Image.Picture.SaveToFile(SavePictureDialog.FileName);
            end;	

(Choose File|Save All to save project)

Coding the Window Menu

Step 1: Switch back to the form by pressing F12. Choose Window|Tile from the form's menu.

Step 2: You need to enter only a single line of code for the  event handler: Tile;

Step 3: Switch back to the form and repeat the process for Window|Cascade: Cascade;

Step 4: Repeat the steps for the Window|Arrange All: ArrangeIcons;

Creating the MDI Child Form

Step 1: Create a new form using the toolbar button or the File menu.

Step 2: Change the Name property to Child. The Caption property can be ignored because you will 
              be setting the dialog box's caption at runtime.

Step 3: Change the FormStyle property to fsMDIChild. This is necessary for the form to be treated 
              as an MDI child window. 

Step 4: Click the Additional tab on the Component palette. Click the Image button and place an 
              Image  component anywhere on the form.

Step 5: Change the Name property to Image.

Step 6: Change the Stretch property to True.

Step 7: Change the Align property to alClient.  The Image component expands to fill the client 
              area of the form.

Step 8: Choose File|Save and save the form's unit as MDIChild.

Step 9: Switch to the Code Editor. Click the PctViewU tab. Now choose File|Use Unit from the 
              main menu, select the MDIChild unit, and click OK. 

Finishing Touches

To prevent the child window from being created at start up do the following:

Step 1: Choose Project|Options from the main menu. 

Step 2: If necessary, click the Forms tab. The list of forms to auto-create is displayed. 

Step 3: Click the child from and then click the > button. This removes the child from from the 
              auto-create list and puts it in the Available froms list. 

To make the close button actually close the window instead of minimizing it (the MS default) 
do the following:

Step 1: Bring up the child window from int the Form Designer. Be sure the form itself is selected 
              and not the Image component on the form (choose the Child from the Component Selector 
              at the top of the object Inspector, if necessary).

Step 2: Double-click the Value column next to the OnClose event in the Object Inspector. Add a 
              line of code to the event havndler so that it looks like this:
procedure Tchild.FormClose(Sender: Tobject; var Action: TCloseAction);
begin
  Action := caFree;
end;

The End!



       	



Last Modified: 24-OCT-00