如以下语句可以使窗体不会跑到屏幕的外面:
Private Sub UserForm_Layout()
If Me.Left < ThisDrawing.Application.WindowLeft Then
Me.Left = ThisDrawing.Application.WindowLeft
End If
If Me.top < ThisDrawing.Application.WindowTop Then
Me.top = ThisDrawing.Application.WindowTop
End If
If Me.top > ThisDrawing.Application.WindowTop + ThisDrawing.Application.Height - Me.Height Then
Me.top = ThisDrawing.Application.WindowTop + ThisDrawing.Application.Height - Me.Height
End If
If Me.Left > ThisDrawing.Application.WindowLeft + ThisDrawing.Application.Width - Me.Width Then
Me.Left = ThisDrawing.Application.WindowLeft + ThisDrawing.Application.Width - Me.Width
End If
Debug.Print "窗口距左侧距离" & Me.Left
Debug.Print "窗口距顶部距离" & Me.top
End Sub
可惜调来调去还是有问题,可能窗体中使用的单位与应用程序使用的单位不同所致,但又不知怎么转换(大家想想办法)。