【问题标题】:Force fit column of flexgridflexgrid的强制拟合列
【发布时间】:2013-01-21 06:55:08
【问题描述】:

在 vb6 中强制拟合 msflexgrid 列的最佳方法是什么?
因此,所有列都是可见的,并且它们占据网格的最大宽度


我已经尝试过这段代码,但它不适合网格内的最后一列,谁能建议可能是什么问题?

Public Sub **FlexGrid_AutoSizeColumns (** ByRef pGrid As MSHFlexGrid, _  
                                ByRef pForm As Form, _  
                                Optional ByVal pIncludeHeaderRows As Boolean = True, _  
                                Optional ByVal pAllowShrink As Boolean = True, _  
                                Optional ByVal pMinCol As Long = 0, _  
                                Optional ByVal pMaxCol As Long = -1, _  
                                Optional ByVal pBorderSize As Long = 8, _  
Optional fitToScreen As Boolean = False **)**  

Dim lngMinCol As Long, lngMaxCol As Long, lngCurrRow As Long  
Dim lngMinRow As Long, lngMaxRow As Long, lngCurrCol As Long  
Dim lngMaxWidth As Long, lngCurrWidth As Long  
Dim fntFormFont As StdFont  
Dim totalWidth As Integer  
totalWidth = 0  

Set fntFormFont = New StdFont  
Call CopyFont(pForm.Font, fntFormFont)  

Call CopyFont(pGrid.Font, pForm.Font)  

    With pGrid  
        lngMinCol = pMinCol  
        lngMaxCol = IIf(pMaxCol = -1, .Cols - 1, pMaxCol)  
        lngMinRow = IIf(pIncludeHeaderRows, 0, .FixedRows)  
        lngMaxRow = .Rows - 1  

        For lngCurrCol = lngMinCol To lngMaxCol  
            lngMaxWidth = IIf(pAllowShrink, 0, pForm.ScaleX(.ColWidth(lngCurrCol), vbTwips, pForm.ScaleMode))  

            For lngCurrRow = lngMinRow To lngMaxRow   '..find widest text (in scalemode of the form)
                lngCurrWidth = pForm.TextWidth(Trim(.TextMatrix(lngCurrRow, lngCurrCol)))
                If lngMaxWidth < lngCurrWidth Then lngMaxWidth = lngCurrWidth
            Next lngCurrRow

           lngMaxWidth = pForm.ScaleX(lngMaxWidth, pForm.ScaleMode, vbTwips)
           .ColWidth(lngCurrCol) = lngMaxWidth + (pBorderSize * Screen.TwipsPerPixelX)
           totalWidth = .ColWidth(lngCurrCol) + totalWidth

       Next lngCurrCol
   End With

Call CopyFont(fntFormFont, pForm.Font)

    If fitToScreen = True Then
        Dim i As Integer
        Dim gridWidth As Long
        gridWidth = pGrid.Width
        For i = 0 To pGrid.Cols - 1
            pGrid.ColWidth(i) = Int(gridWidth * pGrid.ColWidth(i) / totalWidth)
        Next
    End If  

结束子

【问题讨论】:

  • 你能展示你当前 msflexgrid 的图像吗?到目前为止你有没有尝试过?

标签: vba vb6 msflexgrid


【解决方案1】:

我能想到的一种方法是调整列的大小(具有可见性)以适应列(文本)中的最大宽度。该函数返回 0 或双精度值。只要返回的最大列宽不为零,我们就可以相应地调整当前网格列宽。如果为零,则保持不变。

Dim i, j,  as Integer
Dim maxWidth as Double
For i = 0 to MsFlexGrid1.Rows - 1
      For j = 0 to MsFlexGrid1.Cols - 1
             maxWidth = maxColWidth(j)
             If maxWidth > 0 then
                MsFlexGrid.ColWidth(j) = maxWidth
             End If
      Next j
Next i


Private Function maxColWidth(coNum as Integer) as Double
Dim i, Max as Integer
Max = 0
    With MsFlexGrid1
         For i = .FixedRows to .Rows-1
              If TextWidth(.TextMatrix(i, colNum)) > Max Then
                   Max = TextWidth(.TextMatrix(i, colNum))
              End If
         Next i    
         maxColWidth = Max
    End With
End Function

【讨论】:

  • @Vinay,我想在这里添加一件事,你可以做一个额外的检查。 如果您选择的max Width 小于最大列宽,那么您可以遵循最大列宽,否则您将列宽设置为选择的最大宽度 ?听起来不错? :)
【解决方案2】:

要将剩余空间分配到列上,请将其除以列数并将其添加到每一列

'1 form with :
'    1 msflexgrid : name=MSFlexGrid1
Option Explicit

Private Sub Form_Load()
  Dim intCol As Integer
  'example form and grid configuration
  Move 0, 0, 10000, 5000
  With MSFlexGrid1
    .FixedRows = 0
    .FixedCols = 0
    .Rows = 10
    .Cols = 10
    For intCol = 0 To .Cols - 1
      .ColWidth(intCol) = (intCol + 1) * 107
    Next intCol
  End With 'MSFlexGrid1
End Sub

Private Sub Form_Resize()
  MSFlexGrid1.Move 0, 0, ScaleWidth, ScaleHeight
End Sub

Private Sub MSFlexGrid1_Click()
  DistributeWidth
End Sub

Private Sub DistributeWidth()
  Dim intCol As Integer, intColSel As Integer
  Dim lngWidth As Long
  Dim lngRemaining As Long
  Dim lngExpand As Long
  With MSFlexGrid1
    intColSel = .Col                                          'remember selected column
    .Col = 0                                                  'select first column to ...
    lngWidth = .Width - .CellLeft * 2                         '... take flexgrid-borders into account
    .Col = intColSel                                          'select column again
    lngRemaining = lngWidth - InUse                           'calculate the remaining space
    If lngRemaining > 0 Then
      lngExpand = lngRemaining \ .Cols                        'distribute the remaining space over the columns
      For intCol = 0 To .Cols - 1
        .ColWidth(intCol) = .ColWidth(intCol) + lngExpand
      Next intCol
      lngExpand = lngRemaining Mod .Cols
      .ColWidth(.Cols - 1) = .ColWidth(.Cols - 1) + lngExpand 'since we are working with longs, apply the remaining fraction to the last column
    Else
      'what to do with lack of space? Shrink columns or expand grid or nothing?
    End If
  End With 'MSFlexGrid1
End Sub

Private Function InUse() As Long
'calculate how much of the gridwidth is already in use by the columns
  Dim intCol As Integer
  Dim lngInUse As Long
  With MSFlexGrid1
    lngInUse = 0
    For intCol = 0 To .Cols - 1
      lngInUse = lngInUse + .ColWidth(intCol)
    Next intCol
  End With 'MSFlexGrid1
  InUse = lngInUse
End Function

上面的例子并不总是完全填满这个区域,虽然我认为逻辑是正确的,我看不出有什么遗漏......

我猜这会产生与您所拥有的结果相似的结果?还是稍微好一点?

【讨论】:

    猜你喜欢
    • 2017-03-17
    • 2021-07-02
    • 1970-01-01
    • 2021-11-26
    • 1970-01-01
    • 1970-01-01
    • 2020-12-04
    • 1970-01-01
    相关资源
    最近更新 更多