本文介绍了如何在图片框上旋转多个控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

向所有人致意.多年以来,我已经访问了论坛以寻找答案,并且从未需要提出任何问题,因为以前一直都提出了很好的答案!我习惯了数据库应用程序和GUI.但是,我的最新项目需要开始使用绘画和图形.

在我的项目中,我有一个代表地板的画框.然后,其中的多个图片框代表货架.现在,我需要能够将架子旋转到用户定义的任何角度.

我尝试过的事情:

我看着这个:
旋转PictureBox控件 [ ^ ]
但它会创建一个比图片大的透明控件.这将使我的碰撞检查混乱,因此我不能放置两个倾斜的架子彼此接触.
我所拥有的:
我创建了一个类"MyPicturebox",该类继承了picturebox并设置了自定义属性:

Greets to all. For years I have visited the forum looking for answers and have never needed to ask a question as it was always asked before with great answers! I''m used to database applications and GUIs. However my latest project I need to start playing with painting and graphics.

In my project I have a picture box representing the floor. Then multiple picture boxes inside it representing shelves. Now I need to be able to rotate the shelves to any user defined angle.

What I have tried:

I looked at this:
Rotating PictureBox Control[^]
but it creates a transparent control that is bigger than the picture. This will mess with my collision checking so I can''t put two angled shelves touching each other.
What I have:
I created a class ''MyPicturebox'' inherits picturebox and set custom properties:


Public Class MyPictureBox
    Inherits PictureBox

    'Shelf Position and Orientation
    Private _ShelfID As Integer
    Private _ShelfRotation As Single
   '...

    <CategoryAttribute("Custom"), _
      Browsable(True), _
      [ReadOnly](False), _
      DescriptionAttribute("Angle of rotation.")> _
    Public Property ShelfRotation() As Single
        Get
            Return _ShelfRotation
        End Get
        Set(ByVal value As Single)
            _ShelfRotation = value
        End Set
    End Property
 'And a bunch of other properties

'I have a custom paint event:
  Private Sub MyPictureBox_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint

        Dim g As Graphics = e.Graphics
        Dim thisWidth, thisHeight As Integer

        If _shelfBBorderBrush = "Solid" Then
            Dim BorderBrush As New SolidBrush(_ShelfBorderColor)
            g.FillRectangle(BorderBrush, 0, 0, thisWidth, thisHeight)

        ElseIf _shelfBBorderBrush = "Gradient" Then
            Dim BorderBrush As New System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle, _ShelfBorderColor, _ShelfBorderColor2, System.Drawing.Drawing2D.LinearGradientMode.Vertical)
            g.FillRectangle(BorderBrush, 0, 0, thisWidth, thisHeight)
        End If
    End Sub



到目前为止,所有这些都有效.我可以毫无问题地创建,编辑,绘制和移动控件.在我的表单上,我使用以下命令进行冲突检测:


So far all this works. I can create, edit, paint and move the controls without any problem. On my form I do collision detection with:


                For Each picItem As MyPictureBox In picFloor.Controls
                    If sender.Bounds.IntersectsWith(picItem.Bounds) Then
                        If picItem.Tag = sender.Tag Then
                            'Colliding with self
                        Else
                            'Collision!!!
                            sender.left = oldX
                            sender.top = oldY
                        End If
                    End If
                Next
'My class is added to the floor on a button click: 
  Private Sub cmdAddShelf1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddShelf1.Click
  ReDim Preserve cmdShelf(shelfCount)
        cmdShelf(shelfCount - 1) = New MyPictureBox

        With cmdShelf(shelfCount - 1)
            .Tag = shelfCount - 1
            .ShelfName = "Shelf_" & shelfCount - 1
            '...
            .Size = New Size((numDWidth.Value * mScale), (numDDepth.Value * mScale))
            .Location = New Point(DropPannel.HorizontalScroll.Value + BorderBuffer, DropPannel.VerticalScroll.Value + BorderBuffer) ' 

        End With
        AddHandler cmdShelf(shelfCount - 1).MouseWheel, AddressOf PictureBox_MouseWheel
        AddHandler cmdShelf(shelfCount - 1).MouseMove, AddressOf PictureBox_MouseMove
        AddHandler cmdShelf(shelfCount - 1).MouseClick, AddressOf PictureBox_Click
        AddHandler cmdShelf(shelfCount - 1).MouseDown, AddressOf Button_Down
        AddHandler cmdShelf(shelfCount - 1).MouseUp, AddressOf Button_Up
        AddHandler cmdShelf(shelfCount - 1).MouseEnter, AddressOf MEnter
        AddHandler cmdShelf(shelfCount - 1).MouseLeave, AddressOf MLeave
    End Sub



那么如何在不更改大小或可见性的情况下旋转控件?
我的下一个选择是摆脱图片框,仅使用单个图片框上的图形.但是然后我将松开所有事件处理程序(我认为),这将需要大量代码重写.在开始之前,我想我会问专业人士!


So how can I rotate the control without changing the size or visibility?
My next option is to get rid of the picture boxes and only work with graphics on a single picturebox. But then I will loose all my event handlers(I think) and it will take a lot of code rewriting. Before I start doing that, I thought I''ll ask the pro''s!

推荐答案




这篇关于如何在图片框上旋转多个控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 09:37