6.4 File Management Comprehensive example: the implementation of the File Manager
At the end of this chapter, we developed a simple File manager using the file control and file management functions provided by Delphi. Although this file manager cannot be compared to the file manager provided by Windows, it also provides sufficient functionality for general file operations, and can be further expanded if the reader is interested. In the next drag-and-drop Operation chapter, we provide drag-and-drop support to make it look more like a "File manager."
Basic idea of 6.4.1 design
6.4.1.1 window Design
The main window of the File Manager is a multiple-document interface (MDI). The implementation of the file, directory display, and file management features is placed in a child window. A dialog box that completes different actions will be ejected as needed during program execution. These dialog boxes are generated dynamically when needed. Table 6.7 shows a list of the forms designed for this program.
Table 6.7 Filemanger Form List
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
The Form class feature is used to create a menu item for the class form
──────────────────────────────────────
Tfilemanager main Window
Tfmform child window windows| New Window
Tfileattrform Display file Properties file| Properties; function| Search
Tchangeform file Move, copy, rename, change file| Move.Cope.Rename Input dialog box for operations such as current directory Directory|change directory
Tsearchform Enter the name and path of the file to find function| Search
Tdiskviewform Display disk Information function| Disk View
Tviewdir Enter a subdirectory to be created directory| CreateDirectory
Taboutbox Display Copyright Information help| About
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
6.4.1.2 Interface Design
The main window interface is mainly the main menu and the status bar used to represent the current directory and the current file.
Table 6.8 main Window interface design
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Part property Features
─────────────────────────────
FileManager style=fsmdi main Window
Windowmenu=windows
Position=podefault
MAINMENU1 Main Menu
Filepanel Align=albottom Display the currently selected file
Bevelinner=bvlowered
bevelwidth=2
Directorypanel Align=albottom Displays the currently selected directory
Alignment=taleftjustify
Bevelinner=bvlowered
bevelwidth=2
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
The main window main menu includes file, WIndows, help three. The File menu item is replaced by a menu item with the same name as the Quilt window when the child window is built. Set windows, help Groupindex = 9 so that the two menu items still exist when the child window is generated.
The child window interface includes a main menu, a directory tree (directoryoutline), a file list box, a set of tabs for displaying drives (Tabset), and three timage parts for displaying drive types.
Table 6.9 Interface design of child windows
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Part property Features
───────────────────────────────────────
Fmform activecontrol=directoryoutline child window
Position=podefault
Style=fsmdichild
MAINMENU1 Main Menu
Drivetabset align=altop Display Drive
Style=tsownerdraw
Directoryoutline align=alleft Displays the directory tree for the current drive
Options=[oodrawtreeroot,
Oodrawfocusrect,oostretchbitmaps]
FileList align=alclient display files in the current directory
Filetype=[ftreadonly,
Fthidden,ftsystem,ftarchive,ftnormal]
Showglyphs=true
Network (Image) picture (network.bmp) Logo network drive
Vsible=false
Floppy (Image) picture (floppy.bmp) logo floppy
Visible=false
Fixed (Image) picture (fixed.bmp) sign hard drive
Visible=false
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
The child window main menu includes file, function, directory three menu items, which are used to complete the basic management functions of files, other management functions and directory management functions.
Because the dialog box interface design is very simple, here is no longer to repeat. The reader can directly refer to the dialog box interface diagram (Figure 6.8---6.13) that is given later.
6.4.2 the creation, placement, and closing of child windows
The creation and placement of child windows is controlled by the Windows menu of the parent window, and its menu items are as follows:
New Windows: Create a fresh child window
Tile: Tile
Cascade: Cascade
Arrangeicon: Arranging icons
Minimized all: Minimize all child windows
The creation of a child window simply calls the form's Create method:
Fileman: = tfmform.create (application);
The standard arrangement of child windows directly invokes the standard methods of the MDI window tile, Cascade, and ArrangeIcons.
Minimize implementation of all child windows using two properties of the MDI window: Mdichildcount and Mdichildren:
For I: = 0 to MDICount-1 do
Mdichildren[i]. WindowState: = wsminimized;
Frees the memory space when the child window closes, so that the OnClose event in the child window Tfmform
Action: = Onfree;
To maintain consistency with Windows File Manager, we also prohibit the closing of the last child window, which needs to be implemented during the Onclosequery event handling of the child window:
If Filemanager.mdichildcount <= 1 Then
Canclose: = False;
Canclose is a parameter returned by the Onclosequery event procedure that determines whether the window can be closed.
Because this procedure is owned by a child window, Mdichildcount must precede its object name FileManager.
But unfortunately: Our program cannot be terminated! The original MDI window closes all its child windows before it closes. If the child window cannot be closed, the MDI window cannot be closed.
To do this we need to determine whether the system menu of the child window or the exit item of the menu is issued for the shutdown message.
Define a global variable
Var
Exitclick:boolean;
During the Exit1click event processing of a child window:
Exitclick: = True;
Filemanager.exit1click (Sender);
This global variable can be used to detect whether the child window should be closed before it closes:
If (Filemanager.mdichildcount <= 1) and (not Exitclick) then
Canclose: = False;