vb.net 相对于窗体或屏幕定位 UserControl

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25934400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:14:51  来源:igfitidea点击:

Positioning UserControl relative to Form or Screen

vb.netwinformsuser-controlslocation

提问by mookey

Is there any way to get and set the .Locationof an UserControl, which is positioned on a Container(e.g. a Panel) relative to the "most-parental" Form?

有没有办法获取和设置.Locationan 的UserControl,它位于相对于“最亲父母”的表单Container(例如 a Panel)上?

I know that there is the possibility of calculating the offset of the Panelitself and adding it to the .Locationof the UserControl.

我知道,有计算的偏移的可能性Panel本身和它添加到.LocationUserControl

But in my case the number of parent-levels is unknown and can differ from case to case. So once the UserControlcould be placed on a Panelwhich is directly on the Form. But there is also the possibility that the UserControlis placed on a 2nd Panelwhich is on the 1st Panelwhich is on the Form.

但在我的情况下,父级的数量是未知的,并且可能因情况而异。所以一旦UserControl可以Panel直接放在Form. 但也有可能将UserControl放在 2ndPanel上,即 1stPanel上,即Form.

采纳答案by γηρ?σκω δ' αε? πολλ? διδασκ?με

Try:

尝试:

Dim pnt As Point

pnt = UserControl.PointToScreen(New Point(0, 0))
pnt = Me.PointToClient(pnt)

This calculates the location relative to your Form. Change Meto any control, if you like

这将计算相对于您的Form的位置。如果您愿意,可以将更改为任何控件

Now, if you want to setthe location eg (100, 100), relative to your Form

现在,如果您想设置相对于您的表单的位置,例如 (100, 100)

pnt = Me.PointToScreen(New Point(100, 100))
pnt = UserControl.Parent.PointToClient(pnt)
UserControl.Location = pnt

Remember that, if the new location is outside the parent area the control will not be visible.

请记住,如果新位置在父区域之外,则控件将不可见。

回答by Capellan

What if you take your idea of calculating the offset of the Panel and calculate the offset recursively back to the Form? i.e. I have a Textbox1 within a Panel2 within a Panel1. Panel1 is located at .Left 266, Panel2 is at .Left 77 within Panel1.

如果您采用计算面板偏移量的想法并递归计算偏移量返回表单会怎样?即我在 Panel1 中的 Panel2 中有一个 Textbox1。Panel1 位于 .Left 266,Panel2 位于 Panel1 内的 .Left 77。

Private Function GetLeftOffset(ByVal UserControl As Control) As Int32
    Dim intLeftOffset As Int32 = 0
    If Not TypeOf UserControl.Parent Is Form Then
        intLeftOffset = UserControl.Parent.Left
        intLeftOffset += GetLeftOffset(UserControl.Parent)
    End If
    Return intLeftOffset
End Function

Now if I GetLeftOffset(Me.TextBox1), it returns an Offset of 343 (266 + 77).

现在,如果我 GetLeftOffset(Me.TextBox1),它将返回 343 (266 + 77) 的偏移量。

回答by oldvbguy

I use this function to place a context menu near a control like a textbox or button. You can set x and y to zero to return the location of a control itself.

我使用此函数在文本框或按钮等控件附近放置上下文菜单。您可以将 x 和 y 设置为零以返回控件本身的位置。

'--- Return the screen location of a control with an offset

Private Function Offset(ByRef controlObj As Control, ByVal x As Integer, ByVal y As Integer) As Point

  Dim pt As Point
  Dim parentObj As Control = controlObj.Parent

  Do While parentObj IsNot controlObj.FindForm
    x += parentObj.Location.X
    y += parentObj.Location.Y
    parentObj = parentObj.Parent
  Loop

  pt = PointToScreen(controlObj.Location)
  pt.Offset(x, y)
  Return pt

End Function