ALGO.bas 3.6 KB

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