http://community.csdn.net/Expert/topic/4731/4731569.xml?temp=.9679834

解决方案 »

  1.   

    Private Function Sort(ByVal N As Long, TextList As Object)
        Dim id() As Long
        ReDim id(N) As Long
        For i = 0 To N - 1
            id(i) = i
        Next i
        
        Dim minj As Long
        For i = 0 To N - 2
            minj = i
            For j = i + 1 To N - 1
                If TextList(id(j)).Top < TextList(id(minj)).Top Then
                    minj = j
                End If
            Next j
            j = id(i)
            id(i) = id(minj)
            id(minj) = j
        Next i
        
        Dim curWidth As Long
        curWidth = 1000
        Dim maxWidth As Long
        maxWidth = 8000 '限制最大宽度,可以作为参数
        Dim maxHeight As Long
        maxHeight = 6000 '限制最大高度,可以作为参数
        Dim used() As Boolean
        ReDim used(N) As Boolean
        For i = 0 To N - 1
            used(i) = False
            TextList(i).Left = 0
            TextList(i).Width = curWidth
        Next i
        Dim numUsed As Long
        numUsed = 0
        
        Dim curTop As Long
        curTop = 0
        Dim curLeft As Long
        curLeft = 0
        
        Dim found As Boolean
        Do While numUsed < N
            found = False
            For i = 0 To N - 1
                If Not used(id(i)) And TextList(id(i)).Top >= curTop Then
                    found = True
                    used(id(i)) = True
                    TextList(id(i)).Left = curLeft
                    TextList(id(i)).Width = curWidth
                    curTop = TextList(id(i)).Top + TextList(id(i)).Height
                    numUsed = numUsed + 1
                    Exit For
                End If
            Next i
            If Not found Then
                curTop = 0
                curLeft = curLeft + curWidth
            End If
        Loop
        
        Dim aspect As Double
        aspect = maxWidth / (curLeft + curWidth)
        Debug.Print aspect
        For i = 0 To N - 1
            TextList(i).Left = TextList(i).Left * aspect
            TextList(i).Width = TextList(i).Width * aspect
        Next i
        aspect = maxHeight / (TextList(id(N - 1)).Top + TextList(id(N - 1)).Height)
        For i = 0 To N - 1
            TextList(i).Top = TextList(i).Top * aspect
            TextList(i).Height = TextList(i).Height * aspect
        Next i
    End Function