|
|
Выравнивание Элементов Управления по центру при изменении размеров Формы
... то что должно быть по середине - там и останется, не зависимо от изменений размеров формы ...
Внимание!
Свойство Элемента(ов) Horizontal Anchor должно быть устоновлено = Left (по умолчанию) - иначе результат будет не ожидаемым
Option Compare Database
Option Explicit
Private Const cm = 567
Private lLeft As Long
Private lTop As Long
Private lWidth As Long
Private lHeight As Long
Private iСorrectionTwips As Long
Public Sub ControlToCenterHz(frm As Form, ctrl As Control, Optional iMinFormWidthCm As Currency = 0, Optional iPlusMinusCm As Currency = 0)
Dim iNewFormWidth As Long
Dim iLeftToFormMid As Long
Dim lVal As Long
On Error GoTo ControlToCenterHz_Err
iNewFormWidth = frm.InsideWidth
lVal = iMinFormWidthCm * cm + lVal
If iNewFormWidth < lVal Then GoTo ControlToCenterHz_Bye
iСorrectionTwips = Round(iPlusMinusCm * cm, 0)
iLeftToFormMid = Round(iNewFormWidth / 2, 0)
lTop = ctrl.Top
lWidth = ctrl.Width
lHeight = ctrl.Height
lLeft = NewControlPositionInTwips(iLeftToFormMid, ctrl.Width, iСorrectionTwips)
If lLeft < 0 Then GoTo ControlToCenterHz_Bye
ctrl.Move lLeft, lTop, lWidth, lHeight
ControlToCenterHz_Bye:
Exit Sub
ControlToCenterHz_Err:
MsgBox "Ошибка " & Err.Number & vbCrLf & Err.Description & vbCrLf & _
"в процедуре: ControlToCenterHz", vbCritical, "Error!"
Resume ControlToCenterHz_Bye
End Sub
Public Sub ControlToCenterVr(frm As Form, ctrl As Control, Optional ByVal iMinFormHeightCm As Currency = 0, Optional ByVal iPlusMinusCm As Currency = 0)
Dim iNewFormHeight As Long
Dim iTopToFormMid As Long
Dim lVal As Long
Dim lZLong As Long
On Error GoTo ControlToCenterVr_Err
iNewFormHeight = frm.InsideHeight
lVal = iMinFormHeightCm * cm + lVal
If iNewFormHeight < lVal Then GoTo ControlToCenterVr_Bye
iСorrectionTwips = Round(iPlusMinusCm * cm, 0)
If FormHasSections(frm) = True Then
If frm.Section(acHeader).Visible = True Then lZLong = frm.Section(acHeader).Height
If frm.Section(acFooter).Visible = True Then lZLong = lZLong + frm.Section(acFooter).Height
Else
lZLong = 0
End If
lVal = iNewFormHeight - lZLong
iTopToFormMid = Round(lVal / 2, 0)
lLeft = ctrl.Left
lWidth = ctrl.Width
lHeight = ctrl.Height
lTop = NewControlPositionInTwips(iTopToFormMid, ctrl.Height, iСorrectionTwips)
ctrl.Move lLeft, lTop, lWidth, lHeight
ControlToCenterVr_Bye:
Exit Sub
ControlToCenterVr_Err:
MsgBox "Ошибка " & Err.Number & vbCrLf & Err.Description & vbCrLf & _
"в процедуре: ControlToCenterVr", vbCritical, "Error!"
Resume ControlToCenterVr_Bye
End Sub
Private Function FormHasSections(frm As Form) As Boolean
Dim b As Boolean
On Error GoTo FormHasSections_Err
b = frm.Section(acHeader).Visible
FormHasSections = True
Exit Function
FormHasSections_Err:
FormHasSections = False
Err.Clear
End Function
Private Function NewControlPositionInTwips(iTwipsToFormMid As Long, iTwipsControlSize As Long, iPlusMinusTwips As Long) As Long
Dim lALong As Long
Dim lZLong As Long
lALong = iTwipsToFormMid + iPlusMinusTwips
lZLong = Round(iTwipsControlSize / 2, 0)
lALong = lALong - lZLong
If lALong < 0 Then lALong = 0
NewControlPositionInTwips = lALong
End Function
Public Sub ControlsToCenterAutoCode(sFormName As String, Optional bForNotCurrentForm As Boolean = False, Optional curPlusMinusToTopCm As Currency)
Dim iNewFormWidth As Long
Dim iNewFormHeight As Long
Dim iToFormMidHz As Long
Dim iToFormMidVr As Long
Dim iСorrectionTwips As Long
Dim frm As Form
Dim ctrl As Control
Dim sOne As String
Dim sTwo As String
Dim sFormLink As String
Dim cMinFormWidthCm As Currency
Dim cMinFormHeightCm As Currency
Dim cСorrectionCm As Currency
Dim bIsLoaded As Boolean
Dim lVal As Long
Dim cVal As Currency
On Error GoTo ControlsToCenterAutoCode_Err
For lVal = 0 To Forms.Count - 1
If Forms(lVal).FormName = sFormName Then
bIsLoaded = True
End If
Next
If bIsLoaded = True Then DoCmd.Close acForm, sFormName, acSaveNo
DoCmd.OpenForm sFormName, acDesign, "", "", , acHidden
Set frm = Forms(sFormName)
If bForNotCurrentForm = False Then
sFormLink = "Me"
Else
sFormLink = "Forms(""" & frm.Name & """)"
End If
iNewFormWidth = frm.Width
cMinFormWidthCm = Round(iNewFormWidth / cm, 4)
iToFormMidHz = Round(iNewFormWidth / 2, 0)
If FormHasSections(frm) = True Then
If frm.Section(acHeader).Visible = True Then lVal = frm.Section(acHeader).Height
If frm.Section(acFooter).Visible = True Then lVal = lVal + frm.Section(acFooter).Height
Else
lVal = 0
End If
iNewFormHeight = frm.Section(0).Height + lVal
cVal = Round(iNewFormHeight / cm, 2)
cMinFormHeightCm = Round(cVal, 1)
lVal = frm.Section(0).Height
iToFormMidVr = Round(lVal / 2, 0)
Debug.Print "
Debug.Print "
Debug.Print "
For Each ctrl In frm.Section(acDetail).Controls
With ctrl
cVal = ctrl.Left + Round(ctrl.Width / 2, 3)
iСorrectionTwips = cVal - iToFormMidHz
cVal = iСorrectionTwips / cm
cСorrectionCm = Round(cVal, 3)
sOne = CStr(Format(cMinFormWidthCm, "0.0"))
sOne = Replace(sOne, ",", ".")
sTwo = CStr(cСorrectionCm): sTwo = Replace(sTwo, ",", ".")
Debug.Print "
Debug.Print " ControlToCenterHz " & sFormLink & ", " & sFormLink & "!" & ctrl.Name & ", " & sOne & ", " & sTwo
cVal = ctrl.Top + Round(ctrl.Height / 2, 0)
iСorrectionTwips = cVal - iToFormMidVr
cVal = iСorrectionTwips / cm
cСorrectionCm = Round(cVal, 3)
cСorrectionCm = cСorrectionCm + curPlusMinusToTopCm
sOne = CStr(Format(cMinFormHeightCm, "0.0"))
sOne = Replace(sOne, ",", ".")
sTwo = CStr(cСorrectionCm)
sTwo = Replace(sTwo, ",", ".")
Debug.Print " ControlToCenterVr " & sFormLink & ", " & sFormLink & "!" & ctrl.Name & ", " & sOne & ", " & sTwo
DoEvents
End With
Next ctrl
Debug.Print "
DoCmd.OpenForm sFormName, acDesign, "", "", , acHidden
Forms(sFormName).PopUp = False
DoCmd.Close acForm, sFormName, acSaveYes
If bIsLoaded = True Then DoCmd.OpenForm sFormName, acNormal
ControlsToCenterAutoCode_Bye:
Set frm = Nothing
Exit Sub
ControlsToCenterAutoCode_Err:
MsgBox "Ошибка " & Err.Number & vbCrLf & Err.Description & vbCrLf & _
"в процедуре: ControlsToCenterAutoCode", vbCritical, "Error!"
Resume ControlsToCenterAutoCode_Bye
End Sub
MSA-2007 и выше ( 36 kB) Пример
|
|