Ajustar UserForm à Resolução do Monitor

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!

RAFAEL AMORIM

27-03-2018 11:19:56

Amigão...já estive com uma demanda do mesmo tipo.

viewtopic.php?t=5760

Veja o link, aqui do fórum mesmo!

Nos meus form, sempre uso!

Referente aos w32 e w64bites faça as alterações no cód de cada computador após ler:
https://msdn.microsoft.com/pt-br/vba/la ... s-overview

essa última parte consiste em incluir ou excluir o "PtrSafe" para poder rodar legal, isto é, w64 deixa o PtrSafe e nos pc de w32 substraí-se o "PtrSafe"

Espero ter ajudado!

LucasCad

01-04-2018 21:57:29

Amigos alguem conseguiu resolver o problema de ajustar o form mas os restante continua do mesmo tamanho, tipo: textbox, multipáginas, botão, etc?????..