Modules

5.57 API - Riposizionare una maschera centrandola nella MDI/Screen
  Alessandro Baraldi
Non sempre l'Autocenter centra in modo corretto una maschera. In realtà non è la funzione che sbaglia, quanto l'insieme della gestione grafica degli oggetti (menù, maschere Popup ecc...)
Questa funzione permette di ottimizzare la centratura di una maschera.

La funzione tiene conto del fatto che la maschera da centrare potrebbe essere popup, pertanto la centratura non verrà fatta rispetto alla MDI di Access, ma rispetto allo Screen.

Per utilizzare questa funzionalità memorizzare il codice che segue in un modulo del database e per richiamare la funzione da un punto qualsiasi del modulo di classe della maschera basterà eseguire la seguente riga di codice VBA:
Center Me
Option Compare Database
Option Explicit

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

Private Declare Function MoveWindow _
 Lib "user32" _
 (ByVal hWnd As Long, _
 ByVal x As Long, ByVal y As Long, _
 ByVal nWidth As Long, ByVal nHeight As Long, _
 ByVal bRepaint As Long) As Long

Private Declare Function GetDesktopWindow _
 Lib "user32" () As Long

Private Declare Function GetWindowRect _
 Lib "user32" _
 (ByVal hWnd As Long, lpRect As RECT) As Long

Private Declare Function GetParent _
 Lib "user32" _
 (ByVal hWnd As Long) As Long

Public Sub Center(frm As Form)
    ' Centra la Maschera nell'Area della MDI parent(Access)
    ' Se la Form č Popup allora il centraggio avviene
    ' rispetto all'area dello schermo
        
    Dim hWndParent As Long
    Dim rctParent As RECT
    Dim rct As RECT
    Dim intWidth As Integer
    Dim intHeight As Integer
    Dim intParentWidth As Integer
    Dim intParentHeight As Integer

    On Error GoTo HandleErrors

    ' Recupera l'Handle della Finestra MDI.
    hWndParent = GetParent(frm.hWnd)

   ' Recupera le coordinate della Form.
    Call GetWindowRect(frm.hWnd, rct)

    ' Se la Form non č MDI allora hwndParent<>0 e coinciderą
    ' con l'Handle dell'applicazione, altrimenti la Form č
    ' Popup.
    If hWndParent <> Application.hWndAccessApp Then
        Call GetWindowRect(hWndParent, rctParent)
    Else
        ' Get the desktop coordinates.
        Call GetWindowRect(GetDesktopWindow(), rctParent)
    End If
    ' Calcola width/height della Parent (indifferentemente
    ' che sia Access MDI, oppure lo schermo(se Popup).
    With rctParent
        intParentWidth = .Right - .Left
        intParentHeight = .Bottom - .Top
    End With
    ' Calcola width della Form, e le nuove coordinate
    ' relative alla Parent.
    With rct
        intWidth = .Right - .Left
        intHeight = .Bottom - .Top
        .Left = (intParentWidth - intWidth) \ 2
        .Top = (intParentHeight - intHeight) \ 2
    End With
    ' Sposta la Form nella nuova posizione.
    Call MoveWindow(frm.hWnd, rct.Left, rct.Top, _
     intWidth, intHeight, bRepaint:=True)
     
ExitHere:
    Exit Sub

HandleErrors:
    Select Case Err.Number
        Case Else
            Err.Raise Err.Number, Err.Source, _
             Err.Description, Err.HelpFile, Err.HelpContext
    End Select
End Sub


Se pensate di avere del materiale freeware interessante e volete pubblicarlo, allora leggete qui.