準備
(なし)
デザイン
- フォーム (Form1) にボタン (button1) を配置します。
- フォーム (Form1) にリストボックス (listBox1) を配置します。
サンプルコード (C#)
using System.Runtime.InteropServices;
namespace WinFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
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;
}
[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(@"E:\UNTITLED_DISC\*.*");
shellOpStruct.pto = Marshal.StringToCoTaskMemAnsi(@"F:\TEST2");
SHFileOperation(ref shellOpStruct);
Marshal.FreeCoTaskMem(shellOpStruct.pfrom);
Marshal.FreeCoTaskMem(shellOpStruct.pto);
}
}
}
解説
Win32 API を使って、コピー中にアニメーションを表示します。これはエクスプローラーでコピー中に表示されるものと同じです。
結果
動作確認環境
Visual Studio 2022 Professional (.NET 7 C#11)
ログ
初版:2016.04.27 Visual Studio 2015 Professional (C# 6.0)
