我做了一个组合控件,其中子控子有:pixturebox两个,label三个,Image5,MSFlexGrid两个。
我在UserControl_InitProperties()和UserControl_Resize()中已经都它们的相对位置和大小都做好了调整。在以IE做了容器进行测试的时候,一切也很正常。但我把它Make成.ocx后用在另一个程序中的时候,就有问题,其上的子控件的大小和位置乱成一团糟。请问各位大侠是什么原因。应该怎么解决。谢谢!!!
我在UserControl_InitProperties()和UserControl_Resize()中已经都它们的相对位置和大小都做好了调整。在以IE做了容器进行测试的时候,一切也很正常。但我把它Make成.ocx后用在另一个程序中的时候,就有问题,其上的子控件的大小和位置乱成一团糟。请问各位大侠是什么原因。应该怎么解决。谢谢!!!
能想到的原因只能是这个了
UserControl_Resize()
UserControl_InitProperties()
'Initialize Properties for User Control
Private Sub UserControl_InitProperties()
m_Page = m_def_Page
m_PageCount = m_def_PageCount
m_PageSize = m_def_PageSize
'adjust the combine controls' position and size
MarginWd = imgPageUp(0).Width
cellWd = msFlexGrid.CellWidth
cellHi = msFlexGrid.CellHeight
gridLineWd = msFlexGrid.GridLineWidth * Screen.TwipsPerPixelY
'计算页大小及总页数
m_PageSize = picMask.Height / (CellHeight + gridLineWd) - 0.5
m_PageCount = msFlexGrid.Rows / m_PageSize + 0.5
lblPage.Caption = "页:" & m_Page & "/" & m_PageCount
With lblPrompt
.Left = 100
.Top = 50
End With
With lblPage
.Left = ScaleWidth - .Width - MarginWd - 100
.Top = 50
End With
With imgLeft
.Left = 0
.Top = 0
End With
With imgRight
.Left = ScaleWidth - .Width - MarginWd
.Top = 0
End With
With imgMiddle
.Top = 0
.Left = imgLeft.Width - 50
.Width = imgRight.Left - imgLeft.Width + 100
End With
With msFlexFix
.Left = 0
.Top = imgLeft.Height
.Width = ScaleWidth - MarginWd
End With
With lblBack
.Left = 0
.Width = ScaleWidth - MarginWd
.Top = imgLeft.Height + msFlexFix.Height
.Height = ScaleHeight - .Top
End With
'picMask为表格的真正显示区的大小
With picMask
.Left = LINEWD
.Width = lblBack.Width - LINEWD - LINEWD
.Top = lblBack.Top + LINEWD
.Height = lblBack.Height - LINEWD - LINEWD
End With
'使msFlexGrid足够大,以使不需要滚动条便能显示所有内容
With msFlexGrid
.Left = 0
.Top = 0
.Width = (cellWd + gridLineWd) * .Cols
.Height = (cellHi + gridLineWd) * .Rows
End With
'为msFlexGrid的容器,与其大小一样
With picBig
.Left = 0
.Top = 0
.Width = msFlexGrid.Width
.Height = msFlexGrid.Height
End With
'设置PageDown和PageUp的位置
Dim i As Integer
For i = 0 To 2
With imgPageDown(i)
.Left = lblBack.Width
.Top = ScaleHeight - .Height
.Visible = False
End With
With imgPageUp(i)
.Left = lblBack.Width
.Top = imgPageDown(0).Top - .Height + 170
.Visible = False
End With
Next i
'不可用按钮
imgPageDown(0).Enabled = False
imgPageUp(0).Enabled = False
If m_PageCount > 1 Then
'标识为正常状态的Image显示
imgPageDown(1).Visible = True
'标识为不可用状态的Image显示
imgPageUp(0).Visible = True
imgPageUp(0).Enabled = False
End If
End SubPrivate Sub UserControl_Resize()
'adjust controls' size
On Error Resume Next
With lblPage
.Left = ScaleWidth - .Width - MarginWd - 100
End With
With imgRight
.Left = ScaleWidth - .Width - MarginWd
End With
With imgMiddle
.Width = imgRight.Left - imgLeft.Width + 100
End With
With msFlexFix
.Width = ScaleWidth - MarginWd
End With
With lblBack
.Width = ScaleWidth - MarginWd
.Height = ScaleHeight - .Top
End With
'picMask为表格的真正显示区的大小
With picMask
.Width = lblBack.Width - LINEWD - LINEWD
.Height = lblBack.Height - LINEWD - LINEWD
End With
'使msFlexGrid足够大,以使不需要滚动条便能显示所有内容
With msFlexGrid
.Width = (cellWd + gridLineWd) * .Cols
.Height = (cellHi + gridLineWd) * .Rows
End With
'为msFlexGrid的容器,与其大小一样
With picBig
.Width = msFlexGrid.Width
.Height = msFlexGrid.Height
End With
'设置PageDown和PageUp的位置
Dim i As Integer
For i = 0 To 2
With imgPageDown(i)
.Left = lblBack.Width
.Top = ScaleHeight - .Height
End With
With imgPageUp(i)
.Left = lblBack.Width
.Top = imgPageDown(0).Top - .Height + 170
End With
Next i
If m_PageCount > 1 Then
'标识为正常状态的Image可见
imgPageDown(1).Visible = True
'标识为不可用状态的Image可见
imgPageUp(0).Visible = True
Else
'标识为正常状态的Image不可见
imgPageDown(1).Visible = False
'标识为不可用状态的Image不可见
imgPageUp(0).Visible = False
End If
RaiseEvent Resize
End Sub
UserControl_ReadProperties事件里
或UserControl_Initialize事件里
或UserControl_Resize事件里如果不行,三个事件里都放试试。此外:UserControl_InitProperties事件仅控件放在窗体上的一瞬间产生,其它时间就不会产生了。
我一般在UserControl_Control_Initialize里处理类似问题