如何在不影响图片框的情况下移动图片框内的控件 [英] How to move controls inside a picturebox without leaving picturebox bound
问题描述
您好!任何人都可以帮我完成此代码..任何帮助表示赞赏..
我在picturebox1中移动控件有问题 - 作为我的控件的容器
button1 ,combobox1,textbox1和picturebox2,此代码工作正常我可以在运行时移动picturebox1内的所有控件,但问题是移动时,它会使picturebox1绑定,我需要的是,例如,如果我移动button1,当它到达时图片框绑定顶部,左侧,右侧和底部它将停止移动,以便它不会受到限制..对于那些想要帮助我的人,您可以更改我的代码。
a非常感谢提前!
公共 类 Form1
Dim cordinates As Point
私人 Sub Control_MouseDown( ByVal sender 作为 对象, ByVal e As MouseEventArgs)句柄 PictureBox2.MouseDown,Button1。 MouseDown,ComboBox1.MouseDown,TextBox1.MouseDown
cordinates = 新点(-eX,-eY)
结束 Sub
私有 Sub Control_MouseMove( ByVal sender As 对象, ByVal e As MouseEventArgs) 句柄 PictureBox2.MouseMove,Button1.MouseMove,ComboBox1.MouseMove,TextBox1.MouseMove
如果 e.Button = Windows.Forms.MouseButtons.Left 那么
如果 e.Button = Windows.Forms.MouseButtons.Left 那么
Dim ControlPosition As Point = Me .PointToClient(MousePosition)
ControlPosition.Offset (cordinates.X,cordinates.Y)
sender.Location = ControlPosition
结束 如果
结束 如果
结束 Sub
问题在于您的控件不在PictureBox中 - 它们在表单表面上,因为a PictureBox不能充当其他控件的容器。通过比较坐标来修改代码以检查控件是否在图片框内,或者使用Panel而不是 - 它是一个容器,其中包含的所有控件都将具有相对于Panel的坐标,而不是表单。因此检查很简单:上下左右分别是高度和宽度。
看起来你只是在滥用PictureBox
,这是专为非常非常有限的功能而设计的,并且是多余的,因为它只是一个辅助控件,可以完成与图像相关的最简单的杂务。
请看看我过去的答案:
在图片框中附加图片 [ ^ ],
如何从旧图纸中清除面板 [ ^ ],
在C#中绘制一个矩形 [ ^ ]。
参见:
什么样的俏皮方法是Paint? (DataGridViewImageCell.Paint(...)) [ ^ ],
在面板上捕获绘图 [ ^ ],
mdi子表单之间的绘制线 [ ^ ]。
-SA
Hi!! Can Anyone help me to complete this code.. Any help is appreciated..
I have a problem with moving controls inside picturebox1 - serves as a container of my controls
button1, combobox1, textbox1 and picturebox2, this code works fine I can move all controls inside the picturebox1 during runtime but the problem is when moving, it leaves the picturebox1 bound, what I need is, for example, if I move button1, when it reach picturebox bound top, left, right and bottom it will stop moving so that it will not be outside bound..to those who would like to help me you can alter my code.
a very big thanks in advance!
Public Class Form1
Dim cordinates As Point
Private Sub Control_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox2.MouseDown, Button1.MouseDown, ComboBox1.MouseDown, TextBox1.MouseDown
cordinates = New Point(-e.X, -e.Y)
End Sub
Private Sub Control_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox2.MouseMove, Button1.MouseMove, ComboBox1.MouseMove, TextBox1.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim ControlPosition As Point = Me.PointToClient(MousePosition)
ControlPosition.Offset(cordinates.X, cordinates.Y)
sender.Location = ControlPosition
End If
End If
End Sub
The problem is that your controls aren;t within the PictureBox - they are on the form surface, because a PictureBox cannot act as a container for other controls. Either modify your code to check if the controls are within the picture box by comparing the coordinates, or use a Panel instead - it is a container, and all controls contained within will have coordinates relative to the Panel, not the form. So checking is easy: Top and left are both zero, bottom and right are Height and Width respectively.
It looks like you are just misusingPictureBox
, which is designed for very, very limited functionality and is redundant, as it is just a helper control do the simplest possible chores related to images.
Please see my past answers:
Append a picture within picturebox[^],
How do I clear a panel from old drawing[^],
draw a rectangle in C#[^].
See also:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
capture the drawing on a panel[^],
Drawing Lines between mdi child forms[^].
—SA
这篇关于如何在不影响图片框的情况下移动图片框内的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!