Sei sulla pagina 1di 3

Option Explicit

Public PrevResizeX As Long


Public PrevResizeY As Long
Public Const SPI_GETWORKAREA& = 48
Public Const cstResX = 1024
Public Const cstResY = 768

Public Type RECT


Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Public Declare Function SystemParametersInfo Lib "user32" _


Alias "SystemParametersInfoA" _
(ByVal uAction As Long, _
ByVal uParam As Long, _
lpvParam As Any, _
ByVal fuWinIni As Long) As Long

Public Function CheckResolution(Formname As Form)


If Screen.Width / Screen.TwipsPerPixelX <> cstResX Then
Call ResizeX(Formname)
End If
If Screen.Height / Screen.TwipsPerPixelY <> cstResY Then
Call ResizeY(Formname)
End If
End Function

Public Function ResizeAll(Formname As Form)


Dim tmpControl As Control

'If a control doesnot have width or height property then


'ignore it and continue with next control on the form
On Error Resume Next

'If the previous form width was 0


'Which means that this function wasn't run before
'then change prevresize x and y and exit function
If PrevResizeX = 0 Then
PrevResizeX = Formname.ScaleWidth
PrevResizeY = Formname.ScaleHeight
Exit Function
End If

'looping till all the controls on the form are covered


For Each tmpControl In Formname
tmpControl.Left = tmpControl.Left / PrevResizeX * Formname.ScaleWidth
tmpControl.Top = tmpControl.Top / PrevResizeY * Formname.ScaleHeight
tmpControl.Width = tmpControl.Width / PrevResizeX * Formname.ScaleWidth
tmpControl.Height = tmpControl.Height / PrevResizeY * Formname.ScaleHeight
Next tmpControl

'setting the Prevsize to the current form width and height


PrevResizeX = Formname.ScaleWidth
PrevResizeY = Formname.ScaleHeight
End Function
Public Function ResizeX(Formname As Form)
Dim tmpControl As Control

On Error Resume Next


'adjust form
Formname.Left = Formname.Left / cstResX * (Screen.Width / Screen.TwipsPerPixelX)
Formname.Width = Formname.Width / cstResX * (Screen.Width / Screen.TwipsPerPixelX)
'looping till all the controls on the form are covered

For Each tmpControl In Formname


tmpControl.Left = tmpControl.Left / cstResX * (Screen.Width /
Screen.TwipsPerPixelX)
tmpControl.Width = tmpControl.Width / cstResX * (Screen.Width /
Screen.TwipsPerPixelX)
Next tmpControl

End Function

Public Function ResizeY(Formname As Form)


Dim tmpControl As Control

On Error Resume Next


'adjust form
Formname.Top = Formname.Left / cstResY * (Screen.Height / Screen.TwipsPerPixelY)
Formname.Height = Formname.Height / cstResY * (Screen.Height /
Screen.TwipsPerPixelY)
'looping till all the controls on the form are covered

For Each tmpControl In Formname


tmpControl.Top = tmpControl.Top / cstResY * (Screen.Height /
Screen.TwipsPerPixelY)
tmpControl.Height = tmpControl.Height / cstResY * (Screen.Height /
Screen.TwipsPerPixelY)
Next tmpControl

End Function

Form code

Quote:
Option Explicit

Private Sub Form_Load()


Dim rc As RECT

'adjust to client area


'Call SystemParametersInfo(SPI_GETWORKAREA, 0&, rc, 0&)
'Me.Move rc.Left * Screen.TwipsPerPixelX, _
' rc.Top * Screen.TwipsPerPixelY, _
' rc.Right * Screen.TwipsPerPixelX, _
' rc.Bottom * Screen.TwipsPerPixelY

'Check screen resolution and make changes if necessary


Call CheckResolution(Me)
end sub
Private Sub Form_Resize()

'Resize controls
ResizeAll Me

end sub

''==============================================================

Option Explicit
Private Type MyControls
Left As Long
Top As Long
Width As Long
Height As Long
End Type
Dim FrmW&, FrmH&, Ctrs() As MyControls

Private Sub Form_Load()


Dim Ctrl As Control, I&
FrmW = Me.ScaleWidth
FrmH = Me.ScaleHeight
I = Me.Controls.Count - 1
ReDim Ctrs(I)
For I = 0 To I
Set Ctrl = Me.Controls(I)
Ctrs(I).Left = Ctrl.Left
Ctrs(I).Top = Ctrl.Top
Ctrs(I).Width = Ctrl.Width
Ctrs(I).Height = Ctrl.Height
Next
Set Ctrl = Nothing
End Sub

Private Sub Form_Resize()


Dim FW&, FH&, Lt&, Tp&, W&, H&, I&, Ctrl As Control
FW = Me.ScaleWidth
FH = Me.ScaleHeight

I = Me.Controls.Count - 1
For I = 0 To I
Set Ctrl = Me.Controls(I)
Lt = Ctrs(I).Left / FrmW * FW
Tp = Ctrs(I).Top / FrmH * FH
W = Ctrs(I).Width / FrmW * FW
H = Ctrs(I).Height / FrmH * FH
Ctrl.Move Lt, Tp, W, H
Next
Set Ctrl = Nothing
End Sub

Potrebbero piacerti anche