Programming to build your own recycle bin with Visual Basic

zhaozj2021-02-11  221

Programming to build your own recycle bin with Visual Basic

Delete a file or folder in Windows 95, just drag the file (clip) to the recycle bin with the mouse, then release the mouse button, this visual operation has brought great convenience. If the program developers can do this in their own programs, in their own program, it also adds one of the same recycle bits in Windows. Then the developed program will be welcomed by the user. How can I complete this kind of operation? This article describes a method of programming this action using Visual Basic programming.

In the Microsoft Windows data, the mouse button will be pressed as drag, referred to as the mouse button is released. In Visual Basic, if multiple forms are used in the project, you can drag and drop an icon from a form to another. The control allows two drags depending on the two different values ​​of the DragMode feature. The default is that the value of the DragMode feature is 0, except for specific cases, the drag control is not allowed; the value of this feature is changed to 1 (Automatic) means that the user can drag the control.

Here is the implementation process of dragging the mouse to delete the file method. In Visual Basic's project, the user is allowed to drag the icon to the file to the processing unit, which is equivalent to the recycle bin in Windows 95, and the file is removed from the disk after a warning is given.

The first step, first start a project with two forms, add a picture box (or image control) at the lower corner of the startup form, set the Picture feature of this control to the icon in the trash03.ico file, which is located In the Computer subdirectory of the Icon library. If you use a picture box, set the AutoSize feature to true (if an image control is selected, set the StretCH feature to false).

Step 2, establish two control arrays, one is a label array named FileName, another name is an array of image controls for FILES. Set the DragMode feature of the image control to 1 (Automatic); set the Visble feature to false to make the label and image controls are not visible at runtime. Typically, all subsequent elements in these control arrays inherit these features.

Most of this project is reflected in the following DragDrop events:

Private Sub Garbage_DragDrop (Source As Control, X As Single, Y As Single) 'local variables Dim Msg As String Dim ControlIndex As Integer, YesNo As Integer ControlIndex = Source.Index Forml.FileName (ControlIndex) .Visble = False Msg = "Do you really want to delete " Forml.FileName (controlIndex) .Caption YesNo = MsgBox (Msg, vbYesNo," Confirmation Box ") If YesNo = vbYes Then Kill (Forml.FileName (controlIndex) .Caption) Unload Files (controlIndex) Unload Forml.FileName (ControlIndex) else forml.FileName (ControlIndex) .visible = true forml.file (controlindex) .visible = true end if End Sub

This line of ControlIndex in the event finds the index of the drag control; the next two lines of code make the picture box and the label is temporarily invisible after the operation is placed. The ControlIndex variable allows Visual Basic to extract titles from the label array (that is, the file name). The type of message box is = Vbyesno, so it is a YES / NO message box, the title is Confirmation Box. The kill command deletes a file from the disk. Once the program deletes files, remove the tags and picture boxes from the control array. If the user operates incorrectly, the confirmation message box returns NO, then the program code makes the original picture box and the label again. Next, establish a general purpose process called Directons, displaying the information required for this application provided to the user on a form, which is used as a custom dialog.

Sub Directons () Forml.Hide Form2.Show Form2.Cls Form1.Print "This program illustrates dragging and dropping mouse operations." Form2.Print "The user gives a file spec inside the message box and a form" Form2.Print "appears WITH ICONS Labelled by all the files with this file specification. "form2.print" The user can Drag the icon to be 'flushed' Away IE deleted. "End Sub

The Click process of the OK button on the dialog is:

Private sub btnok-click () me.hide form1.show end SUB

The Form-Load process of starting the form requires the Directons form and calls the general process of displaying information. It can be like this:

Private Sub Form-Load () load form2 directons end SUB

The File menu on the startup form has the following two items:

New File Spec

Exit

Click the New File SPEC menu item to let the user view the new directory or drive. This menu item calls a generic process getFile and reads the file name from the disk.

Finally, how to handle new file descriptions. When the user uses this application for the first time, Visual Basic is loaded into a number of elements in the two control arrays. The program needs to change the information contained in the title, which contains the name of the file to be deleted. These two control arises only load it once, then use the Dir $ command to create a string, and assign the value of the value to the CAPTION feature of the label after reading the value of the array. The program code is to read the disk information twice to re-determine the array size. As for the menu item, because "N" adds the underscore, the user can press N as a simpletist that activates this. The control name of this menu is set to MNUNewFilespec after the menu is open. The Click event process of this control is:

Private Sub MNUNewFilespec Click Unload Form1 Load Form1 GetFile End Sub Unload Form1 This code line clears the old element of the array of controls. GetFile process is this:. Public Sub GetFile () 'local variables Dim FileSpec As String Form2 Hide FileSpesc = InputBox $ ( "? File specification") If FileSpesc = "" Then Directons Else Form1.Show DisplayFiles (FileSpec) End If End Sub Displays the detailed accommodation of files on the startup form is included in a general procedure DisplayFiles, which is only one parameter, which is as follows:

Public Sub DisplayFiles (FileSpec As String) 'Local variables Dim NameOfFile As String Dim ControlIndex As Integer Form1 .Show Form1.Width = 8000 Form1.Height = 5500 Form1.Garbage.Move 7000,4200' move the garbage can icon NameOfFile = Dir $ (FileSpec) If NameOfFile = "" Then MsgBox "No Files found with that file specification End If ControlIndex = ControlIndex 1 'start with index = 1 Do While NameOgFile <>" "Load Form1.Files (ControlIndex) Load Form1.FileName ( Contro; index) column = (ControlIndex MOD 6) ROW = (ControlIndex-1) / 6 form1.files (controlindex) .Move (1300 * column) 275, 800 * Row 2 'to allow for menu form1.filename ( ControlIndex) .Move 1300 * Column, 800 * (Row 1) Form1.Files (ControlIndex) .Visible = True Form1.Filename (ControlIndex) .Visible = True Form1.Filename (ControlIndex) .Caption = NameOfFile ControlIndex = ControlIndex 1 IF Controlindex> 30 THEN MSGBOX ("Too Many Files!") EXIT DO End If NameOffile = Dir $ Loop End Sub

The DO loop implementation divides pictures boxes and labels, separated spacings, determined by different values ​​test. Like all new elements in the control array, they are invisible before setting the Visible feature to TRUE.

To end the program, simply associate the event process with the Quit menu item on the form, and add a query UNLOAD event to end the program.

Sub mnuquit_click () unload form2 unload (Cancel As Integer, unload mode as integer) End Sub's design method of the above program, how to drag the file in Visual Basic to delete files and the processing of files method. The program is designed to make the user feel convenient and intuitive when the user operation is performed by controlling the mouse. If you can do this in your own program, you can make your own program will also have a Windows style. Of course, there are many mouse drag delete file methods, and the method discussed here for all colleagues.

转载请注明原文地址:https://www.9cbs.com/read-4884.html

New Post(0)