图片框阵列的碰撞 [英] collision of picturebox array

查看:82
本文介绍了图片框阵列的碰撞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中,我使用了一个按钮来创建图片框&它可以移动.我想避免在窗体中创建的任何图片框重叠.我为此使用了计时器,但我在避免重叠和处理方面遇到麻烦.我无法在intersectwith()中应用数组.


In my code i used a button to create a picturebox & it can move. I want to avoid overlapping of any pictureboxes created in the form.i used timer for it but i am having a trouble to avoid overlapping & i cant apply array in the intersectwith().


Public Class Form1
    Dim pic As New List(Of PictureBox)
    Dim mousex, mousey As Integer
    Dim drag As Boolean = False
    Private Sub pic_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        If e.Button = Windows.Forms.MouseButtons.Left Then
            Me.Cursor = Cursors.Hand
            drag = True
            mousex = -e.X
            mousey = -e.Y
            Dim left As Integer = Me.PointToClient(MousePosition).X - sender.location.x
            Dim right As Integer = Me.PointToClient(MousePosition).Y - sender.location.y
            Dim width As Integer = Me.ClientSize.Width - (sender.width - left)
            Dim height As Integer = Me.ClientSize.Height - (sender.width - right)
            Windows.Forms.Cursor.Clip = Me.RectangleToScreen(New Rectangle(left, right, width, height))
            sender.invalidate()
        End If
    End Sub

    Private Sub pic_Mousemove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        If drag Then
            Dim mposition As New Point()
            mposition = Me.PointToClient(MousePosition)
            mposition.Offset(mousex, mousey)
            sender.location = mposition
        End If
    End Sub

    Private Sub pic_Mouseup(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        drag = False
        Windows.Forms.Cursor.Clip = Nothing
        Me.Cursor = Cursors.Arrow
        sender.invalidate()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If pic(1).Bounds.IntersectsWith(pic(2).Bounds) Then
            Button1.Enabled = False
        Else
            pic(1).Enabled = True
        End If
    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim a As New PictureBox
        a.Image = Image.FromFile("C:\Users\Rose\Desktop\pic.jpeg")
        Dim pic = a
        Me.Controls.Add(pic)
        AddHandler pic.MouseDown, AddressOf pic_MouseDown
        AddHandler pic.MouseMove, AddressOf pic_Mousemove
        AddHandler pic.MouseUp, AddressOf pic_Mouseup
    End Sub
End Class

推荐答案

我注意到这是来自http://www.codeproject.com/Questions/403349/avoid-overlapping-of-pictureboxes的重复内容

您是说不能将数组与IntersectsWith一起使用,所以为什么不对数组进行"For Each ... Next"循环,调用IntersectsWith
I noticed this is a repeat from http://www.codeproject.com/Questions/403349/avoid-overlapping-of-pictureboxes

You''re saying you can''t use an array with IntersectsWith, so why not just do a "For Each ... Next" loop through your array, calling IntersectsWith


这篇关于图片框阵列的碰撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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