我有简化的源代码:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim Panel1 As New Panel
Panel1.Size = New Size(4000, 4000)
Panel1.Location = New Point(-2000, -2000)
Me.Controls.Add(Panel1)
Debug.WriteLine(Panel1.Location.ToString)
Me.WindowState = FormWindowState.Minimized
ShowInTaskbar = False
Debug.WriteLine(Panel1.Location.ToString)
End Sub
我的问题是,当表单发送时最小化为" ShowInTaskbar = False"然后如果控制X或Y位置超过-768,表格将其重新定位到-768。 例如: Panel1的位置为{X = -2000,Y = -2000} 但是在最小化形式之后,表单会自动将其设置为{X = -768,Y = -768}
如何禁用此自动重定位。
开发环境VB2013 .NET2.0 Win7 64位。
答案 0 :(得分:0)
我试图追溯到导致此问题的原因。 当您更改 ShowInTaskbar 属性时,这会调用 MyBase.RecreateHandle(),后者调用 RecreateHandleCore()。 这里调用了 2 个方法:Me.DestroyHandle() 和 Me.CreateHandle()。 这些方法调用子控件相同的方法。 我无法追踪更多。我认为这是一个 Windows 错误。
如果表单最小化,以下代码给出相同的结果:
Dim Panel1 As New Panel
Panel1.Size = New Size(4000, 4000)
Panel1.Location = New Point(-2000, -2000)
Me.Controls.Add(Panel1)
Debug.WriteLine("Panel handle: " & Panel1.Handle.ToString & " Panel Bounds: " & Panel1.Bounds.ToString)
Debug.WriteLine("Form handle: " & Me.Handle.ToString & " Form Bounds: " & Me.Bounds.ToString)
Me.WindowState = FormWindowState.Minimized
Debug.WriteLine("----- Minimized -----")
Debug.WriteLine("Panel handle: " & Panel1.Handle.ToString & " Panel Bounds: " & Panel1.Bounds.ToString)
Debug.WriteLine("Form handle: " & Me.Handle.ToString & " Form Bounds: " & Me.Bounds.ToString)
Panel1.[GetType]().GetMethod("DestroyHandle", BindingFlags.Instance Or BindingFlags.NonPublic).Invoke(Panel1, Nothing)
Panel1.[GetType]().GetMethod("CreateHandle", BindingFlags.Instance Or BindingFlags.NonPublic).Invoke(Panel1, Nothing)
Debug.WriteLine("----- Destroyed Handle -----")
Debug.WriteLine("Panel handle: " & Panel1.Handle.ToString & " Panel Bounds: " & Panel1.Bounds.ToString)
Debug.WriteLine("Form handle: " & Me.Handle.ToString & " Form Bounds: " & Me.Bounds.ToString)
我的解决方案,在Panel创建后添加这一行。不是很漂亮,但很有效。
AddHandler Panel1.HandleCreated, Sub()
Panel1.Location = New Point(-2000, -2000)
End Sub
完整的解决方案代码:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim Panel1 As New Panel
AddHandler Panel1.HandleCreated, Sub()
Panel1.Location = New Point(-2000, -2000)
End Sub
Panel1.Size = New Size(4000, 4000)
Panel1.Location = New Point(-2000, -2000)
Me.Controls.Add(Panel1)
Me.WindowState = FormWindowState.Minimized
Debug.WriteLine(Panel1.Location.ToString) '--> {X=-2000,Y=-2000}
ShowInTaskbar = False
Debug.WriteLine(Panel1.Location.ToString) '--> {X=-2000,Y=-2000} Work
End Sub