ALGO.bas 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. '// Algorithm 模块
  2. #If VBA7 Then
  3. '// For CorelDRAW X6-2023 62bit
  4. Public Declare PtrSafe Function i18n Lib "C:\TSP\lyvba.dll" (ByVal str As String, ByVal code As Long) As String
  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. Public LNG_CODE As Long
  33. Private Sub Test_Sort_ShapeRange()
  34. API.BeginOpt
  35. Dim sr As ShapeRange, ssr As ShapeRange
  36. Dim s As Shape
  37. Set sr = ActiveSelectionRange
  38. Set ssr = ShapeRange_To_Sort_Array(sr, topWt_left)
  39. '// s 调整次序
  40. For Each s In ssr
  41. s.OrderToFront
  42. Next s
  43. MsgBox "ShapeRange_SortItem:" & " " & topWt_left & " 枚举值"
  44. API.EndOpt
  45. End Sub
  46. Public Function X4_Sort_ShapeRange(ByRef sr As ShapeRange, ByRef Sort_By As SortItem) As ShapeRange
  47. Set X4_Sort_ShapeRange = ShapeRange_To_Sort_Array(sr, Sort_By)
  48. End Function
  49. '// 映射 ShapeRange 到 Array 然后调用 DLL库排序
  50. Private Function ShapeRange_To_Sort_Array(ByRef sr As ShapeRange, ByRef Sort_By As SortItem) As ShapeRange
  51. On Error GoTo ErrorHandler
  52. Dim sp As ShapeProperties
  53. Dim size As Long, ret As Long
  54. Dim s As Shape
  55. size = sr.Count
  56. Dim sr_Array() As ShapeProperties
  57. Dim ret_Array() As Long
  58. ReDim ret_Array(1 To size)
  59. ReDim sr_Array(1 To size)
  60. For Each s In sr
  61. sp.Item = sr.IndexOf(s)
  62. sp.StaticID = s.StaticID
  63. sp.lx = s.LeftX: sp.rx = s.RightX
  64. sp.by = s.BottomY: sp.ty = s.TopY
  65. sp.cx = s.CenterX: sp.cy = s.CenterY
  66. sp.sw = s.SizeWidth: sp.sh = s.SizeHeight
  67. sr_Array(sp.Item) = sp
  68. Next s
  69. '// 在VBA中数组的索引从1开始, 将数组的地址传递给函数需要Arr(1)方式
  70. '// C/C++ 函数定义 int __stdcall SortByItem(ShapeProperties* sr_Array, int size, SortItem Sort_By, int* ret_Array)
  71. '// sr_Array首地址,size 长度, Sort_By 排序方式, 返回数组 ret_Array
  72. ret = sort_byitem(sr_Array(1), size, Sort_By, ret_Array(1))
  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. Next i
  78. Set ShapeRange_To_Sort_Array = srcp
  79. End If
  80. ErrorHandler:
  81. End Function
  82. Private Sub Test_i18n()
  83. MsgBox i18n("Nodes", 2052)
  84. MsgBox i18n("Nodes", 1033)
  85. MsgBox i18n("Preset Property", 2052)
  86. End Sub
  87. Public Function Init_Translations(frm As UserForm, code As Long)
  88. Dim ctl As Variant: Dim en As String
  89. LNG_CODE = code
  90. For Each ctl In frm.Controls
  91. If TypeOf ctl Is MSForms.Label Or TypeOf ctl Is MSForms.CommandButton Or TypeOf ctl Is MSForms.ToggleButton Or TypeOf ctl Is MSForms.CheckBox Then
  92. If Not IsNull(ctl.Caption) And ctl.Caption <> "" Then
  93. en = ctl.Caption
  94. ctl.Caption = i18n(en, LNG_CODE)
  95. End If
  96. If Not IsNull(ctl.ControlTipText) And ctl.ControlTipText <> "" Then
  97. en = ctl.ControlTipText
  98. ctl.ControlTipText = i18n(en, LNG_CODE)
  99. End If
  100. End If
  101. Next ctl
  102. End Function