如何在图片框上旋转多个控件 [英] How do I rotate multple controlls on picturebox

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

问题描述

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

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

我尝试过的事情:

我看着这个:
旋转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!

推荐答案

您的问题是关于CP的文章,最好离开对文章底部的评论
旋转PictureBox控件(注释) [
Your question is about an article on CP, it is better to leave a comment on bottom of article
Rotating PictureBox Control (Comments)[^]
Advantage, the author will be noticed.
I checked, the author is still active on the site.


没有建议吗?
好吧,我找到了一种可能的解决方案.它并没有真正旋转控件,但却提供了相同的效果.
您可以创建一个自定义形状控件.参见此处:
Visual Studio .NET中的Windows窗体和控件形状.

创建一个四点多边形(或您想要的任何其他形状),然后仅根据旋转角度重新计算这些点.请参见 VB帮助器:如何:旋转多边形中的点 [
No suggestions?
Well I found one possible solution. Its not really rotating the control but gives the same effect.
You can create a custom shape control. See here: Shaped Windows Forms and Controls in Visual Studio .NET.

Create a four point polygon,(or any other shape you want) then just recalculate the points based on the rotation angle. See VB Helper: HowTo: Rotate the points in a polygon[^].
However, I decided to go with the graphics matrix option, using regions for interaction. Lot of code to rewrite, but drawing 100 polygons is much more eficient than drawing 100 picture boxes. I''d rather do a proper job now than sit a problem later.


感谢您的答复,但我认为您误会了.我不是在问其他人的文章.他的文章确实可以解决我的问题,但不幸的是,它不适用于我的情况.我正在寻找其他尝试的机会.
Thanks for the reply but I think you misunderstood. I''m not asking about the other persons article. His article did come up as a possible solution to my question, but unfortunately it won''t work in my case. I was looking for other possibilities to try.


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

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