douken
27-03-2018 10:31:36
Bom dia.
Eu criei um aplicativo em vba Excel que será usado em computadores distintos. O problema disto é que o UserForm é grande, o que faz com que nos monitores mais pequenos não se consiga utilizá-lo adequadamente. Assim, eu tenho procurado por um código que permita ajustar o tamanho do Userform e respetivo conteúdo à resolução do monitor.
Andei à procura na internet e encontrei alguma coisa, mas nenhum dos códigos que encontrei está preparado para 32 e 64 bit.
Este pareceu-me interessante:
Option Explicit
'Function to get screen resolution
Private Declare Function GetSystemMetrics32 Lib "user32" Alias "GetSystemMetrics" (ByVal nIndex As Long) As Long
'Functions to get DPI
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hDC As Long, ByVal nIndex As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hDC As Long) As Long
Private Const LOGPIXELSX = 88 'Pixels/inch in X
Private Const POINTS_PER_INCH As Long = 72 'A point is defined as 1/72 inches
'Return DPI
Public Function PointsPerPixel() As Double
Dim hDC As Long
Dim lDotsPerInch As Long
hDC = GetDC(0)
lDotsPerInch = GetDeviceCaps(hDC, LOGPIXELSX)
PointsPerPixel = POINTS_PER_INCH / lDotsPerInch
ReleaseDC 0, hDC
End Function
'Resize when Userform Initialize
Private Sub UserForm_Initialize()
Dim w As Long, h As Long
w = GetSystemMetrics32(0) ' Screen Resolution width in points
h = GetSystemMetrics32(1) ' Screen Resolution height in points
With Me
.StartUpPosition = 1
.Width = w * PointsPerPixel * 0.85 'Userform width= Width in Resolution * DPI * 85%
.Height = h * PointsPerPixel * 0.85 'Userform height= Height in Resolution * DPI * 85%
End With
End Sub
Será que alguém me pode ajudar a torná-lo compatível com vba7? Ou então, poderiam propor-me um código alternativo?
Cumprimentos e obrigado!
Eu criei um aplicativo em vba Excel que será usado em computadores distintos. O problema disto é que o UserForm é grande, o que faz com que nos monitores mais pequenos não se consiga utilizá-lo adequadamente. Assim, eu tenho procurado por um código que permita ajustar o tamanho do Userform e respetivo conteúdo à resolução do monitor.
Andei à procura na internet e encontrei alguma coisa, mas nenhum dos códigos que encontrei está preparado para 32 e 64 bit.
Este pareceu-me interessante:
Option Explicit
'Function to get screen resolution
Private Declare Function GetSystemMetrics32 Lib "user32" Alias "GetSystemMetrics" (ByVal nIndex As Long) As Long
'Functions to get DPI
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hDC As Long, ByVal nIndex As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hDC As Long) As Long
Private Const LOGPIXELSX = 88 'Pixels/inch in X
Private Const POINTS_PER_INCH As Long = 72 'A point is defined as 1/72 inches
'Return DPI
Public Function PointsPerPixel() As Double
Dim hDC As Long
Dim lDotsPerInch As Long
hDC = GetDC(0)
lDotsPerInch = GetDeviceCaps(hDC, LOGPIXELSX)
PointsPerPixel = POINTS_PER_INCH / lDotsPerInch
ReleaseDC 0, hDC
End Function
'Resize when Userform Initialize
Private Sub UserForm_Initialize()
Dim w As Long, h As Long
w = GetSystemMetrics32(0) ' Screen Resolution width in points
h = GetSystemMetrics32(1) ' Screen Resolution height in points
With Me
.StartUpPosition = 1
.Width = w * PointsPerPixel * 0.85 'Userform width= Width in Resolution * DPI * 85%
.Height = h * PointsPerPixel * 0.85 'Userform height= Height in Resolution * DPI * 85%
End With
End Sub
Será que alguém me pode ajudar a torná-lo compatível com vba7? Ou então, poderiam propor-me um código alternativo?
Cumprimentos e obrigado!