在打开Excel
时,我想以编程方式创建工作簿的新窗口,类似于Excel中的View Menu -> New Window
命令。
然后,我想将这些窗口的位置和大小分别调整为屏幕分辨率的60%
和40%
,并且它们应该在双显示器系统中的水平显示器上并排放置。
另一个监视器可以是垂直或水平的。
当水平监视器是主监视器,但如果它是辅助监视器时,所有这一切都很好。 第一个窗口的宽度未正确调整大小。因此,在监视器的右端还有一个未使用的区域,如图所示。
我使用的调整大小代码如下
Sub Resize()
Dim mainWindow As Window
Dim secondWindow As Window
'Recalculate new widths and positions
Dim mainWindowWidth As Double
Dim secondWindowWidth As Double
mainWindowWidth = 0.6 * screenWidthPoints
secondWindowWidth = 0.4 * screenWidthPoints
'Assign window variables
If Not IsNull(ActiveWorkbook.Windows(1)) Then
Set mainWindow = ActiveWorkbook.Windows(1)
'switch to normal state before setting width, else will throw error if the window state is maximized and then try to set width
mainWindow.WindowState = xlNormal
mainWindow.Left = horzMonPoints.Left
mainWindow.Top = horzMonPoints.Top
mainWindow.Width = mainWindowWidth
End If
If Not IsNull(ActiveWorkbook.Windows(2)) Then
Set secondWindow = ActiveWorkbook.Windows(2)
secondWindow.Top = mainWindow.Top
secondWindow.Width = secondWindowWidth
secondWindow.Left = mainWindow.Left + mainWindow.Width
secondWindow.Height = mainWindow.Height
End If End Sub
用于计算监视器数量以及监视器宽度和高度的代码,我在VBA中使用了Windows API调用。 通过this链接。