背景:
我想在我单击或用鼠标悬停的位置粘贴任何东西(最好是图像,形状((使用鼠标使用键进行激活时(。我不知道如何获取文档(x,y(上的位置,我单击。
(Apache OpenOffice,Sdraw-Document,OpenOffice Basic Macro(
我需要的:
- 提示/提示如何从文档上的鼠标单击/鼠标位置获取位置。(我需要哪个类,听众,组件(
注释:如果给定的oEvent
给了我文档的X Y,那么com.sun.star.awt.XMouseClickHandler
之类的东西将是完美的。(也许您知道如何"激活" PopupTrigger
?(com.sun.star.awt.MouseEvent
((
到目前为止我的代码:
我尝试使用上述XMouseClickHandler
获取X Y。可悲的是,x y是指窗口的相对位置,而不是形状或文本在文档上具有的实际位置。
执行:我的子Main
是通过顶部的菜单按钮执行的。 然后单击任何地方(通过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
我的参考文献:
我所有的信息都来自到目前为止的官方参考/文档,因为我所有的搜索都没有找到任何有用的信息。
- class-list:http://api.libreoffice.org/docs/idl/ref/annotated.html您可以在这里查看二手类的文档(com.sun.sun.star.star.xmouseclicklicklickhandler,com.sun。star.awt.mouseevent(
- 关于听众的信息:https://help.libreoffice.org/3.6/basic/createunolistener_function_runtime
预先感谢。
我终于找到了一种方法获取鼠标单击的确切坐标(相对于文档(。我设法从底部的状态栏中获取信息,这通常显示坐标(对我而言(。
这是我现在用来获取位置(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
中从上方调用。
希望这也对他人也有效。
我如何找到它?
我花了很多时间检查thecomponent和stardesktop的每个应用程序context在调试器中手动。
如果需要其他值,这是通过ThisDesktop迭代的起点。 ThisComponent.CurrentController.Frame.ComponentWindow.AccessibleContext
未来改进
i"知道" getAccessibleChild((的索引 - 功能是因为我检查了调试器。肯定有更好的方法可以到达o3
,您不应该期望每个人都有相同的AccessibleContext。
事实证明,绘图documentDrawView服务具有一个名为 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
为了弄清楚这一点,我使用了XRARE。该公式可能需要进行微调。但是,矩形的左上角似乎在我测试时大致在鼠标被单击的地方进行。
。 InsertProcessShape
来自https://forum.openoffice.org/en/forum/viewtopic.php?f=45&t=46682,加上Andrewpitonyak宏观文档的5.84中的一些代码。