相对于来自另一个forms的按钮移动winform vb / c#

我要你先检查一下..

这就是我正在处理的问题,按钮上解决了问题,现在,无论何时何地拖动地图/图片,我都需要让WinForm按下按钮。 它是这样的,在谷歌的API上infowindows。 第一张照片,我是在html上制作的。

在此处输入图像描述

而这一个..这就是我现在正在努力的,在winForms上,我不能用图片拖动form2 .. 在此处输入图像描述

这是我目前的代码..

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim loc As Point = PictureBox1.PointToClient(Button1.PointToScreen(Point.Empty)) Button1.Parent = PictureBox1 Button1.Location = loc End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Form2.Show() End Sub Private Sub pictureBox1_LocationChanged(ByVal sender As Object, ByVal e As EventArgs) Dim p As Point = button1.PointToScreen(Point.Empty) p.Offset(5, 10) Form2.Location = p Form2.Owner = Me End Sub 

正如你所看到的,那个infowindow,我希望它成为我的winForms中的一个forms。 它的位置是否可能是按钮的相对/父级,就像上面的链接一样。 谢谢!

您可以尝试像这样处理图片框的LocationChanged

 //LocationChanged event handler for your pictureBox1 private void pictureBox1_LocationChanged(object sender, EventArgs e){ //Get the location of button1 in screen coordinates Point p = button1.PointToScreen(Point.Empty); //Offset it to what you want p.Offset(5,10); //set the location for your infoWindow form infoWindow.Location = p; } 

请注意,我使用infoWindow作为表单,我认为它在您的情况下可用,将FormBorderStyle设置为None并添加一些自定义关闭按钮…(您可以在此搜索更多内容,有大量示例)。 如果你不知道如何注册LocationChanged事件处理程序,这里是:

 pictureBox1.LocationChanged += pictureBox1_LocationChanged; 

另请注意,您的infoWindow表单必须将您的主表单作为其所有者:

 infoWindow.Owner = yourMainForm; //or this if the code is placed inside mainForm class infoWindow.Owner = this; 

更新

 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim loc As Point = PictureBox1.PointToClient(Button1.PointToScreen(Point.Empty)) Button1.Parent = PictureBox1 Button1.Location = loc Form2.Owner = Me End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Form2.Show() End Sub Private Sub pictureBox1_LocationChanged(ByVal sender As Object, ByVal e As EventArgs) Handles PictureBox1.LocationChanged Dim p As Point = button1.PointToScreen(Point.Empty) p.Offset(-Form2.Width/2, -Form2.Height-10) Form2.Location = p End Sub