[英]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.