The TPageControl Component

Now, let's use the TPageControl (Win32 category) component to enhance the drag and drop example from the previous section and allow the user to preview plain text and rich text files and images.

To do that, we have to do the following:

  1. Remove the TListBox component that was used in the previous section.

  2. Add a TPageControl to the Designer Surface (and rename it PageControl).

  3. Add the ExtCtrls unit to the uses list because we have to use the TImage component.

  4. Create the OpenDroppedFile procedure that will load plain and rich text files and images and display them as pages in the TPageControl component.

The first thing we have to do in the OpenDroppedFile procedure is determine the file type, because we have to instantiate a different component for every file type: TMemo for plain text files, TRichEdit for rich text files, and TImage for images.

procedure TForm1.OpenDroppedFile(const AFileName: string); var   Extension: string;   NewTab: TTabSheet;   FileClass: TControlClass;   NewControl: TControl; begin   Extension := LowerCase(ExtractFileExt(AFileName));   { check the extension }   if Extension = '.txt' then     FileClass := TMemo   else if Extension = '.rtf' then     FileClass := TRichEdit   else if Extension = '.bmp' then     FileClass := TImage   else     Exit; { exit if an unsupported file is dropped } 

After we've determined the file type, we have to create the page that will display the contents of the file. To create a new page, we have to create an instance of the TTabSheet class and then add it to the TPageControl by assigning the TPageControl component to the PageControl property of the TTabSheet.

{ create the new tab } NewTab := TTabSheet.Create(PageControl); NewTab.PageControl := PageControl; NewTab.Caption := ExtractFileName(AFileName);

Now, we have to instantiate the component necessary to load the dropped file. To display a component in a TPageControl page, make the TTabSheet component its Parent.

{ create the required control } NewControl := FileClass.Create(Self); NewControl.Parent := NewTab; NewControl.Align := alClient;

Finally, we have to open the file and activate the new page to display the freshly opened file. Because both TMemo and TRichEdit components descend from the TCustomMemo component, we can use polymorphism here to load both plain text and rich text files in a single line.

  { open the file }   if FileClass = TImage then     TImage(NewControl).Picture.LoadFromFile(AFileName)   else     TCustomMemo(NewControl).Lines.LoadFromFile(AFileName);   { activate the new page }   PageControl.ActivePage :=     PageControl.Pages[Pred(PageControl.PageCount)]; end; 

Now that the OpenDroppedFile procedure is finished, replace the Items.Add call with the OpenDroppedFile procedure call in the WM_DROPFILES message handler:

for i := 0 to Pred(dropCount) do begin   DragQueryFile(Message.Drop, i, nameBuffer, SizeOf(nameBuffer));   OpenDroppedFile(string(nameBuffer)); end;      // for

One last thing that we should enable the user to do is to close all opened files by selecting File ® Close All:

procedure TForm1.CloseAll1Click(Sender: TObject); var   i: Integer; begin   for i := Pred(PageControl.PageCount) downto 0 do     PageControl.Pages[i].Free; end;

If you run the application now and drop several files to the form, the OpenDroppedFile procedure will open all supported files and display them as pages in the TPageControl component, as shown in Figure 20-9.

image from book
Figure 20-9: TPageControl pages

Inside Delphi 2006
Inside Delphi 2006 (Wordware Delphi Developers Library)
ISBN: 1598220039
EAN: 2147483647
Year: 2004
Pages: 212
Authors: Ivan Hladni

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: