首页上一页 1 下一页尾页 4 条记录 1/1页
vb开发经验技巧宝典-335(第172页)《控件随窗体大小而改变》 对sstab控件部分失效
发表在VB答疑区
2011-02-28
是否精华
是
否
版块置顶:
是
否
设置时间:
非永久
永久
起始时间:
结束时间:
是否扣分:
是
否
比如:窗体上画一个sstab1控件,有两个选项卡:tab0,tab1.分别画上frame0,frame1.
如果,窗体缩放的时候停留在tab0,则只能缩放frame0.如果切换选项卡到tab1上,不能看到其上的控件frame1.
本论坛好像不能上传附件,我把这个例子放到了如下可以下载的地址:
http://bbs.ytxxb.cn/attachment.jsp?aid=733
这个问题耗费了我一天的时间才发现,但是不知道如何更改请管理员抽空改下吧。
如果,窗体缩放的时候停留在tab0,则只能缩放frame0.如果切换选项卡到tab1上,不能看到其上的控件frame1.
本论坛好像不能上传附件,我把这个例子放到了如下可以下载的地址:
http://bbs.ytxxb.cn/attachment.jsp?aid=733
这个问题耗费了我一天的时间才发现,但是不知道如何更改请管理员抽空改下吧。
精彩评论 4
2011-03-01
板凳
出现该错误的原因在于 如果某一控件的容器是SSTab,那么当它不显示的时候,它的LEFT属性值为负值。
在AI过程中计算出的比率也是个负值,导致之后计算出来的LEFT属性都是负值。LEFT属性值为负值,控件当然显示不出来。
修改后的主要代码:
Const SPI_GETWORKAREA = 48
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" _
(ByVal uAction As Long, ByVal uParam As Long, lpvParam As Any, _
ByVal fuWinIni As Long) As Long
Dim ap() As cp
Sub ai()
Dim i As Integer
If Controls.Count > 0 Then
'如果窗体中包括菜单,这时程序将出错
'解决办法:在代码前加"On Error Resume Next"
For i = 0 To Controls.Count - 1
With ap(i)
If Controls(i).Container Is Me Then
.wp = Controls(i).Width / Controls(i).Container.ScaleWidth
.hp = Controls(i).Height / Controls(i).Container.ScaleHeight
.lp = Controls(i).Left / Controls(i).Container.ScaleWidth
.tp = Controls(i).Top / Controls(i).Container.ScaleHeight
ElseIf Controls(i).Container Is SSTab1 Then
.wp = Controls(i).Width / Controls(i).Container.Width
.hp = Controls(i).Height / (Controls(i).Container.Height - Controls(i).Container.TabHeight)
.lp = (Controls(i).Container.Width - Controls(i).Width) / 2 / Controls(i).Container.Width
.tp = Controls(i).Top / (Controls(i).Container.Height - Controls(i).Container.TabHeight)
End If
End With
Next i
End If
End Sub
Private Sub Form_Load()
If Controls.Count > 0 Then
ReDim ap(0 To Controls.Count - 1)
ai
Dim lRet As Long
Dim apiRECT As RECT
lRet = SystemParametersInfo(SPI_GETWORKAREA, vbNull, apiRECT, 0)
If lRet Then
If Me.Width > apiRECT.Right / 72 * 1440 - 180 Then
MsgBox "sdfds"
Me.Width = apiRECT.Right / 72 * 1440 - 180
Me.Width = Me.Width - 180
End If
If Me.Height > apiRECT.Bottom / 72 * 1440 - 180 Then
Me.Height = apiRECT.Bottom / 72 * 1440 - 180
Me.Height = Me.Height - 180
End If
End If
End If
End Sub
Private Sub Form_Resize()
If Controls.Count > 0 Then
Dim i As Integer
For i = 0 To Controls.Count - 1
If Controls(i).Container Is Me Then
Controls(i).Move ap(i).lp * Form1.ScaleWidth, ap(i).tp * Form1.ScaleHeight, ap(i).wp * Form1.ScaleWidth, ap(i).hp * Form1.ScaleHeight
ElseIf Controls(i).Container Is SSTab1 Then
With Controls(i)
.Left = ap(i).lp * Controls(i).Container.Width
.Top = ap(i).tp * (Controls(i).Container.Height - Controls(i).Container.TabHeight)
.Width = ap(i).wp * .Container.Width
.Height = ap(i).hp * (.Container.Height - .Container.TabHeight)
.Visible = False
If Controls(i).Container.Tab = 0 Then
Frame1.Visible = False
Frame0.Visible = True
Else
Frame0.Visible = False
Frame1.Visible = True
End If
End With
End If
Next i
End If
End Sub
Private Sub SSTab1_Click(PreviousTab As Integer)
If SSTab1.Tab = 0 Then
Frame1.Visible = False
Frame0.Visible = True
Else
Frame0.Visible = False
Frame1.Visible = True
End If
End Sub
在AI过程中计算出的比率也是个负值,导致之后计算出来的LEFT属性都是负值。LEFT属性值为负值,控件当然显示不出来。
修改后的主要代码:
Const SPI_GETWORKAREA = 48
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" _
(ByVal uAction As Long, ByVal uParam As Long, lpvParam As Any, _
ByVal fuWinIni As Long) As Long
Dim ap() As cp
Sub ai()
Dim i As Integer
If Controls.Count > 0 Then
'如果窗体中包括菜单,这时程序将出错
'解决办法:在代码前加"On Error Resume Next"
For i = 0 To Controls.Count - 1
With ap(i)
If Controls(i).Container Is Me Then
.wp = Controls(i).Width / Controls(i).Container.ScaleWidth
.hp = Controls(i).Height / Controls(i).Container.ScaleHeight
.lp = Controls(i).Left / Controls(i).Container.ScaleWidth
.tp = Controls(i).Top / Controls(i).Container.ScaleHeight
ElseIf Controls(i).Container Is SSTab1 Then
.wp = Controls(i).Width / Controls(i).Container.Width
.hp = Controls(i).Height / (Controls(i).Container.Height - Controls(i).Container.TabHeight)
.lp = (Controls(i).Container.Width - Controls(i).Width) / 2 / Controls(i).Container.Width
.tp = Controls(i).Top / (Controls(i).Container.Height - Controls(i).Container.TabHeight)
End If
End With
Next i
End If
End Sub
Private Sub Form_Load()
If Controls.Count > 0 Then
ReDim ap(0 To Controls.Count - 1)
ai
Dim lRet As Long
Dim apiRECT As RECT
lRet = SystemParametersInfo(SPI_GETWORKAREA, vbNull, apiRECT, 0)
If lRet Then
If Me.Width > apiRECT.Right / 72 * 1440 - 180 Then
MsgBox "sdfds"
Me.Width = apiRECT.Right / 72 * 1440 - 180
Me.Width = Me.Width - 180
End If
If Me.Height > apiRECT.Bottom / 72 * 1440 - 180 Then
Me.Height = apiRECT.Bottom / 72 * 1440 - 180
Me.Height = Me.Height - 180
End If
End If
End If
End Sub
Private Sub Form_Resize()
If Controls.Count > 0 Then
Dim i As Integer
For i = 0 To Controls.Count - 1
If Controls(i).Container Is Me Then
Controls(i).Move ap(i).lp * Form1.ScaleWidth, ap(i).tp * Form1.ScaleHeight, ap(i).wp * Form1.ScaleWidth, ap(i).hp * Form1.ScaleHeight
ElseIf Controls(i).Container Is SSTab1 Then
With Controls(i)
.Left = ap(i).lp * Controls(i).Container.Width
.Top = ap(i).tp * (Controls(i).Container.Height - Controls(i).Container.TabHeight)
.Width = ap(i).wp * .Container.Width
.Height = ap(i).hp * (.Container.Height - .Container.TabHeight)
.Visible = False
If Controls(i).Container.Tab = 0 Then
Frame1.Visible = False
Frame0.Visible = True
Else
Frame0.Visible = False
Frame1.Visible = True
End If
End With
End If
Next i
End If
End Sub
Private Sub SSTab1_Click(PreviousTab As Integer)
If SSTab1.Tab = 0 Then
Frame1.Visible = False
Frame0.Visible = True
Else
Frame0.Visible = False
Frame1.Visible = True
End If
End Sub
2011-03-04
4L
在您的代码基础上做了微小的改动就ok了。粘贴下来,共享。
Const SPI_GETWORKAREA = 48
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" _
(ByVal uAction As Long, ByVal uParam As Long, lpvParam As Any, _
ByVal fuWinIni As Long) As Long
Dim ap() As cp
Sub ai()
Dim i As Integer
If Controls.Count > 0 Then
'如果窗体中包括菜单,这时程序将出错
'解决办法:在代码前加"On Error Resume Next"
For i = 0 To Controls.Count - 1
With ap(i)
If Controls(i).Container Is SSTab1 Then
.wp = Controls(i).Width / Controls(i).Container.Width
.hp = Controls(i).Height / (Controls(i).Container.Height - Controls(i).Container.TabHeight)
.lp = (Controls(i).Container.Width - Controls(i).Width) / 2 / Controls(i).Container.Width
.tp = Controls(i).Top / (Controls(i).Container.Height - Controls(i).Container.TabHeight)
Else
.wp = Controls(i).Width / Form1.ScaleWidth
.hp = Controls(i).Height / Form1.ScaleHeight
.lp = Controls(i).Left / Form1.ScaleWidth
.tp = Controls(i).Top / Form1.ScaleHeight
End If
End With
Next i
End If
End Sub
Private Sub Form_Load()
If Controls.Count > 0 Then
ReDim ap(0 To Controls.Count - 1)
ai
Dim lRet As Long
Dim apiRECT As RECT
lRet = SystemParametersInfo(SPI_GETWORKAREA, vbNull, apiRECT, 0)
If lRet Then
If Me.Width > apiRECT.Right / 72 * 1440 - 180 Then
MsgBox "sdfds"
Me.Width = apiRECT.Right / 72 * 1440 - 180
Me.Width = Me.Width - 180
End If
If Me.Height > apiRECT.Bottom / 72 * 1440 - 180 Then
Me.Height = apiRECT.Bottom / 72 * 1440 - 180
Me.Height = Me.Height - 180
End If
End If
End If
End Sub
Private Sub Form_Resize()
If Controls.Count > 0 Then
Dim i As Integer
For i = 0 To Controls.Count - 1
If Controls(i).Container Is SSTab1 Then
With Controls(i)
.Left = ap(i).lp * Controls(i).Container.Width
.Top = ap(i).tp * (Controls(i).Container.Height - Controls(i).Container.TabHeight)
.Width = ap(i).wp * .Container.Width
.Height = ap(i).hp * (.Container.Height - .Container.TabHeight)
.Visible = False
If Controls(i).Container.Tab = 0 Then
Frame1.Visible = False
Frame0.Visible = True
Else
Frame0.Visible = False
Frame1.Visible = True
End If
End With
Else
Controls(i).Move ap(i).lp * Form1.ScaleWidth, ap(i).tp * Form1.ScaleHeight, ap(i).wp * Form1.ScaleWidth, ap(i).hp * Form1.ScaleHeight
End If
Next i
End If
End Sub
Private Sub SSTab1_Click(PreviousTab As Integer)
If SSTab1.Tab = 0 Then
Frame1.Visible = False
Frame0.Visible = True
Else
Frame0.Visible = False
Frame1.Visible = True
End If
End Sub
Const SPI_GETWORKAREA = 48
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" _
(ByVal uAction As Long, ByVal uParam As Long, lpvParam As Any, _
ByVal fuWinIni As Long) As Long
Dim ap() As cp
Sub ai()
Dim i As Integer
If Controls.Count > 0 Then
'如果窗体中包括菜单,这时程序将出错
'解决办法:在代码前加"On Error Resume Next"
For i = 0 To Controls.Count - 1
With ap(i)
If Controls(i).Container Is SSTab1 Then
.wp = Controls(i).Width / Controls(i).Container.Width
.hp = Controls(i).Height / (Controls(i).Container.Height - Controls(i).Container.TabHeight)
.lp = (Controls(i).Container.Width - Controls(i).Width) / 2 / Controls(i).Container.Width
.tp = Controls(i).Top / (Controls(i).Container.Height - Controls(i).Container.TabHeight)
Else
.wp = Controls(i).Width / Form1.ScaleWidth
.hp = Controls(i).Height / Form1.ScaleHeight
.lp = Controls(i).Left / Form1.ScaleWidth
.tp = Controls(i).Top / Form1.ScaleHeight
End If
End With
Next i
End If
End Sub
Private Sub Form_Load()
If Controls.Count > 0 Then
ReDim ap(0 To Controls.Count - 1)
ai
Dim lRet As Long
Dim apiRECT As RECT
lRet = SystemParametersInfo(SPI_GETWORKAREA, vbNull, apiRECT, 0)
If lRet Then
If Me.Width > apiRECT.Right / 72 * 1440 - 180 Then
MsgBox "sdfds"
Me.Width = apiRECT.Right / 72 * 1440 - 180
Me.Width = Me.Width - 180
End If
If Me.Height > apiRECT.Bottom / 72 * 1440 - 180 Then
Me.Height = apiRECT.Bottom / 72 * 1440 - 180
Me.Height = Me.Height - 180
End If
End If
End If
End Sub
Private Sub Form_Resize()
If Controls.Count > 0 Then
Dim i As Integer
For i = 0 To Controls.Count - 1
If Controls(i).Container Is SSTab1 Then
With Controls(i)
.Left = ap(i).lp * Controls(i).Container.Width
.Top = ap(i).tp * (Controls(i).Container.Height - Controls(i).Container.TabHeight)
.Width = ap(i).wp * .Container.Width
.Height = ap(i).hp * (.Container.Height - .Container.TabHeight)
.Visible = False
If Controls(i).Container.Tab = 0 Then
Frame1.Visible = False
Frame0.Visible = True
Else
Frame0.Visible = False
Frame1.Visible = True
End If
End With
Else
Controls(i).Move ap(i).lp * Form1.ScaleWidth, ap(i).tp * Form1.ScaleHeight, ap(i).wp * Form1.ScaleWidth, ap(i).hp * Form1.ScaleHeight
End If
Next i
End If
End Sub
Private Sub SSTab1_Click(PreviousTab As Integer)
If SSTab1.Tab = 0 Then
Frame1.Visible = False
Frame0.Visible = True
Else
Frame0.Visible = False
Frame1.Visible = True
End If
End Sub