获取用户屏幕宽高比vba excel

时间:2016-12-16 08:56:44

标签: excel vba excel-vba screen aspect-ratio

我在excel中设计了一个工具供多个用户使用。当我在显示器上打开工具时,它完全适合屏幕。我只需缩放到一个范围即可完成此操作:

<DataGrid x:Name="dataGrid1" 
          IsReadOnly="True" 
          HorizontalAlignment="Left" 
          Margin="50,30,0,0" 
          VerticalAlignment="Top" 
          Height="251" 
          Width="544" 
          AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Name" Binding="{Binding Birthday}" />
    </DataGrid.Columns>
</DataGrid>

我想知道是否有人知道更好的方法。有些用户说这个工具切断了侧面和顶部的部分?我想这是因为他们的监视器的纵横比与我的不同。

有没有办法在excel中使用VBA访问这类信息?如果是这种情况,我可以为不同类型的屏幕制作案例。

2 个答案:

答案 0 :(得分:1)

试试这个:

Declare Function GetSystemMetrics32 Lib "User32" _
    Alias "GetSystemMetrics" (ByVal nIndex As Long) As Long

Sub FindResolution()
Dim w As Long, h As Long
    w = GetSystemMetrics32(0) ' width in pixels
    h = GetSystemMetrics32(1) ' height in pixels
    MsgBox w & Chr(10) & h, vbOKOnly + vbInformation, "Monitor Size (width x height)"


End Sub

答案 1 :(得分:-1)

对于自动调整,我通常使用:

工作表上的

突出显示要显示的整个单元格范围 转到“插入”菜单,然后选择“#34;名称&#34;然后&#34;定义&#34; 为您突出显示的范围命名&#34; ResizeRange&#34;

然后在VBA中选择&#34; ThisWorkbook&#34;并粘贴以下代码:

Private Sub Workbook_Open()
range("ResizeRange").select
ActiveWindow.Zoom = True
cells(1,1).select
end sub

如果你想更进一步,你也可以删除所有的丝带等,这样你只能在视觉上看到你的传感器。然后执行以下操作

在这本工作簿中:

Sub Workbook_Open()
Application.EnableEvents = False
Call masque
Application.EnableEvents = True
End Sub

Sub Workbook_Activate()
Application.EnableEvents = False
Call masque
Application.EnableEvents = True
End Sub

Sub Workbook_Deactivate()
Application.EnableEvents = False
Call normal
Application.EnableEvents = True
End Sub

Sub Workbook_BeforeClose(Cancel As Boolean)
Application.EnableEvents = False
Call normal
Application.EnableEvents = True
ThisWorkbook.Saved = True
End Sub

在module1中:

Sub masque()
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",False)"
ActiveWindow.DisplayHeadings = False
ActiveWindow.DisplayGridlines = False
Application.DisplayFullScreen = True
Application.DisplayStatusBar = Not Application.DisplayStatusBar
Application.WindowState = xlMaximized
ActiveWindow.WindowState = xlMaximized
Application.DisplayFormulaBar = False
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub

在module2中:

Sub normal()
Application.ScreenUpdating = False
ActiveWindow.View = xlNormalView
ActiveWindow.DisplayHeadings = True
ActiveWindow.DisplayGridlines = True
Application.DisplayStatusBar = True
ActiveWindow.DisplayHorizontalScrollBar = True
ActiveWindow.DisplayVerticalScrollBar = True
Application.DisplayFullScreen = False
Application.DisplayFormulaBar = True
Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",True)"
'Curiously, if we put the previous line at the beginning of the module, it     is not taken into account each time ...
Application.ScreenUpdating = True
End Sub

在每张纸上:

Sub Worksheet_Open()
Call masque
End Sub

Sub Worksheet_Activate()
Application.ScreenUpdating = False
Call masque 
Application.ScreenUpdating = True
End Sub

这将删除所有内容,并在关闭后将其全部放回原处,因此如果您打开excel文件,它将再次显示正常。

在worksheet_activate和worksheet_open中,您可以添加以下行,以确保一次无法滚动,并且您的信息始终保持在屏幕上。

me.scrollarea = resizerange