2

感谢以下代码,我创建并添加图像(作为缩略图)到 FlowLayoutPanel。

实现非常简单。我读取目录中的可用图像并调用以下子过程。

Private Sub LoadImages(ByVal FlowPanel As FlowLayoutPanel, ByVal fi As FileInfo)
        Pedit = New DevExpress.XtraEditors.PictureEdit
        Pedit.Width = txtIconsWidth.EditValue
        Pedit.Height = Pedit.Width / (4 / 3)
        Dim fs As System.IO.FileStream
        fs = New System.IO.FileStream(fi.FullName, IO.FileMode.Open, IO.FileAccess.Read)
        Pedit.Image = System.Drawing.Image.FromStream(fs)
        fs.Close()
        fs.Dispose()
        Pedit.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Zoom

        If FlowPanel Is flowR Then
            AddHandler Pedit.MouseClick, AddressOf Pedit_MouseClick
            AddHandler Pedit.MouseEnter, AddressOf Pedit_MouseEnter
            AddHandler Pedit.MouseLeave, AddressOf Pedit_MouseLeave
        End If

        FlowPanel.Controls.Add(Pedit)
    End Sub

现在,我想扩展它。我想创建分页效果。应用程序应该读取所​​有可用的图像,但只绘制屏幕可见的图像。

和往常一样,我不知道从哪里开始。请问我可以用你的灯吗?

...C# 版本来了!

private void LoadImages(FlowLayoutPanel FlowPanel, FileInfo fi)
{
    Pedit = new DevExpress.XtraEditors.PictureEdit();
    Pedit.Width = txtIconsWidth.EditValue;
    Pedit.Height = Pedit.Width / (4 / 3);
    System.IO.FileStream fs = null;
    fs = new System.IO.FileStream(fi.FullName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
    Pedit.Image = System.Drawing.Image.FromStream(fs);
    fs.Close();
    fs.Dispose();
    Pedit.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Zoom;

    if (object.ReferenceEquals(FlowPanel, flowR)) {
        Pedit.MouseClick += Pedit_MouseClick;
        Pedit.MouseEnter += Pedit_MouseEnter;
        Pedit.MouseLeave += Pedit_MouseLeave;
    }

    FlowPanel.Controls.Add(Pedit);
}
4

1 回答 1

1

为了加快这个过程,一旦图像被加载,您可以缓存它们,这样您就不必在每次需要它们时从文件流中加载。

虽然我不知道显式代码,但这是一个一般过程:

1) 你可以有几个变量,但最重要的是 currentPage 的整数。

2) 接下来,您需要定义每页显示多少个缩略图,可以是常量或另一个整数变量。我们称之为 thumbsPerPage

3)在事件处理程序(OnClick,悬停或您希望的其他操作事件)上,执行以下操作:

4)清除所有项目的FlowPanel,可能类似于FlowPanel.Controls.Items.Clear()

5) 然后为范围内的给定页面添加以下图像:[(currentPage-1) * thumbsPerPage, (currentPage * thumbsPerPage) - 1]

这假设您的图像索引从 0 开始,页面索引从 1 开始

例如,对于每页 9 个图像:在第 1 页上,您需要图像 [0,8] 在第 2 页上,您需要图像 [9,17],等等。

所以在代码中它类似于

FlowPanel.Items.Clear()
for(int i = (currentPage-1) * thumbsPerPage; i < (currentPage * thumbsPerPage) - 1; i++)
   FlowPanel.Controls.Add(Pedits[i])

最后,将您的代码转换为 C# :)...不是必需的,但是当它不在 VB.NET 中时,用户通常更愿意提供帮助

于 2011-02-02T22:03:31.637 回答