c ++ - Hvordan påberåber jeg shell'en 'File Copy dialog' for at rapportere fremskridt med en kopi i Win32?

Indlæg af Hanne Mølgaard Plasc

Problem



Hvordan kan jeg kopiere filer ved at kalde windows 'copy files' dialog til at gøre det?


SHFileOperation virker som en mulighed, men det er inde i vores proces.


Af den måde har SHFileOperation GUI interface? Er det nøjagtigt det samme som Windows-kopidialogen?

Bedste reference


Ja, det er, hvad funktionen SHFileOperation er designet til. Det accepterer en SHFILEOPSTRUCT struktur som dens eneste parameter, som du kan bruge til at angive de ønskede muligheder. [9] [10]


Det er dog blevet udskiftet i Windows Vista og senere versioner med IFileOperation interface. [11]

Andre referencer 1


Ja, SHFileOperation viser standarddialogdialogerne for de tilsvarende filoperationer, du har en vis kontrol over dette, når du ringer det. [12]

Andre referencer 2


Ja. Du vil udpege SHFileOperation som i eksemplet her: [13]


using System;
using System.Windows;
using System.Runtime.InteropServices;

namespace WpfApplication2
{
    public class InteropSHFileOperation
    {
        public enum FO\_Func : uint
        {
            FO\_MOVE = 0x0001,
            FO\_COPY = 0x0002,
            FO\_DELETE = 0x0003,
            FO\_RENAME = 0x0004,
        }

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 2)]
        struct SHFILEOPSTRUCT
        {
            public IntPtr hwnd;
            public FO\_Func wFunc;
            [MarshalAs(UnmanagedType.LPWStr)]
            public string pFrom;
            [MarshalAs(UnmanagedType.LPWStr)]
            public string pTo;
            public ushort fFlags;
            [MarshalAs(UnmanagedType.Bool)]
            public bool fAnyOperationsAborted;
            public IntPtr hNameMappings;
            [MarshalAs(UnmanagedType.LPWStr)]
            public string lpszProgressTitle;

        }

        [DllImport("shell32.dll", CharSet = CharSet.Unicode)]
        static extern int SHFileOperation([In, Out] ref SHFILEOPSTRUCT lpFileOp);

        private SHFILEOPSTRUCT \_ShFile;
        public FILEOP\_FLAGS fFlags;

        public IntPtr hwnd
        {
            set
            {
                this.\_ShFile.hwnd = value;
            }
        }
        public FO\_Func wFunc
        {
            set
            {
                this.\_ShFile.wFunc = value;
            }
        }

        public string pFrom
        {
            set
            {
                this.\_ShFile.pFrom = value + '' + '';
            }
        }
        public string pTo
        {
            set
            {
                this.\_ShFile.pTo = value + '' + '';
            }
        }

        public bool fAnyOperationsAborted
        {
            set
            {
                this.\_ShFile.fAnyOperationsAborted = value;
            }
        }
        public IntPtr hNameMappings
        {
            set
            {
                this.\_ShFile.hNameMappings = value;
            }
        }
        public string lpszProgressTitle
        {
            set
            {
                this.\_ShFile.lpszProgressTitle = value + '';
            }
        }

        public InteropSHFileOperation()
        {

            this.fFlags = new FILEOP\_FLAGS();
            this.\_ShFile = new SHFILEOPSTRUCT();
            this.\_ShFile.hwnd = IntPtr.Zero;
            this.\_ShFile.wFunc = FO\_Func.FO\_COPY;
            this.\_ShFile.pFrom = "";
            this.\_ShFile.pTo = "";
            this.\_ShFile.fAnyOperationsAborted = false;
            this.\_ShFile.hNameMappings = IntPtr.Zero;
            this.\_ShFile.lpszProgressTitle = "";

        }

        public bool Execute()
        {
            this.\_ShFile.fFlags = this.fFlags.Flag;
            return SHFileOperation(ref this.\_ShFile) == 0;//true if no errors
        }

        public class FILEOP\_FLAGS
        {
            [Flags]
            private enum FILEOP\_FLAGS\_ENUM : ushort
            {
                FOF\_MULTIDESTFILES = 0x0001,
                FOF\_CONFIRMMOUSE = 0x0002,
                FOF\_SILENT = 0x0004,  // don't create progress/report
                FOF\_RENAMEONCOLLISION = 0x0008,
                FOF\_NOCONFIRMATION = 0x0010,  // Don't prompt the user.
                FOF\_WANTMAPPINGHANDLE = 0x0020,  // Fill in SHFILEOPSTRUCT.hNameMappings
                                                 // Must be freed using SHFreeNameMappings
                FOF\_ALLOWUNDO = 0x0040,
                FOF\_FILESONLY = 0x0080,  // on *.*, do only files
                FOF\_SIMPLEPROGRESS = 0x0100,  // means don't show names of files
                FOF\_NOCONFIRMMKDIR = 0x0200,  // don't confirm making any needed dirs
                FOF\_NOERRORUI = 0x0400,  // don't put up error UI
                FOF\_NOCOPYSECURITYATTRIBS = 0x0800,  // dont copy NT file Security Attributes
                FOF\_NORECURSION = 0x1000,  // don't recurse into directories.
                FOF\_NO\_CONNECTED\_ELEMENTS = 0x2000,  // don't operate on connected elements.
                FOF\_WANTNUKEWARNING = 0x4000,  // during delete operation, warn if nuking instead of recycling (partially overrides FOF\_NOCONFIRMATION)
                FOF\_NORECURSEREPARSE = 0x8000,  // treat reparse points as objects, not containers
            }

            public bool FOF\_MULTIDESTFILES = false;
            public bool FOF\_CONFIRMMOUSE = false;
            public bool FOF\_SILENT = false;
            public bool FOF\_RENAMEONCOLLISION = false;
            public bool FOF\_NOCONFIRMATION = false;
            public bool FOF\_WANTMAPPINGHANDLE = false;
            public bool FOF\_ALLOWUNDO = false;
            public bool FOF\_FILESONLY = false;
            public bool FOF\_SIMPLEPROGRESS = false;
            public bool FOF\_NOCONFIRMMKDIR = false;
            public bool FOF\_NOERRORUI = false;
            public bool FOF\_NOCOPYSECURITYATTRIBS = false;
            public bool FOF\_NORECURSION = false;
            public bool FOF\_NO\_CONNECTED\_ELEMENTS = false;
            public bool FOF\_WANTNUKEWARNING = false;
            public bool FOF\_NORECURSEREPARSE = false;

            public ushort Flag
            {
                get
                {
                    ushort ReturnValue = 0;

                    if (this.FOF\_MULTIDESTFILES)
                        ReturnValue |= (ushort)FILEOP\_FLAGS\_ENUM.FOF\_MULTIDESTFILES;
                    if (this.FOF\_CONFIRMMOUSE)
                        ReturnValue |= (ushort)FILEOP\_FLAGS\_ENUM.FOF\_CONFIRMMOUSE;
                    if (this.FOF\_SILENT)
                        ReturnValue |= (ushort)FILEOP\_FLAGS\_ENUM.FOF\_SILENT;
                    if (this.FOF\_RENAMEONCOLLISION)
                        ReturnValue |= (ushort)FILEOP\_FLAGS\_ENUM.FOF\_RENAMEONCOLLISION;
                    if (this.FOF\_NOCONFIRMATION)
                        ReturnValue |= (ushort)FILEOP\_FLAGS\_ENUM.FOF\_NOCONFIRMATION;
                    if (this.FOF\_WANTMAPPINGHANDLE)
                        ReturnValue |= (ushort)FILEOP\_FLAGS\_ENUM.FOF\_WANTMAPPINGHANDLE;
                    if (this.FOF\_ALLOWUNDO)
                        ReturnValue |= (ushort)FILEOP\_FLAGS\_ENUM.FOF\_ALLOWUNDO;
                    if (this.FOF\_FILESONLY)
                        ReturnValue |= (ushort)FILEOP\_FLAGS\_ENUM.FOF\_FILESONLY;
                    if (this.FOF\_SIMPLEPROGRESS)
                        ReturnValue |= (ushort)FILEOP\_FLAGS\_ENUM.FOF\_SIMPLEPROGRESS;
                    if (this.FOF\_NOCONFIRMMKDIR)
                        ReturnValue |= (ushort)FILEOP\_FLAGS\_ENUM.FOF\_NOCONFIRMMKDIR;
                    if (this.FOF\_NOERRORUI)
                        ReturnValue |= (ushort)FILEOP\_FLAGS\_ENUM.FOF\_NOERRORUI;
                    if (this.FOF\_NOCOPYSECURITYATTRIBS)
                        ReturnValue |= (ushort)FILEOP\_FLAGS\_ENUM.FOF\_NOCOPYSECURITYATTRIBS;
                    if (this.FOF\_NORECURSION)
                        ReturnValue |= (ushort)FILEOP\_FLAGS\_ENUM.FOF\_NORECURSION;
                    if (this.FOF\_NO\_CONNECTED\_ELEMENTS)
                        ReturnValue |= (ushort)FILEOP\_FLAGS\_ENUM.FOF\_NO\_CONNECTED\_ELEMENTS;
                    if (this.FOF\_WANTNUKEWARNING)
                        ReturnValue |= (ushort)FILEOP\_FLAGS\_ENUM.FOF\_WANTNUKEWARNING;
                    if (this.FOF\_NORECURSEREPARSE)
                        ReturnValue |= (ushort)FILEOP\_FLAGS\_ENUM.FOF\_NORECURSEREPARSE;

                    return ReturnValue;
                }
            }
        }
    }

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void ButtonBase\_OnClick(object sender, RoutedEventArgs e)
        {
            var fileOp = new InteropSHFileOperation();
            fileOp.pFrom = @"c:gitWinRTXamlToolkit";
            fileOp.pTo = @"C:	mp";
            fileOp.wFunc = InteropSHFileOperation.FO\_Func.FO\_COPY;
            fileOp.Execute();
        }
    }
}