怎样使控件随着窗体放大而放大

monking007 2003-05-18 10:44:02
怎样使控件随着窗体放大而放大,这个问题困扰了小弟很久,请大家指教
...全文
53 点赞 收藏 3
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
monking007 2003-05-18
谢谢指教,受益匪浅
回复
自由之眼 2003-05-18
一个自己写的控件自适应用窗体的函数.让控件和窗体保持大小一致。
调用一下就行,在win98/me/2000下通过。
Option Explicit
Private M_FormHight As Integer '记录窗体高度
Private M_FormWide As Integer '记录窗体宽度
Private M_NumControls As Integer '记录窗体中控件总数

Private Type M_FormPropertySize
M_FTop As Integer '存储窗体中所有控件的相对高度
M_FLeft As Integer '存储窗体中所有控件的相对左偏移
M_FWide As Integer '存储窗体中所有控件的控件宽
M_FHight As Integer '存储窗体中所有控件的高度
M_FRightMargin As Integer '存储窗体中所有控件的页边距
M_FFontSize As Integer '存储窗体中所有控件的字体大小
End Type
Private M_FControlSize() As M_FormPropertySize '定义一个控件类型

Public Sub M_Init(M_Form As Form) '对窗体的性质进行初始化
Dim i As Integer '计数器
On Error GoTo ine
If M_Form.WindowState = 1 Then '如果窗体最小化,什么也不做
GoTo inerr
End If
'M_Form.WindowState = 2 '先最大化窗体
M_FormHight = M_Form.Height '得到窗体高度
M_FormWide = M_Form.Width '得到窗体宽度
M_NumControls = M_Form.Controls.Count '得到窗体上控件的总数

ReDim M_FControlSize(M_NumControls + 1) '定义窗体中控件类型的个数
On Error Resume Next
For i = 1 To M_NumControls '为窗体属性结构数组赋值
If TypeOf M_Form.Controls(i - 1) Is Line Then
M_FControlSize(i).M_FTop = M_Form.Controls(i - 1).Y1
M_FControlSize(i).M_FLeft = M_Form.Controls(i - 1).X1
M_FControlSize(i).M_FWide = M_Form.Controls(i - 1).X2
M_FControlSize(i).M_FHight = M_Form.Controls(i - 1).Y2
'M_FControlSize(i).M_FFontSize = M_Form.Controls(i).FontSize
Else
M_FControlSize(i).M_FTop = M_Form.Controls(i - 1).Top
M_FControlSize(i).M_FLeft = M_Form.Controls(i - 1).Left
M_FControlSize(i).M_FWide = M_Form.Controls(i - 1).Width
M_FControlSize(i).M_FHight = M_Form.Controls(i - 1).Height
M_FControlSize(i).M_FFontSize = M_Form.Controls(i - 1).FontSize
'M_FControlSize(i).M_FRightMargin = M_Form.Controls(i - 1).RightMargin
End If
Next i

GoTo inerr '无错跳出循环
ine:
MsgBox Err.Description, , "错误信息"
inerr:
End Sub

Public Sub M_FormResize(M_Form As Form)
Dim i As Integer
Dim M_X As Single '横向缩放比例
Dim M_Y As Single '纵向缩放比例
On Error GoTo ine
If M_Form.WindowState = 1 Then '如果窗体最小化,什么也不做
GoTo inerr
End If
'M_FormHight = M_Form.Height '得到窗体高度
'M_FormWide = M_Form.Width '得到窗体高度
M_Form.AutoRedraw = True '允许窗体控件重画

If M_Form.Height < M_FormHight / 2 Then
M_Form.Height = M_FormHight / 2 '自动缩放到原来的1/2
End If
If M_Form.Width < M_FormWide / 2 Then
M_Form.Width = M_FormWide / 2 '自动缩放到原来的1/2
End If

'计算访缩比例
M_X = 1# * M_FormWide / M_Form.Width '得到宽比例
M_Y = 1# * M_FormHight / M_Form.Height '得到高比例
On Error Resume Next
For i = 1 To M_NumControls
If TypeOf M_Form.Controls(i - 1) Is Line Then
M_Form.Controls(i - 1).Top = Int(M_FControlSize(i).M_FTop / M_Y)
M_Form.Controls(i - 1).Left = Int(M_FControlSize(i).M_FLeft / M_X)
M_Form.Controls(i - 1).Width = Int(M_FControlSize(i).M_FWide / M_X)
M_Form.Controls(i - 1).hight = Int(M_FControlSize(i).M_FHight / M_Y)
Else
M_Form.Controls(i - 1).Top = Int(M_FControlSize(i).M_FTop / M_Y)
M_Form.Controls(i - 1).Left = Int(M_FControlSize(i).M_FLeft / M_X)
M_Form.Controls(i - 1).Width = Int(M_FControlSize(i).M_FWide / M_X)
M_Form.Controls(i - 1).Height = Int(M_FControlSize(i).M_FHight / M_Y)
M_Form.Controls(i - 1).FontSize = (Int(M_FControlSize(i).M_FFontSize / M_X) + Int(M_FControlSize(i).M_FFontSize / M_Y)) / 2
'M_Form.Controls(i - 1).RightMargin = Int(M_FControlSize(i).M_FRightMargin / M_Y)
End If
Next i

GoTo inerr
ine:
MsgBox Err.Description, , "错误信息"
inerr:
End Sub

回复
lzwGL 2003-05-18
每个控件多加个标签控件来进行监测控件和窗体的位置关系,属性设置成不可见。然后,在窗口变化事件里写相应的代码
回复
相关推荐
发帖
VB基础类
创建于2007-09-28

7522

社区成员

VB 基础类
申请成为版主
帖子事件
创建了帖子
2003-05-18 10:44
社区公告
暂无公告