2

背景:

我想将任何东西(最好是图像、形状)粘贴(如 [CTRL+V])到我单击或用鼠标悬停的位置(使用键激活时)。我不知道如何在单击的文档 (X, Y) 上获取位置

(Apache OpenOffice、SDraw-Document、OpenOffice BASIC 宏)

我需要的:

  • 提示/提示如何从文档上的鼠标单击/鼠标位置获取位置。(我需要哪个类、监听器、组件)

注意:如果给定的文件给了我单击的文档的 X+Y com.sun.star.awt.XMouseClickHandler,则类似 a 的东西会很完美。oEvent(也许你知道如何“激活” PopupTrigger?(com.sun.star.awt.MouseEvent))

到目前为止我的代码:

我尝试使用提到XMouseClickHandler的来获得 X+Y。遗憾的是,X+Y 指的是窗口的相对位置,而不是形状或文本在文档上的实际位置。

执行: My SubMain通过顶部的菜单按钮执行。然后单击任意位置将输出(通过 MsgBox)该单击的坐标。

唯一的问题:坐标相对于窗口的角,而不是文档的角。

Global gListener As Object

Sub Main
  gListener = CreateUnoListener("Listener_","com.sun.star.awt.XMouseClickHandler")
  ThisComponent.CurrentController.addMouseClickHandler(gListener) 
End Sub

Sub Listener_mousePressed(oMouseEvent) As Boolean
   ThisComponent.CurrentController.removeMouseClickHandler(gListener)

   Msg = "Position: "
   Msg = Msg & oMouseEvent.X & "/" & oMouseEvent.Y
   MsgBox(Msg)

   REM :: I want something like:
   REM :: Msg = "Position: " & oMouseEvent.PositionOnDocument.X
   REM :: Msg = Msg & "/" & oMouseEvent.PositionOnDocument.Y
   REM :: MsgBox(Msg)
End Sub

我的参考资料:

到目前为止,我所有的信息都来自官方参考资料/文档,因为我的所有搜索都没有发现任何有用的信息。

提前致谢。

4

2 回答 2

1

我终于找到了一种方法来获得鼠标点击的确切坐标(相对于文档)。我设法从底部的状态栏获取信息,它通常显示坐标(对我来说以厘米为单位)。

这是我现在用来获取位置(X / Y)的函数:

REM // Warning: If there is currently a selection, the returning Point will instead show the coordinates of the selection!
Sub GetMousePositionOnDocument as com.sun.star.awt.Point
  Dim aPosition As New com.sun.star.awt.Point
  Dim o1, o2, o3, o4, o5, o6

  REM // First get AccessibleContext of the Window of the active Frame of the Application
  o1 = StarDesktop.ActiveFrame.ContainerWindow.AccessibleContext

  REM // 7th AC of o1 is the StatusBar at the bottom;
  o2 = o1.GetAccessibleChild(6).AccessibleContext

  REM // 2nd AC of o2 is the Position + Size of the Selection (e.g: "10,95 / 14,980,00 x 0,00") 
  o3 = o2.GetAccessibleChild(1)
  o4 = o3.GetText()

  REM // Taking out only the coordinates from o4
  REM // TODO: Check for negatives (longer)
  o5 = LEFT(o4, 4)
  o6 = MID(o4, 8, 5)

  aPosition.X = o5
  aPosition.Y = o6

  REM // Return
  GetMousePositionOnDocument = aPosition
End Sub

注意:这个函数是在我之前Listener_mousePressed从上面调用的。

希望这也适用于其他人。

我是怎么找到的?

我花了很多时间在调试器中手动检查 ThisComponent 和 StarDesktop 的窗口的每个 ApplicationContext。

如果需要其他值,这是迭代 ThisDesktop 的起点。 ThisComponent.CurrentController.Frame.ComponentWindow.AccessibleContext

未来的改进

我“知道”GetAccessibleChild()-Function 的索引,因为我检查了调试器。当然有更好的方法o3,你不应该期望每个人都有相同的 AccessibleContext。

于 2017-06-10T00:45:40.410 回答
0

事实证明,DrawingDocumentDrawView服务有一个称为VisibleArea帮助的成员。从鼠标位置减去(实际上是添加,因为它们具有相反的符号)这些坐标以获得相对于文档的位置。

这是一个在鼠标单击位置创建矩形的示例。

Sub Listener_mousePressed(oMouseEvent) As Boolean
   ThisComponent.CurrentController.removeMouseClickHandler(gListener)

   xpos = (oMouseEvent.X + ThisComponent.VisibleArea.X / 25.4) / 100
   ypos = (oMouseEvent.Y + ThisComponent.VisibleArea.Y / 25.4) / 100
   Msg = "Position: " & xpos & "/" & ypos
   MsgBox(Msg)
   InsertProcessShape(xpos, ypos)
End Sub

Sub InsertProcessShape(xpos, ypos)
   Dim oDoc As Object
   Dim oDrawPage As Object
   Dim oShape As Object
   Dim shapeGeometry(0) as new com.sun.star.beans.PropertyValue
   Dim oSize As new com.sun.star.awt.Size

   oSize.width = 3000
   oSize.height = 1000

   oDoc = ThisComponent
   odrawPage = oDoc.DrawPages(0)
   oShape = oDoc.createInstance("com.sun.star.drawing.CustomShape")
   shapeGeometry(0).Name = "Type"
   shapeGeometry(0).Value = "flowchart-process"
   oDrawPage.add(oShape)
   oShape.CustomShapeGeometry = shapeGeometry
   oShape.Size = oSize

   ' Position the object
   IN_TO_CM = 2540  ' converts 1/1000 cm to inches
   Dim aPosition As New com.sun.star.awt.Point
   aPosition.X = xpos * IN_TO_CM
   aPosition.Y = ypos * IN_TO_CM
   oShape.setposition(aPosition)
End Sub

为了解决这个问题,我使用了xray。公式可能需要微调。但是,当我测试它时,矩形的左上角似乎大致位于鼠标单击的位置,正如预期的那样。

InsertProcessShape来自https://forum.openoffice.org/en/forum/viewtopic.php?f=45&t=46682以及Andrew Pitonyak 的 Macro Document清单 5.84 中的一些代码。

于 2017-06-06T10:47:16.713 回答