怎样使控件随窗体的大小而变化

pcube 2003-12-09 06:16:39
当窗体的大小变化时,如调整大小,最大化,还原时,怎样使窗体里的控件(如文本框)跟着窗体变大变小,请高手指教,谢谢
...全文
91 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
daisy8675 2003-12-09
  • 打赏
  • 举报
回复
上面只是個exp﹐一般只要Form_Activate和Form_Resize就可以做很好的控制了。

Good Luck
daisy8675 2003-12-09
  • 打赏
  • 举报
回复
Dim a(), g(1) As Single
Dim t As Integer, dt As Integer
Private Sub Check1_Click()
Form_Resize
End Sub
Private Sub Command1_Click()
If t = 0 Then t = 0: dt = 100: Timer1.Enabled = True
End Sub

Private Sub Form_Activate()
If g(0) = 0 Then '原始值只記錄一次
g(0) = Form1.ScaleWidth: g(1) = Form1.ScaleHeight '一開始表單的大小
ReDim a(Form1.Controls.Count - 1, 5)
j = 0
For Each i In Form1.Controls '記錄每個物件的資料
a(j, 0) = i.Name
On Error Resume Next '避免某些物件沒有指定的屬性而錯誤
a(j, 1) = i.Left: a(j, 2) = i.Top
a(j, 3) = i.Width: a(j, 4) = i.Height
a(j, 5) = i.FontSize
On Error GoTo 0 '取消錯誤處理
j = j + 1
Next i
t = 0: dt = 100: Timer1.Enabled = True
End If
End Sub

Private Sub Form_Resize()
If Form1.WindowState <> 1 And g(0) > 0 And g(1) > 0 Then
'重算物件的新位置
For i = 0 To Form1.Controls.Count - 1
Set b = Controls(a(i, 0))
On Error Resume Next
b.Left = a(i, 1) / g(0) * Form1.ScaleWidth
b.Top = a(i, 2) / g(1) * Form1.ScaleHeight
b.Width = a(i, 3) / g(0) * Form1.ScaleWidth
b.Height = a(i, 4) / g(1) * Form1.ScaleHeight
If Form1.ScaleWidth / g(0) < Form1.ScaleHeight / g(1) Then
b.FontSize = a(i, 5) / g(0) * Form1.ScaleWidth
Else
b.FontSize = a(i, 5) / g(1) * Form1.ScaleHeight
End If
On Error GoTo 0
If TypeOf b Is PictureBox Then
If Check1.Value = 1 Then b.PaintPicture b.Picture, 0, 0, b.ScaleWidth, b.ScaleHeight Else b.Cls
End If
Set b = Nothing
Next i
End If
End Sub
Private Sub Timer1_Timer()
t = t + Sgn(dt)
If t > 15 Then dt = -dt
If Form1.WindowState = 2 Then Form1.WindowState = 0: Timer1.Interval = 100: GoTo kk
If t = 16 Then Form1.WindowState = 2 - Form1.WindowState: Timer1.Interval = 1000: GoTo kk
Form1.Move (Screen.Width - Form1.Width - dt * 3) / 2, (Screen.Height - Form1.Height - dt) / 2
Form1.Move Form1.Left, Form1.Top, Form1.Width + dt * 3, Form1.Height + dt
kk:
If dt < 0 And t < 1 Then t = 0: Timer1.Enabled = False
End Sub

'後記:
'某些物件的長寬會有自動微調的功能,如 ListBox會以能顯示完整行的方式呈現
'另外如 PictureBox 的.picture並不像Image有Stretch縮放的功能,必要時須重繪!
'還要注意某些物件並不允許被重設位置!如 Timer,所以 On Error 的運用也是關鍵之一
'最好能限定表單長寬的最小值!避免某些物件因太小而不可見!

7,785

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧