如何在VB.NET中旋转生成的控件? [英] How do I rotate generated controls in VB.NET?
问题描述
您好,
我正在开发一个可用于设计电气装置的项目。在这个项目中,我使用以编程方式生成的控件(图片框)。当我试图旋转这些图片框中的图像时,我偶然发现了一个奇怪的问题,我不明白,谷歌也不是很有帮助,所以我希望有人知道如何解决这个问题。
问题:
当我使用rightmouse按钮旋转生成的图片框时,没有任何反应。虽然这应该有效,对吗?但是,如果我按下rightmouse按钮,然后单击leftmousebutton并开始拖动控件,然后立即旋转。
有人知道我做错了什么以及我怎么做解决这个问题?
注意:图像应该只旋转90度角度
我尝试过:
这是在运行时添加控件的代码,以及用于移动控件和获取信息的所有事件处理程序。 Drawinggrid是添加控件的父图片框。
Hello,
I'm working on a project which can be used to design electrical installations. In this project i work with programmatically generated controls (pictureboxes). When i tried to rotate the images in these pictureboxes i stumbled on a weird problem which i don't understand and google wasn't much of a help either, so I hope that somebody here knows how to fix this problem.
The problem:
When i rotate a generated picturebox with rightmousebutton, nothing happens. While this should work, right?. However, if i press rightmousebutton, then click with leftmousebutton and start to drag the control, then it immediately rotates.
Does anybody know what i did wrong and how i could fix this?
Note: The images are supposed to rotate with only 90 degree angles
What I have tried:
Here is the code where the controls are added in runtime and all of the event handlers for moving it and get info. Drawinggrid is the parent picturebox where the controls are added in.
Private Sub DrawingGrid_DoubleClick(sender As Object, e As MouseEventArgs) Handles DrawingGrid.DoubleClick
item_num = 0
Dim pic As New TransparentPicturebox With
{
.Visible = True,
.Size = New System.Drawing.Size(ItemResizeTrackBar.Value, ItemResizeTrackBar.Value),
.SizeMode = PictureBoxSizeMode.StretchImage,
.BackColor = Color.Transparent,
.Image = ImageSelect(),
.Tag = index & "." & item_num,
.Text = curr_item
}
DrawingGrid.Controls.Add(pic)
If picnum > 0 Then
For k As Integer = 0 To picarray.Length - 1
If picarray(k).Text = pic.Text Then
item_num = item_num + 1
End If
pic.Tag = index & "." & item_num
Next
End If
Dim i As ListViewItem
i = Changelog.Items.Add("")
i.SubItems.Add(pic.Tag)
i.SubItems.Add(pic.Text & " Added")
i.SubItems.Add("Size: (" & pic.Size.Height & ";" & pic.Size.Width & ")")
Changelog.Items((Changelog.Items.Count - 1)).EnsureVisible()
Dim picList As List(Of TransparentPicturebox) = New List(Of TransparentPicturebox)
For Each c As TransparentPicturebox In DrawingGrid.Controls
If (TypeOf c Is TransparentPicturebox) Then
picList.Add(DirectCast(c, TransparentPicturebox))
End If
Next
picarray = picList.ToArray
pic.Location = New Point(MousePosition.X - DrawingGrid.Location.X - ItemResizeTrackBar.Value / 2, MousePosition.Y - DrawingGrid.Location.Y - 24 - ItemResizeTrackBar.Value / 2)
If picnum > 0 Then
TextBox1.Text = Math.Round(Math.Sqrt(Math.Abs(picarray(picnum).Location.X - picarray(picnum - 1).Location.X) ^ 2 + (Math.Abs(picarray(picnum).Location.Y - picarray(picnum - 1).Location.Y) ^ 2)))
End If
AddHandler pic.MouseDown, AddressOf PictureboxMouseDown
AddHandler pic.MouseUp, AddressOf PictureboxMouseUp
AddHandler pic.MouseMove, AddressOf PictureboxMouseMove
'AddHandler pic.MouseWheel, AddressOf PictureboxMouseWheel
AddHandler pic.Click, AddressOf PictureboxMouseClick
picnum = picnum + 1
End Sub
'imageselect function
Private Function ImageSelect()
Dim SelectImageToPlaceFromDatabase_Adapter As New OleDbDataAdapter("SELECT Id, Naam, Locatie FROM onderdelen WHERE Id =" & index + 1, myconnection)
Dim dq As New DataSet()
myconnection.Open()
SelectImageToPlaceFromDatabase_Adapter.Fill(dq, "onderdelen")
myconnection.Close()
curr_item = dq.Tables(0).Rows(0).Item(1)
Return Image.FromFile(dq.Tables(0).Rows(0).Item(2))
End Function
'PIC EVENT HANDLERS
'mouse down event update current data
Private Sub PictureboxMouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
Dim pic As TransparentPicturebox = CType(sender, TransparentPicturebox)
curr_item = pic.Text
move_object = e.Location
End Sub
'moving the pic
Private Sub PictureboxMouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
Dim pic As TransparentPicturebox = CType(sender, TransparentPicturebox)
PositionStatus.Text = "Position: " & pic.Location.ToString
If (e.Button = MouseButtons.Left) Then
pic.Left = (pic.Left + (e.X - move_object.X))
pic.Top = (pic.Top + (e.Y - move_object.Y))
If picnum > 1 Then
TextBox1.Text = Math.Round(Math.Sqrt(Math.Abs(picarray(picnum - 1).Location.X - picarray(picnum - 2).Location.X) ^ 2 + (Math.Abs(picarray(picnum - 1).Location.Y - picarray(picnum - 2).Location.Y) ^ 2)))
End If
End If
If DrawingGrid.Height <= (pic.Height + pic.Location.Y) Then
pic.Top = DrawingGrid.Height - pic.Height
If pic.Location.X <= 0 Then
pic.Left = 0
ElseIf DrawingGrid.Width <= (pic.Width + pic.Location.X) Then
pic.Left = DrawingGrid.Width - pic.Width
End If
ElseIf pic.Location.Y <= 0 Then
pic.Top = 0
If pic.Location.X <= 0 Then
pic.Left = 0
ElseIf DrawingGrid.Width <= (pic.Width + pic.Location.X) Then
pic.Left = DrawingGrid.Width - pic.Width
End If
ElseIf pic.Location.X <= 0 Then
pic.Left = 0
ElseIf DrawingGrid.Width <= (pic.Width + pic.Location.X) Then
pic.Left = DrawingGrid.Width - pic.Width
End If
End Sub
'add movement changes to changelog
Private Sub PictureboxMouseUp(ByVal sender As Object, ByVal e As MouseEventArgs)
Dim pic As TransparentPicturebox = CType(sender, TransparentPicturebox)
Dim i As ListViewItem
i = Changelog.Items.Add("")
i.SubItems.Add(pic.Tag)
i.SubItems.Add(pic.Text & " Moved to")
i.SubItems.Add("Location: (" & pic.Location.X & ";" & pic.Location.Y & ")")
Changelog.Items((Changelog.Items.Count - 1)).EnsureVisible()
End Sub
'middle click info & right click rotate event
Private Sub PictureboxMouseClick(ByVal sender As Object, ByVal e As MouseEventArgs)
Dim pic As TransparentPicturebox = CType(sender, TransparentPicturebox)
Select Case e.Button
Case MouseButtons.Middle
'MessageBox.Show("Item: " & curr_item)
Dim Info As New Form
Info.Show()
TextBox1.Text = e.Location.ToString
Info.Top = MousePosition.Y
Info.Left = MousePosition.X
Info.Height = 250
Info.Width = 250
Info.Text = "Info"
Info.MinimizeBox = False
Info.MaximizeBox = False
Info.ShowIcon = False
Info.Opacity = 0.92
Info.MaximumSize = Info.Size
Info.MinimumSize = Info.Size
AddHandler Info.Deactivate, AddressOf Info_Deactivate
Dim lbl As New Label With
{
.Text = pic.Tag,
.Location = New Point(100, 100)
}
Dim lblUp As New Label With
{
.Image = My.Resources.About,
.Location = New Point(0, 0)
}
AddHandler lblUp.Click, AddressOf lblUp_Click
Info.Controls.Add(lblUp)
Info.Controls.Add(lbl)
Case MouseButtons.Right
pic.Image.RotateFlip(RotateFlipType.Rotate90FlipNone)
End Select
End Sub
推荐答案
尝试使图片框无效:
Try invalidating the picturebox:
Case MouseButtons.Right
pic.Image.RotateFlip(RotateFlipType.Rotate90FlipNone)
pic.Invalidate()
pic.Image.RotateFlip(RotateFlipType.Rotate90FlipNone)
将旋转图像但不更新 PictureBox
,因为它使用获取
方法。
使 PictureBox
无效以触发重绘:
will rotate the image but not update the PictureBox
because it uses the Get
method.
Invalidate the PictureBox
to trigger redrawing:
pic.Image.RotateFlip(RotateFlipType.Rotate90FlipNone)
pic.Invalidate()
使用无效而不是刷新
此处因为操作是在处理程序中执行的。
Use invalidation rather than Refresh
here because the operation is executed within a handler.
这篇关于如何在VB.NET中旋转生成的控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!