从图像列表加载和查看图像 [英] Loading and viewing images from imagelist

查看:41
本文介绍了从图像列表加载和查看图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如您所见,下面给出的代码不是很有用.是否可以缩短代码.鼠标滚轮前后给出相同的结果(下一张图片).无法配置 Keydown.

As you see the code given below is not very useful. Is it possible to shorten the code. Mousewheel back and forward give the same result (next image). Keydown cannot be configured.

Private Sub Images_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    count += 1
    If count + 1 > ImageList1.Images.Count Then
        count = 0
    End If
    PictureBox1.Image = ImageList1.Images.Item(count)
End Sub

Private Sub Images_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseWheel
    count += 1
    If count + 1 > ImageList1.Images.Count Then
        count = 0
    End If
    PictureBox1.Image = ImageList1.Images.Item(count)
End Sub

Private Sub Images_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    If e.KeyCode = Keys.Down Then
        count += 1
        If count + 1 > ImageList1.Images.Count Then
            count = 0
        End If
        PictureBox1.Image = ImageList1.Images.Item(count)
    End If
End Sub

推荐答案

这里有一个缩短这个的方法,只需使用函数:

Here is a way to shorten this, just use function:

Private Sub Images_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    PictureBox1.Image = ImageList1.Images.Item(increaseCount(count))
End Sub

Private Sub Images_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseWheel
If e.Delta > 0 Then
    PictureBox1.Image = ImageList1.Images.Item(decreaseCount(count))
ElseIf e.Delta < 0 Then
    PictureBox1.Image = ImageList1.Images.Item(increaseCount(count))
End If
End Sub

Private Sub Images_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.Down Then
   PictureBox1.Image = ImageList1.Images.Item(increaseCount(count))
End If
End Sub

Private Function increaseCount(ByRef count as integer) As Integer
    count += 1
    If count + 1 > ImageList1.Images.Count Then
        count = 0
    End If
    Return count
End Sub

Private Function decreaseCount(ByRef count as integer) As Integer
    count -= 1
    If count - 1 > ImageList1.Images.Count OR count < 0 Then
        count = 0
    End If
    Return count
End Sub

e.Delta 是你滚动多少,这取决于控制面板中关于鼠标滚轮滚动的选项.所以我们不能确定这些值是什么,但好消息是向上滚动总是积极的,向下滚动总是消极的.

e.Delta is how much you scrolled, this is depending on the options in the control panel about mouse wheel scrolling. So we can't be sure what these values will be, but the good thing is that scrolling ups is always positive and scrolling down is negative.

这篇关于从图像列表加载和查看图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆