繁体   English   中英

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

[英]move winform in relation to a button from another form vb/c#

我要你去检查首..

这就是我正在处理的问题,按钮上的问题解决了,现在,无论何时何地拖动地图/图片,我都需要按下按钮。 它是这样的,在谷歌的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中的一个形式。 它的位置是否可能是按钮的相对/父级,就像上面的链接一样。 谢谢!

您可以尝试像这样处理图片框的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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM