如何隐藏用户窗体后面的工作簿?
How to hide workbook behind userform?
我尝试了多种方法来隐藏用户窗体后面的特定工作簿!
我最后使用的代码在这里:
Private Sub UserForm_Layout()
Application.Left = MainWindow.Left
Application.Top = MainWindow.Top
End Sub
Private Sub UserForm_Activate()
Application.Left = Me.Left
Application.Top = Me.Top
Application.Width = Me.Width * 0.85
Application.Height = Me.Height * 0.85
End sub
它会将应用程序 window 隐藏在用户窗体后面,但是如果打开了多个工作簿并且我激活了其中一个,当我之后单击用户窗体时,它只会移动用户窗体中的活动工作簿!
如何让这个函数始终只影响特定的工作簿?
此外,通过从一个 UF 跳转到另一个 UF,每次都会执行相同的代码!
基本上,我需要将特定的工作簿隐藏在用户窗体后面并且用户无法访问,但所有其他已经打开的工作簿或我打算打开的工作簿不得受此影响!如果我使用此功能或类似功能,其他工作簿必须可访问、可见且不应消失或移动!
我也试过 application.visible = false 但是,这很危险,因为它还会影响其他工作簿,并且应用程序在 OFC 任务栏上不可见,任何错误都可能导致应用程序在后台打开并且不可见用户!
如果您建议任何其他方法来实现上述要求,我很乐意尝试!
谢谢
尝试隐藏表单的父级 window
Private Sub UserForm_Initialize()
ThisWorkbook.Windows(1).Visible = False
End Sub
Private Sub UserForm_Terminate()
ThisWorkbook.Windows(1).Visible = True
End Sub
或者确定窗体的屏幕坐标并将它们应用到父级
Private Sub UserForm_Initialize()
With ThisWorkbook.Windows(1)
.WindowState = xlNormal
.Left = Me.Left + Application.Left 'Calculate exact Screen.Left coordinate
.Top = Me.Top + Application.Top 'Calculate exact Screen.Top coordinate
.Width = Me.Width * 0.85
.Height = Me.Height * 0.85
End With
End Sub
.
要获得屏幕分辨率,请使用 GetSystemMetrics function:
#If VBA7 Then
Declare PtrSafe Function GetSystemMetrics32 Lib "user32" Alias "GetSystemMetrics" _
(ByVal nIndex As Long) As Long
#Else
Declare Function GetSystemMetrics32 Lib "user32" Alias "GetSystemMetrics" _
(ByVal nIndex As Long) As Long
#End If
Public Const SM_CXSCREEN = 0
Public Const SM_CYSCREEN = 1
Private Sub setMonitors()
celTotalMonitors = GetSystemMetrics32(80)
End Sub
Private Sub setResolution()
'The width of the virtual screen, in pixels
celScreenResolutionX = Format(GetSystemMetrics32(78), "#,##0")
'The height of the virtual screen, in pixels
celScreenResolutionY = Format(GetSystemMetrics32(79), "#,##0")
'celScreenResolutionY = celScreenResolutionY.Value \ celTotalMonitors
End Sub
我尝试了多种方法来隐藏用户窗体后面的特定工作簿!
我最后使用的代码在这里:
Private Sub UserForm_Layout()
Application.Left = MainWindow.Left
Application.Top = MainWindow.Top
End Sub
Private Sub UserForm_Activate()
Application.Left = Me.Left
Application.Top = Me.Top
Application.Width = Me.Width * 0.85
Application.Height = Me.Height * 0.85
End sub
它会将应用程序 window 隐藏在用户窗体后面,但是如果打开了多个工作簿并且我激活了其中一个,当我之后单击用户窗体时,它只会移动用户窗体中的活动工作簿!
如何让这个函数始终只影响特定的工作簿?
此外,通过从一个 UF 跳转到另一个 UF,每次都会执行相同的代码!
基本上,我需要将特定的工作簿隐藏在用户窗体后面并且用户无法访问,但所有其他已经打开的工作簿或我打算打开的工作簿不得受此影响!如果我使用此功能或类似功能,其他工作簿必须可访问、可见且不应消失或移动!
我也试过 application.visible = false 但是,这很危险,因为它还会影响其他工作簿,并且应用程序在 OFC 任务栏上不可见,任何错误都可能导致应用程序在后台打开并且不可见用户!
如果您建议任何其他方法来实现上述要求,我很乐意尝试!
谢谢
尝试隐藏表单的父级 window
Private Sub UserForm_Initialize()
ThisWorkbook.Windows(1).Visible = False
End Sub
Private Sub UserForm_Terminate()
ThisWorkbook.Windows(1).Visible = True
End Sub
或者确定窗体的屏幕坐标并将它们应用到父级
Private Sub UserForm_Initialize()
With ThisWorkbook.Windows(1)
.WindowState = xlNormal
.Left = Me.Left + Application.Left 'Calculate exact Screen.Left coordinate
.Top = Me.Top + Application.Top 'Calculate exact Screen.Top coordinate
.Width = Me.Width * 0.85
.Height = Me.Height * 0.85
End With
End Sub
.
要获得屏幕分辨率,请使用 GetSystemMetrics function:
#If VBA7 Then
Declare PtrSafe Function GetSystemMetrics32 Lib "user32" Alias "GetSystemMetrics" _
(ByVal nIndex As Long) As Long
#Else
Declare Function GetSystemMetrics32 Lib "user32" Alias "GetSystemMetrics" _
(ByVal nIndex As Long) As Long
#End If
Public Const SM_CXSCREEN = 0
Public Const SM_CYSCREEN = 1
Private Sub setMonitors()
celTotalMonitors = GetSystemMetrics32(80)
End Sub
Private Sub setResolution()
'The width of the virtual screen, in pixels
celScreenResolutionX = Format(GetSystemMetrics32(78), "#,##0")
'The height of the virtual screen, in pixels
celScreenResolutionY = Format(GetSystemMetrics32(79), "#,##0")
'celScreenResolutionY = celScreenResolutionY.Value \ celTotalMonitors
End Sub