準備
(なし)
デザイン
1. フォーム (Form1) にボタン (button1) を配置します。
2. フォーム (Form1) にリストボックス (listBox1) を配置します。
サンプルコード (C#)
// 名前空間の追加 using System.Runtime.InteropServices; // 構造体の宣言 struct SHFILEOPSTRUCT { public IntPtr hWnd; public int wfunc; public IntPtr pfrom; public IntPtr pto; public int fFlags; [MarshalAs(UnmanagedType.Bool)] public bool fAnyOperationsAborted; public IntPtr hNamemappings; public IntPtr lpszProgressTitle; } // API の宣言 [DllImport("shell32.dll", EntryPoint = "SHFileOperationA")] private static extern int SHFileOperation(ref SHFILEOPSTRUCT lpFileOp); private const int FO_COPY = 0x02; // コード private void button1_Click(object sender, EventArgs e) { SHFILEOPSTRUCT shellOpStruct = new SHFILEOPSTRUCT(); shellOpStruct.hWnd = this.Handle; shellOpStruct.wfunc = FO_COPY; shellOpStruct.pfrom = Marshal.StringToCoTaskMemAnsi(@"D:\HV\*.*"); shellOpStruct.pto = Marshal.StringToCoTaskMemAnsi(@"D:\TEST2"); SHFileOperation(ref shellOpStruct); Marshal.FreeCoTaskMem(shellOpStruct.pfrom); Marshal.FreeCoTaskMem(shellOpStruct.pto); }
解説
Win32 API を使って、コピー中にアニメーションを表示します。これはエクスプローラーでコピー中に表示されるものと同じです。
結果
動作確認環境
Visual Studio 2015 Professional (C# 6.0)