Programming to build your own recycle bin with Visual Basic

zhaozj2021-02-08  333

Programming to build your own recycle bin with Visual Basic

Http://tech.sina.com.cn 2000/09/29

Software world

Wang Fusheng Yang Hongyong

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 the work in this project are reflected in the following DragDrop event procedure: 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 event The CONTROLINDEX code in the process finds the index of the drag control; the next two lines of code make the picture box and tags are temporarily invisible after the label 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 tria file specification. "form2.print" The User CAN DRAG The Icon To Be 'Flushed' Away IE Deleted. "The click process of the OK button on the End Sub dialog is: Private Sub btnok-Click () ME.HIDE FORM1.SHOW End Sub Startup Form-load process 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 Startup File menu Sub-form: New File Spec EXIT Click the New File SPEC menu item to view new directories or drives. 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 Display files on the startup form is included in a general procedure DisplayFiles, which is only one parameter, which is as follows: Public Sub DisplayFiles' 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 ) .V isible = 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 DO Confucius Realize the picture frame and tag, separated spacing, is determined by different values. 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.

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

New Post(0)