简单几行代码就实现了 ,加载本地图片文件夹的图片列表功能。
窗口拖曳一个 listview 控件 。 实现其中的方法。(如果需要点击放大,直接在listview 上添加 鼠标点击事件,打开新窗口即可)
private void loadPicBtn_Click(object sender, EventArgs e)
{
listView1.Clear();
string path = this.selectPath.Text;
listView1.View = View.LargeIcon;
ImageList listimg = new ImageList();
listimg.ImageSize = new Size(120, 120);//显示图片的大小
listView1.LargeImageList = listimg;
listimg.ColorDepth = ColorDepth.Depth32Bit;
string[] files = Directory.GetFiles(@path, "*.*");
App.filePaths = files;
for (int i = 0; i < files.Length; i++)
{
listimg.Images.Add(Image.FromFile(files[i]));
listView1.Items.Add(Path.GetFileName(files[i]));
listView1.Items[i].ImageIndex = i;
//listView1.Items[i].ToolTipText = "Demo-" + i;
SetSpacing(145, 125); //目前调整到一个合适的距离
}
}
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
const int LVM_FIRST = 0X1000;
const int LVM_SETICONSPACING = LVM_FIRST + 53;
/// <summary>
/// y 左右间距,x 是上下间距
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
public void SetSpacing(Int16 x, Int16 y)
{
SendMessage(listView1.Handle, LVM_SETICONSPACING, 0, x * 65536 + y);
this.listView1.Refresh();
}
最后显示的结果: