避免图片框重叠 [英] avoid overlapping of pictureboxes

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

问题描述

我正在制作一个应用程序,其中在运行时创建了多个图片框,但它们彼此不重叠
我只为两个图片框编写了代码bt.如何对图片框数量进行编码..



代码:

I am making an application in which number of pictureboxes created at runtime but they do not overlap to each other
I wrote a code bt its only for two pictureboxes.How can i do that for number of pictureboxes..



code:

Public Class Form1
    Dim pic1, pic2 As Rectangle
    Dim drag, iscollided As Boolean
    Dim mousex, mousey As Integer

    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 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 Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        pic1.Location = PictureBox1.Location
        pic2.Location = PictureBox2.Location
        pic1.Size = PictureBox1.Size
        pic2.Size = PictureBox2.Size
        PictureBox1.SendToBack()
        PictureBox2.SendToBack()
        iscollided = CheckCollision()
        If iscollided = True Then
            PictureBox1.BackColor = Color.Transparent
        Else
            PictureBox1.BackColor = Color.Black
        End If
        Me.Text = iscollided.ToString
        AddHandler PictureBox1.MouseDown, AddressOf pic_MouseDown
        AddHandler PictureBox1.MouseMove, AddressOf pic_Mousemove
        AddHandler PictureBox1.MouseUp, AddressOf pic_Mouseup
        AddHandler PictureBox2.MouseDown, AddressOf pic_MouseDown
        AddHandler PictureBox2.MouseMove, AddressOf pic_Mousemove
        AddHandler PictureBox2.MouseUp, AddressOf pic_Mouseup

    End Sub


    Public Function CheckCollision()
        Dim result As Boolean = False
        If pic1.IntersectsWith(pic2) Then
            result = True
        Else
            result = False
        End If
        Return result
    End Function
End Class

推荐答案

您应更改CheckCollision()的方式,使控件"pic1"始终是您要移动的控件,而对于另一个控件,您可以使用GetChilAtPoint [
You should change CheckCollision() in such a way that control "pic1" will always be your control you are moving and for the other one you can use GetChilAtPoint[^] method of form controls. If the method returns a control of type "PictureBox" then you should cast the control in picturebox and use "IntersectsWith" method as you already did.


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

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