ALGO.bas 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. Attribute VB_Name = "ALGO"
  2. '// Algorithm 模块
  3. #If VBA7 Then
  4. '// For CorelDRAW X6-2023 62bit
  5. Private Declare PtrSafe Function sort_byitem Lib "C:\TSP\lyvba.dll" (ByRef sr_Array As ShapeProperties, ByVal size As Long, _
  6. ByVal Sort_By As SortItem, ByRef ret_Array As Long) As Long
  7. #Else
  8. '// For CorelDRAW X4 32bit
  9. Declare Function sort_byitem Lib "C:\TSP\lyvba32.dll" (ByRef sr_Array As ShapeProperties, ByVal size As Long, _
  10. ByVal Sort_By As SortItem, ByRef ret_Array As Long) As Long
  11. #End If
  12. Type ShapeProperties
  13. Item As Long '// ShapeRange.Item
  14. StaticID As Long '// Shape.StaticID
  15. lx As Double: rx As Double '// s.LeftX s.RightX s.BottomY s.TopY
  16. by As Double: ty As Double
  17. cx As Double: cy As Double '// s.CenterX s.CenterY s.SizeWidth s.SizeHeight
  18. sw As Double: sh As Double
  19. End Type
  20. Enum SortItem
  21. stlx
  22. strx
  23. stby
  24. stty
  25. stcx
  26. stcy
  27. stsw
  28. stsh
  29. Area
  30. topWt_left
  31. End Enum
  32. Private Sub Test_Sort_ShapeRange()
  33. API.BeginOpt
  34. Dim sr As ShapeRange, ssr As ShapeRange
  35. Dim s As Shape
  36. Set sr = ActiveSelectionRange
  37. Set ssr = ShapeRange_To_Sort_Array(sr, topWt_left)
  38. '// s 调整次序
  39. For Each s In ssr
  40. s.OrderToFront
  41. Next s
  42. MsgBox "ShapeRange_SortItem:" & " " & topWt_left & " 枚举值"
  43. API.EndOpt
  44. End Sub
  45. Public Function X4_Sort_ShapeRange(ByRef sr As ShapeRange, ByRef Sort_By As SortItem) As ShapeRange
  46. Set X4_Sort_ShapeRange = ShapeRange_To_Sort_Array(sr, Sort_By)
  47. End Function
  48. '// 映射 ShapeRange 到 Array 然后调用 DLL库排序
  49. Private Function ShapeRange_To_Sort_Array(ByRef sr As ShapeRange, ByRef Sort_By As SortItem) As ShapeRange
  50. ' On Error GoTo ErrorHandler
  51. Dim sp As ShapeProperties
  52. Dim size As Long, ret As Long
  53. Dim s As Shape
  54. size = sr.Count
  55. Dim sr_Array() As ShapeProperties
  56. Dim ret_Array() As Long
  57. ReDim ret_Array(1 To size)
  58. ReDim sr_Array(1 To size)
  59. For Each s In sr
  60. sp.Item = sr.IndexOf(s)
  61. sp.StaticID = s.StaticID
  62. sp.lx = s.LeftX: sp.rx = s.RightX
  63. sp.by = s.BottomY: sp.ty = s.TopY
  64. sp.cx = s.CenterX: sp.cy = s.CenterY
  65. sp.sw = s.SizeWidth: sp.sh = s.SizeHeight
  66. sr_Array(sp.Item) = sp
  67. Next s
  68. '// 在VBA中数组的索引从1开始, 将数组的地址传递给函数需要Arr(1)方式
  69. '// C/C++ 函数定义 int __stdcall SortByItem(ShapeProperties* sr_Array, int size, SortItem Sort_By, int* ret_Array)
  70. '// sr_Array首地址,size 长度, Sort_By 排序方式, 返回数组 ret_Array
  71. ret = sort_byitem(sr_Array(1), size, Sort_By, ret_Array(1))
  72. Debug.Print ret, size
  73. If ret = size Then
  74. Dim srcp As New ShapeRange, i As Integer
  75. For i = 1 To size
  76. srcp.Add sr(ret_Array(i))
  77. ' Debug.Print i
  78. Next i
  79. Set ShapeRange_To_Sort_Array = srcp
  80. End If
  81. ErrorHandler:
  82. End Function