【发布时间】:2022-01-29 03:08:13
【问题描述】:
我有一个可移动的按钮。一旦鼠标向上事件,新位置就会保存在我的设置中。当我移动按钮时,有时按钮会消失,我重新运行后,它会完全消失。
如何在关闭应用前保存按钮位置?
Public Class Form1
Dim x, y As Integer
Dim newpoint As New Point
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If My.Settings.button1_x70123454440211bghff > 0 Or My.Settings.button1_y70123454440211bghff > 0 Then
Me.Button1.Location = New Point(My.Settings.button1_x70123454440211bghff, My.Settings.button1_y70123454440211bghff)
End If
End Sub
Private Sub Button1_MouseMove(sender As Object, e As MouseEventArgs) Handles Button1.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
newpoint = Control.MousePosition
newpoint = Control.MousePosition
newpoint.X -= x
newpoint.Y -= y
Button1.Location = newpoint
End If
End Sub
Private Sub Button1_MouseDown(sender As Object, e As MouseEventArgs) Handles Button1.MouseDown
x = Control.MousePosition.X - Button1.Location.X
y = Control.MousePosition.Y - Button1.Location.Y
End Sub
Private Sub Button1_MouseUp(sender As Object, e As MouseEventArgs) Handles Button1.MouseUp
My.Settings.button1_x70123454440211bghff = x
My.Settings.button1_y70123454440211bghff = y
My.Settings.Save()
End Sub
Private Sub Form1_Closed(sender As Object, e As EventArgs) Handles Me.Closed
My.Settings.richtextbox1 = RichTextBox1.Text
End Sub
End Class
【问题讨论】:
-
您正在将
x和y保存到您的设置中。这些变量实际上代表什么?当然不是Button的位置,那你为什么要保存它们呢?如果您想保存Button的位置,请实际执行此操作,即保存Left和Top属性值。 -
您必须阻止用户将按钮移出窗口。针对 0 和 Me.ClientSize.Width - Button1.Width 进行测试,高度相同。这会产生有趣的鼠标问题,因此最好使用布尔变量取消移动。
-
在
Form_Load,If My.Settings.button1_x70123454440211bghff > 0 Or My.Settings.button1_y70123454440211bghff > 0 Then应该是And? -
确保设置范围为用户。
标签: vb.net