简体   繁体   English

如何处理DevExpress GalleryItem右键单击?

[英]How to Handle DevExpress GalleryItem Right Click?

Today, I was working on a program that required me to isolate the event caused when user right-clicks a DevExpress GalleryItem. 今天,我正在开发一个程序,该程序需要我隔离用户右键单击DevExpress GalleryItem时引起的事件。 Having consulted the forums and documentation, I was unsatisfied with the provided answers, "walking away" feeling there was a better answer. 在查阅了论坛和文档后,我对所提供的答案不满意,“走开”感到有更好的答案。 The problem is not that the documentation isn't there, the problem is that the documentation for this matter is woefully disjointed and requires a great deal of digging. 问题在于文档不存在,而在于此问题的文档严重分离,需要大量挖掘。 Again, even after consulting the documentation, I was left with the question: "How do I handle the right-click event on a GalleryControl containing GalleryItemGroup s?" 同样,即使查阅了文档,我仍然GalleryItemGroup一个问题:“如何处理包含GalleryItemGroupGalleryControl上的右键单击事件?”

My question was a bit more specific: "How do I access the GalleryItem located under the cursor?" 我的问题更加具体:“如何访问光标下方的GalleryItem ?”

NOTE: I am not providing code for the initial questions as I am answering the question in Q&A Format, ie I don't have the code that led me to the above question 注意:我没有以问答形式回答问题时提供最初问题的代码,即我没有导致我遇到上述问题的代码

DevExpress provides a tool that will be helpful in this situation. DevExpress提供了一种在这种情况下将很有帮助的工具。 Typically, click events are handled in the background by a Hit-Testing Algorithm , something which requires a bit of finesse and thinking (though the algorithm and logic behind the algorithm is relatively simple). 通常,点击事件是通过点击测试算法在后台处理的,这需要一些技巧和思考(尽管算​​法和算法的逻辑相对简单)。 Essentially, the code in question must capture the location of the cursor at the raising of the event and compare that to the location of the displayed objects - what you do with that comparison is the complicated bit. 本质上,所讨论的代码必须在事件发生时捕获光标的位置,并将其与显示的对象的位置进行比较-使用该比较所做的事情很复杂。

Fortunately for us, DevExpress removes the complicated bit, providing a RibbonHitInfo object, the documentation for which can be found here (I encourage you to click around a bit, it's pretty interesting stuff). 对我们来说幸运的是,DevExpress删除了复杂的部分,提供了RibbonHitInfo对象,该对象的文档可以在这里找到(我鼓励您单击一下,这是非常有趣的东西)。 The RibbonHitInfo provides a number of utilities that make what happens after the comparison of the click and the underlying objects quite easy. RibbonHitInfo提供了许多实用程序,使点击和底层对象的比较之后发生的事情变得非常容易。

The fully-qualified identifier for that class is: 该类的标准标识符为:

DevExpress.Xtrabars.Ribbon.ViewInfo.RibbonHitInfo

Capturing the Mouse Location 捕获鼠标位置

First, after the mouseclick event is fired, you must capture the location of the click (in this case, I am utilizing the MouseUp event. Two things to keep in mind for the below code: first (1), I am looking to isolate the right-click event; second (2), the mouse event function header format can be found throughout the internet (MSDN, DevExpress, StackOverflow, etc.) - that's where I pulled it from, initially. 首先,在触发mouseclick事件之后,您必须捕获单击的位置(在这种情况下,我正在利用MouseUp事件。以下代码需要牢记两点:第一(1),我希望隔离右键单击事件;其次(2),可以在整个Internet(MSDN,DevExpress,StackOverflow等)中找到鼠标事件函数标头格式,这是我最初从中获取它的地方。

Private Sub GalleryControl1_MouseRelease(ByVal sender As System.Object, ByVal MouseEvent As System.Windows.Forms.MouseEventArgs) Handles GalleryControl1.MouseUp

    If e.Button = Windows.Forms.MouseButtons.Right Then

        'Capture the location of the click:
        Dim PointOfClick As Point = e.Location

    End If

End Sub

The above code does two things: first, the If e.Button = statement evaluates which mouse control sent the event; 上面的代码有两件事:首先, If e.Button =语句评估哪个鼠标控件发送了事件; second, the X and Y coordinates of the mouse click are captured as a Point object for use, later. 其次,鼠标单击的X和Y坐标被捕获为Point对象供以后使用。

Instantiating the RibbonHitInfo Object 实例化RibbonHitInfo对象

Next, we must decide where our click is in relation to the objects we are interested in accessing (here, we are dealing with GalleryItem objects). 接下来,我们必须确定点击相对于我们要访问的对象(在这里,我们处理GalleryItem对象)的位置。 DevExpress provides the RibbonHitInfo class for this purpose. DevExpress为此提供了RibbonHitInfo类。 I'll display the code, then explain afterward. 我将显示代码,然后进行解释。

Note: The below code makes additions to the code, above. 注意:下面的代码是对上面代码的补充。

Private Sub GalleryControl1_MouseRelease(ByVal sender As System.Object, ByVal MouseEvent As System.Windows.Forms.MouseEventArgs) Handles GalleryControl1.MouseUp

    If e.Button = Windows.Forms.MouseButtons.Right Then

        'Capture the location of the click:
        Dim PointOfClick As Point = e.Location

        'Instantiate a RibbonHitInfo Object:
        Dim HitInfo As RibbonHitInfo = GalleryControl1.CalcHitInfo(PointOfClick)

        ' Decide Where the PointOfClick is in Relationship to Other Objects
        If HitInfo.InGalleryItem Then

            ' Do Something

        End If
    End If
End Sub

Again, the above code does two (2) things: first, the code instantiates the RibbonHitInfo object (explained in detail, below); 同样,以上代码做了两(2)件事:首先,该代码实例化RibbonHitInfo对象(下面详细说明); second, the code evaluates the location of the RibbonHitInfo object (really the PointOfClick ) by utilizing the available utilities - or, member functions. 其次,代码通过利用可用的实用程序(或成员函数)评估RibbonHitInfo对象(实际上是PointOfClick )的位置。

To instantiate HitInfo , I call the .CalcHitInfo() function, passing a Point object to the invoked function. 为了实例化HitInfo ,我调用.CalcHitInfo()函数,将Point对象传递给调用的函数。 .CalcHitInfo(aPoint) returns, "...[in this case] information on the GalleryControl's elements at that location". .CalcHitInfo(aPoint)返回“ ...(在这种情况下)有关该位置GalleryControl元素的信息”。 For me, it is easiest to think of a RibbonHitInfo object as a Point object that can interface with other objects behind it (that's a bit of a stretch, I know). 对我来说,最简单的方法是将RibbonHitInfo对象视为一个Point对象,该对象可以与其背后的其他对象进行交互(我知道这有点RibbonHitInfo )。

HitInfo has a number of member functions provided by DevExpress that allow us to evaluate where HitInfo is located; HitInfo具有DevExpress提供的许多成员函数,这些函数使我们能够评估HitInfo的位置。 in this case, I want to know if HitInfo (or PointOfClick , really) is within the a GalleryItem object contained by GalleryControl1 . 在这种情况下,我想知道HitInfo (或PointOfClick ,实际上)是否在GalleryControl1包含的GalleryItem对象中。 Hence, the code: If HitInfo.InGalleryItem Then . 因此,代码为: If HitInfo.InGalleryItem Then Through all of DevExpress' glory, the RibbonHitInfo object detects whether - at the time of click (?) - the click was made on top of another object; 通过所有DevExpress的荣耀, RibbonHitInfo对象检测-在单击(?)时是否单击了另一个对象。 again, in this case, a GalleryItem object. 同样,在这种情况下,是GalleryItem对象。

What To Do If HitInfo Is Where You Want It 如果HitInfo在您想要的地方该怎么办

If HitInfo is where you want it, then you can do what you want! 如果HitInfo是您想要的地方,那么您可以做自己想做的! However, for the sake of example (and, for my purposes, accessing the object underneath the click), I will provide some code. 但是,为了举例(出于我的目的,在单击下面访问对象),我将提供一些代码。 And, again, the below code is an addition to the above code (though this time it's more of a snippet). 同样,下面的代码是上面的代码的补充(尽管这次更多的是代码段)。

       ' Decide Where the PointOfClick is in Relationship to Other Objects
        If HitInfo.InGalleryItem Then

            ' To Access The Object Behind Hit Info:
            Dim ClickedGalleryItem As GalleryItem = HitInfo.GalleryItem


        End If

Here, I am able to access directly the GalleryItem object initially found "behind" the PointOfClick . 在这里,我能够直接访问GalleryItem最初发现“背后”的对象PointOfClick Both the returned GalleryItem object and ClickedGalleryItem an be used just as if they were a GalleryItem object -- because they are. 返回的GalleryItem对象和ClickedGalleryItem可以使用,就像它们是GalleryItem对象一样-因为它们是。 From here, you can access any of the properties, methods, etc. you wish. 从这里,您可以访问所需的任何属性,方法等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM