|
本帖最后由 红尘如烟 于 2009-3-31 10:17 编辑
因为正好要用到这个东东,找了一下,有的效果很不错,但都是需要调用Tag属性,这样当需要用tag属性做别的事情的时候,就比较麻烦
而有一些在单个窗体中用着很好,但一旦在多个窗体中调用时会出问题,因为每个窗体都需要单独保存窗体中控件等的初始信息,但只有一个保存位置,也就是一个窗体占用了,其它就用不了,说到底就是无法真正模块化
其实这个问题说到底要实现并不难,主要问题在于每个窗体的初始信息存放,想了半天才想起来用自定义集合来实现,效果还行,当窗体中的控件在100个左右时,我在笔记本上试了一下,访问速度在0.1秒以内,还可以接受(自定义集合内的都是Variant型,所以速度较慢)
代码有点长,主要是处理子窗体和子窗体中的子窗体时代码是重复的,我试了一下用别的办法精减,但代码是少了,却极不稳定,所以为了稳定性考虑,大家将就一下代码的冗长吧
- '----------------------------------------------------------------------------------------------------
- '-函数名称: FormResize
- '-功能描述: 通用窗体自适应缩放函数,支持2层子窗体
- '-输入参数: 参数1:frm 窗体对象
- ' 参数2:IsNotInitialize 不为初始
- ' 参数3:ini 存放所有控件初始尺寸信息的自定义集合
- '-使用示例: 在窗体的调整大小事件中调用:Call FormResize(Me,blnIsInitialize,MyIni)
- '-使用注意: 下面的变量必须在每一个调用此函数的窗体内含模块中声明:
- ' Private blnIsNotInitialize As Boolean
- ' Private MyIni As New Collection
- ' 调用时参数都必须为按地址传递,不能按值传递
- '-兼 容 性: 不能在子窗体中使用
- '-参考资料:
- '-作 者: 红尘如烟
- '-创建日期: 2008年3月28日
- '----------------------------------------------------------------------------------------------------
- Function FormResize(frm As Form, IsNotInitialize As Boolean, ini As Collection)
- On Error Resume Next
- Dim ctl As Control '循环控件集合用的临时变量
- Dim ctl1 As Control '同上,用于子窗体
- Dim ctl2 As Control '同上,用于子窗体中的子窗体
- Dim strName As String '控件名称(由于在循环中多次调用控件名属性,将其存入变量以加快速度)
- Dim strName1 As String '同上,用于子窗体
- Dim strName2 As String '同上,用于子窗体中的子窗体
- Dim ScaleX As Double '窗体调整大小后的宽度除以初始宽度得到的横向比例
- Dim ScaleY As Double '窗体调整大小后的高度除以初始高度得到的纵向比例
- '取得窗体中控件的初始位置及尺寸
- If IsNotInitialize = False Then
- With ini
- .Add frm.InsideWidth, "InsideWidth"
- .Add frm.InsideHeight, "InsideHeight"
- .Add frm.Width, "FormWidth"
- '窗体的节不包含在Controls集合中,所以只能单独处理
- .Add frm.Section(acDetail).Height, frm.Section(acDetail).Name & "Height"
- .Add frm.Section(acHeader).Height, frm.Section(acHeader).Name & "Height"
- .Add frm.Section(acFooter).Height, frm.Section(acFooter).Name & "Height"
- For Each ctl In frm.Controls
- strName = ctl.Name
- .Add ctl.Left, strName & "Left"
- .Add ctl.Top, strName & "Top"
- .Add ctl.Width, strName & "Width"
- .Add ctl.Height, strName & "Height"
- .Add ctl.FontSize, strName & "FontSize"
- '同样处理子窗体
- If TypeOf ctl Is SubForm Then
- .Add ctl.Form.Width, strName & "FormWidth"
- .Add ctl.Form.Section(acDetail).Height, strName & ctl.Form.Section(acDetail).Name & "Height"
- .Add ctl.Form.Section(acHeader).Height, strName & ctl.Form.Section(acHeader).Name & "Height"
- .Add ctl.Form.Section(acFooter).Height, strName & ctl.Form.Section(acFooter).Name & "Height"
- For Each ctl1 In ctl.Form.Controls
- strName1 = ctl1.Name
- .Add ctl1.Left, strName & strName1 & "Left"
- .Add ctl1.Top, strName & strName1 & "Top"
- .Add ctl1.Width, strName & strName1 & "Width"
- .Add ctl1.Height, strName & strName1 & "Height"
- .Add ctl1.FontSize, strName & strName1 & "FontSize"
- '同样处理子窗体中的子窗体
- If TypeOf ctl1 Is SubForm Then
- .Add ctl1.Form.Width, strName & "FormWidth"
- .Add ctl1.Form.Section(acDetail).Height, strName & strName1 & ctl1.Form.Section(acDetail).Name & "Height"
- .Add ctl1.Form.Section(acHeader).Height, strName & strName1 & ctl1.Form.Section(acHeader).Name & "Height"
- .Add ctl1.Form.Section(acFooter).Height, strName & strName1 & ctl1.Form.Section(acFooter).Name & "Height"
- For Each ctl2 In ctl1.Form.Controls
- strName2 = ctl2.Name
- .Add ctl2.Left, strName & strName1 & strName2 & "Left"
- .Add ctl2.Top, strName & strName1 & strName2 & "Top"
- .Add ctl2.Width, strName & strName1 & strName2 & "Width"
- .Add ctl2.Height, strName & strName1 & strName2 & "Height"
- .Add ctl2.FontSize, strName & strName1 & strName2 & "FontSize"
- Next
- End If
- Next
- End If
- Next
- End With
- IsNotInitialize = True
- End If
- With frm
- .Painting = False '关闭窗体重画
- ScaleX = .InsideWidth / ini("InsideWidth") '得到横向比例
- ScaleY = .InsideHeight / ini("InsideHeight") '得到纵向比例
- '按照比例缩放控件的位置及尺寸
- .Width = ini("Form_Width") * ScaleX
- .Section(acDetail).Height = ini(.Section(acDetail).Name & "Height") * ScaleY
- .Section(acHeader).Height = ini(.Section(acHeader).Name & "Height") * ScaleY
- .Section(acFooter).Height = ini(.Section(acFooter).Name & "Height") * ScaleY
- End With
- For Each ctl In frm.Controls
- With ctl
- strName = .Name
- .Left = ini(strName & "Left") * ScaleX
- .Top = ini(strName & "Top") * ScaleY
- .Width = ini(strName & "Width") * ScaleX
- .Height = ini(strName & "Height") * ScaleY
- '字体大小按照横向比例和纵向比例中最小的那个进行设置
- If ScaleX < ScaleY Then
- .FontSize = ini(strName & "FontSize") * ScaleX
- Else
- .FontSize = ini(strName & "FontSize") * ScaleY
- End If
- End With
- '同样处理子窗体
- If TypeOf ctl Is SubForm Then
- With ctl
- .Form.Width = ini("Form_Width") * ScaleX
- .Form.Section(acDetail).Height = ini(strName & .Form.Section(acDetail).Name & "Height") * ScaleY
- .Form.Section(acHeader).Height = ini(strName & .Form.Section(acHeader).Name & "Height") * ScaleY
- .Form.Section(acFooter).Height = ini(strName & .Form.Section(acFooter).Name & "Height") * ScaleY
- End With
- For Each ctl1 In ctl.Form.Controls
- With ctl1
- strName1 = .Name
- .Left = ini(strName & strName1 & "Left") * ScaleX
- .Top = ini(strName & strName1 & "Top") * ScaleY
- .Width = ini(strName & strName1 & "Width") * ScaleX
- .Height = ini(strName & strName1 & "Height") * ScaleY
- If ScaleX < ScaleY Then
- .FontSize = ini(strName & strName1 & "FontSize") * ScaleX
- Else
- .FontSize = ini(strName & strName1 & "FontSize") * ScaleY
- End If
- End With
- '同样处理子窗体中的子窗体
- If TypeOf ctl1 Is SubForm Then
- With ctl1
- .Form.Width = ini("Form_Width") * ScaleX
- .Form.Section(acDetail).Height = ini(strName & strName1 & .Form.Section(acDetail).Name & "Height") * ScaleY
- .Form.Section(acHeader).Height = ini(strName & strName1 & .Form.Section(acHeader).Name & "Height") * ScaleY
- .Form.Section(acFooter).Height = ini(strName & strName1 & .Form.Section(acFooter).Name & "Height") * ScaleY
- End With
- For Each ctl2 In ctl1.Form.Controls
- With ctl2
- strName2 = .Name
- .Left = ini(strName & strName1 & strName2 & "Left") * ScaleX
- .Top = ini(strName & strName1 & strName2 & "Top") * ScaleY
- .Width = ini(strName & strName1 & strName2 & "Width") * ScaleX
- .Height = ini(strName & strName1 & strName2 & "Height") * ScaleY
- If ScaleX < ScaleY Then
- .FontSize = ini(strName & strName1 & strName2 & "FontSize") * ScaleX
- Else
- .FontSize = ini(strName & strName1 & strName2 & "FontSize") * ScaleY
- End If
- End With
- Next
- End If
- Next
- End If
- Next
- frm.Painting = True '开启窗体重画
- End Function
复制代码 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
评分
-
查看全部评分
|