Copy files between VB and Windows Resource Manager

Source: Internet
Author: User
Tags format definition

Copy files between VB and Windows Resource Manager
The principle of copying or moving files through vbprogramming may be very clear to everyone. You can use Windows API
Shfileoperation. You can also use the built-in functions of VB to perform operations. However
The written program can only perform file operations within the program. Here I want to introduce how to program Through VB
File Operations in are connected to the copy and cut operations in Windows Resource Manager.
In Windows Resource Manager, select one or more files, right-click the file, and
Select copy from the menu. Switch to another directory, right-click, and click paste. The file is executed once.
For the copy operation, what does Windows perform during the copy process and whether to copy the entire file to the clipboard?
On? Of course not. In fact, Windows only copies a file structure to the clipboard.
As follows:
Tdropfile + file 1 file name + vbnullchar file 2 file name + vbnullchar... + file n file name + vbnullchar
Here, tdropfile is a dropfiles structure, which is defined in the Windows API. Paste the file
You can use the API function dragqueryfile to obtain the full path name of the file copied to the clipboard.
You can execute the file copy function based on the obtained file name to paste the file.
The following describes the specific program:
1. Add a module to the project file and add the following code to the module:
Option explicit

Private type pointapi
X as long
Y as long
End type

Private type shfileopstruct
Hwnd as long
Wfunc as long
Pfrom as string
PTO as string
Fflags as integer
Fanyoperationsaborted as long
Hnamemappings as long
Lpszprogresstitle as string
End type

Private declare function shfileoperation lib "shell32.dll" alias _
"Shfileoperationa" (lpfileop as shfileopstruct) as long

'Clipboard processing function
Private declare function emptyclipboard lib "USER32" () as long
Private declare function openclipboard lib "USER32" (byval hwnd _
As long) as long
Private declare function closeclipboard lib "USER32" () as long
Private declare function setclipboarddata lib "USER32" (byval wformat _
As long, byval hmem as long) as long
Private declare function getclipboarddata lib "USER32" (byval wformat _
As long) as long
Private declare function isclipboardformatavailable lib "USER32 "_
(Byval wformat as long) as long

Private declare function dragqueryfile lib "shell32.dll" alias _
"Dragqueryfilea" (byval hdrop as long, byval uint as long ,_
Byval lpstr as string, byval ch as long) as long
Private declare function dragquerypoint lib "shell32.dll" (byval _
Hdrop as long, lppoint as pointapi) as long
Private declare function globalalloc lib "Kernel32" (byval wflags _
As long, byval dwbytes as long) as long
Private declare function globalfree lib "Kernel32" (byval hmem _
Long) as long
Private declare function globallock lib "Kernel32" (byval hmem _
Long) as long
Private declare function globalunlock lib "Kernel32" (byval hmem _
Long) as long
Private declare sub copymem lib "Kernel32" alias "rtlmovememory "_
(Destination as any, source as any, byval length as long)

'Clipboard data format definition
Private const cf_text = 1
Private const cf_bitmap = 2
Private const cf_metafilepict = 3
Private const cf_sylk = 4
Private const cf_dif = 5
Private const cf_tiff = 6
Private const cf_oemtext = 7
Private const cf_dib = 8
Private const cf_palette = 9
Private const cf_pendata = 10
Private const cf_riff = 11
Private const cf_wave = 12
Private const cf_unicodetext = 13
Private const cf_enhmetafile = 14
Private const cf_hdrop = 15
Private const cf_locale = 16
Private const cf_max = 17

'Memory operation Definitions
Private const gmem_fixed = & H0
Private const gmem_moveable = & H2
Private const gmem_nocompact = & H10
Private const gmem_nodiscard = & H20
Private const gmem_zeroinit = & h40
Private const gmem_modify = & h80
Private const gmem_discardable = & h100
Private const gmem_not_banked = & h1000
Private const gmem_share = & h2000
Private const gmem_ddeshare = & h2000
Private const gmem_notify = & h4000
Private const gmem_lower = gmem_not_banked
Private const gmem_valid_flags = & h7f72
Private const gmem_invalid_handle = & h8000
Private const ghnd = (gmem_moveable or gmem_zeroinit)
Private const gptr = (gmem_fixed or gmem_zeroinit)

Private const fo_copy = & H2

Private type dropfiles
Pfiles as long
Pt as pointapi
FNC as long
Fwide as long
End type

Public Function clipcopyfiles (Files () as string) as Boolean
Dim data as string
Dim DF as dropfiles
Dim hglobal as long
Dim lpglobal as long
Dim I as long

'Clear existing data in the clipboard
If openclipboard (0 &) then
Call emptyclipboard

For I = lbound (Files) to ubound (files)
Data = Data & files (I) & vbnullchar
Next I
Data = Data & vbnullchar

'Allocate the corresponding memory size for the copy operation of the clipboard
Hglobal = globalalloc (ghnd, Len (DF) + Len (data ))
If hglobal then
Lpglobal = globallock (hglobal)

DF. pfiles = Len (DF)
'Copy the dropfiles structure to the memory.
Call copymem (byval lpglobal, DF, Len (DF ))
'Copy the full file path name to the allocated memory.
Call copymem (byval (lpglobal + Len (DF), byval data ,_
Len (data ))
Call globalunlock (hglobal)

'Copy the data to the clipboard.
If setclipboarddata (cf_hdrop, hglobal) then
Clipcopyfiles = true
End if
End if
Call closeclipboard
End if
End Function

Public Function clippastefiles (Files () as string) as long
Dim hdrop as long
Dim nfiles as long
Dim I as long
Dim DESC as string
Dim filename as string
Dim Pt as pointapi
Dim tfstr as shfileopstruct
Const max_path as long = 260

'Make sure the data format of the clipboard is file and open the clipboard
If isclipboardformatavailable (cf_hdrop) then
If openclipboard (0 &) then
Hdrop = getclipboarddata (cf_hdrop)
'Get the number of files
Nfiles = dragqueryfile (hdrop,-1 &, "", 0)

Redim files (0 to nfiles-1) as string
Filename = space (max_path)

'Confirm that the operation type is copy operation
Tfstr. wfunc = fo_copy
'The destination path is set to the path specified by file1.
Tfstr. PTO = form1.file1. Path

For I = 0 to nfiles-1
'Copy Objects Based on each obtained File
Call dragqueryfile (hdrop, I, filename, Len (filename ))
Files (I) = trimnull (filename)
Tfstr. pfrom = files (I)
Shfileoperation tfstr
Next I
Form1.file1. Refresh
Form1.dir1. Refresh

Call closeclipboard
End if
Clippastefiles = nfiles
End if
End Function

Private function trimnull (byval strin as string) as string
Dim NUL as long

Nul = instr (strin, vbnullchar)
Select case NUL
Case is> 1
Trimnull = left (strin, NUL-1)
Case 1
Trimnull = ""
Case 0
Trimnull = trim (strin)
End select
End Function

2. Add a FileListBox to form1 and set the name attribute to file1. Add a DirListBox,
Set the name attribute to dir1 and add the following code to the dir1 change event:
Private sub dir1_change ()
File1.path = dir1.path
End sub
Add a DriveListBox, set the name attribute to drive1, and add the following to the drive1 change event:
Code:
Private sub driveappschange ()
Dir1.path = drive1.drive
End sub
Add a commandbutton and set the name attribute to upload copy. Add the following to the click event of upload copy:
Code:
Private Sub Category copy_click ()
Dim files () as string
Dim path as string
Dim I as long, N as long

Path = dir1.path
If right (path, 1) <> "/" then
Path = Path &"/"
End if

'Create a copy object list based on the selection on list1.
With file1
For I = 0 to. listcount-1
If. Selected (I) then
Redim preserve files (0 to N) as string
Files (n) = Path &. List (I)
N = n + 1
End if
Next I
End

'Copy the file to clipboard
If clipcopyfiles (Files) then
Msgbox "file copied successfully.", "success"
Else
Msgbox "the file cannot be copied...", "failure"
End if
End sub
Add a commandbutton and set the name attribute to worker paste. In the click event of worker paste, add
Code below:
Private sub resume paste_click ()
Dim files () as string
Dim nret as long
Dim I as long
Dim MSG as string

Nret = clippastefiles (files)
If nret then
For I = 0 to nret-1
MSG = MSG & files (I) & vbcrlf
Next I
Msgbox MSG, "Paste" & nret & "Files"
Else
Msgbox "An error occurred while pasting files from the Clipboard", "failure"
End if
End sub

Run the file. In Windows Resource Manager, select a file and edit it in the resource manager menu. | copy
Click Export paste in form1 to copy the file copied from the Resource Manager to the directory where dir1 is located. Slave
Select a file in file1, copy it by using volume copy, and then select and edit | paste in the Resource Manager. The selected file is
Copy to the current directory of Windows Resource Manager.
The above program runs in Windows 98 VB6.0.

Www.applevb.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.