简体   繁体   中英

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. 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?"

My question was a bit more specific: "How do I access the GalleryItem located under the cursor?"

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. 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). The RibbonHitInfo provides a number of utilities that make what happens after the comparison of the click and the underlying objects quite easy.

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.

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; second, the X and Y coordinates of the mouse click are captured as a Point object for use, later.

Instantiating the RibbonHitInfo Object

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). DevExpress provides the RibbonHitInfo class for this purpose. 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); second, the code evaluates the location of the RibbonHitInfo object (really the PointOfClick ) by utilizing the available utilities - or, member functions.

To instantiate HitInfo , I call the .CalcHitInfo() function, passing a Point object to the invoked function. .CalcHitInfo(aPoint) returns, "...[in this case] information on the GalleryControl's elements at that location". 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).

HitInfo has a number of member functions provided by DevExpress that allow us to evaluate where HitInfo is located; in this case, I want to know if HitInfo (or PointOfClick , really) is within the a GalleryItem object contained by GalleryControl1 . Hence, the code: 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; again, in this case, a GalleryItem object.

What To Do If HitInfo Is Where You Want It

If HitInfo is where you want it, then you can do what you want! 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 . Both the returned GalleryItem object and ClickedGalleryItem an be used just as if they were a GalleryItem object -- because they are. From here, you can access any of the properties, methods, etc. you wish.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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