使窗体中指定数量的控件按一定间距水平居中排列

Eddy 发布于2010-8-26 21:18:18 分类: 程序设计 已浏览loading 网友评论0条 我要评论

关于参数中的ParamArray ,解释下:

只用于最后一个参数,表示最后的参数是一个 Variant 元素的 Optional 的数组。使用 ParamArray 关键字可以提供任意数目的参数。ParamArray 关键字不能与 ByVal、ByRef或 Optional 一起使用。

代码:

 

'功能:使窗体中指定数量的控件按一定间距水平居中排列

'参数含义:space     间距值

'               AlignTop 是否顶端对齐

'               cnts()      待整理的控件

Public Sub CenterControlsHorizontal(space As Single, AlignTop As Boolean, ParamArray cnts())

Dim sngTotalSpace As Single

Dim i As Integer

Dim oParent As Object

Dim sngBaseTop As Single

Dim sngParentWidth As Single

On Error Resume Next

sngParentWidth = frmRequest.ScaleWidth'过程中再加个父窗体参数就更perfect了

For i = 0 To UBound(cnts)

    sngTotalSpace = sngTotalSpace + cnts(i).Width

Next

sngTotalSpace = sngTotalSpace + (space * (UBound(cnts)))

cnts(0).Left = (sngParentWidth - sngTotalSpace) / 2

sngBaseTop = cnts(0).Top

For i = 1 To UBound(cnts)

    cnts(i).Left = cnts(i - 1).Left + cnts(i - 1).Width + space

    If AlignTop Then cnts(i).Top = sngBaseTop

Next

End Sub

另外再附上一个启动时窗体居中的代码:

 

'功能:窗体居中

Public Function FormCenter(frmObj As Form) As Boolean

Dim lLeft As Long

Dim lTop As Long

On Error Resume Next

If frmObj.ScaleMode <> vbTwips Then Exit Function

lLeft = Screen.Width / 2 - frmObj.Width / 2

lTop = Screen.Height / 2 - frmObj.Height / 2

frmObj.Move lLeft, lTop

FormCenter = Err.Number = 0 And Err.LastDllError = 0'这个返回方式不错^_

End Function

已经有(0)位网友发表了评论,你也评一评吧!
原创文章如转载,请注明:转载自Eddy Blog
原文地址:http://www.rrgod.com/program/543.html     欢迎订阅Eddy Blog

关于 VB  ParamArray  窗体居中  的相关文章

记住我的信息,下次不用再输入 欢迎给Eddy Blog留言