Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Detecting when mouse cursor is outside the canvas

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Detecting when mouse cursor is outside the canvas

    Hi all !

    I want to detect when the cursor is over the canvas (chart) of a chart control or not . I know how to get the cursor coordinates (x,y) and the canvas X,Y,H W. But with that info , I can`t find out if the mouse is or isn't in the limits of the canvas because tha MAX value of the coordinates are the limits of the canva

    Any clue about how to get that?

    Thanks

    #2
    Hello hbolorin,

    Thank you for your post.

    Are you looking to detect if a mouse click is detected within the chart control or only the mouse cursor hovering within vs. outside of the chart control? If you are looking to detect a mouse click, you could use IsInHitTest:


    You can get the x-coordinates in pixels for the beginning and the end of the chart canvas using CanvasLeft and CanvasRight:



    You could use GetYByValue to get the y-pixel coordinate for minValue and maxValue





    Next, after obtaining the x and y pixels for the canvas left, canvas right, minvalue, and maxvalue, you should be able to determine if a certain coordinate from the cursor is within that range or not.

    I hope this information has been useful. Please let us know if we may be of further assistance.

    Comment


      #3
      Hello, Emily,

      Thank you for the answer.
      I want to know only if the mouse cursor is hovering over the chart control o not.
      I know all the variables and methods you wrote in your answer.My problem is that I don't know how to combine all of them to determinine if the cursor is hovering over the control panel or not.
      I have copied this code from a ZIP example I found in another post. With this example, I know the cursor coordinates inside the chart control.

      Code:
              void ChartPanel_MouseMove (object sender, System.Windows.Input.MouseEventArgs e)
              {
                  TriggerCustomEvent(o =>
                  {
                      ClearOutputWindow();
      
                      double curPosX = e.GetPosition(ChartPanel).X;
                      double curPosY = e.GetPosition(ChartPanel).Y;
                      double price = Instrument.MasterInstrument.RoundToTickSize(ChartScale.GetValueByY((float)curPosY));
                      DateTime time = ChartControl.GetTimeByX((int)curPosX);
      
                      Print(String.Format("X: {0} Y: {1}", curPosX, curPosY));
                      Print(String.Format("PriceLevel: {0}", price));
                      Print(String.Format("Time: {0}", time));
                      Print(String.Format("Price Above Current Market Price: {0}", (price > Close[0])));
                  }, null);
              }​
      But the main problem I see is when the cursor hover outside the control panel. the getPosition function returns 0 or the Maxvalue of the canvas. Let's say that the X canvas size is from 0 to 950 , when the cursor is hovering out of it, the GetPosition X has value 0 or 950. Same for Y coordinate. So I can't find out when the cursor coordiantes are outside the limits.

      So I I want to know if the mouse is not hovering over the chart control, I should be able to get coordinates outside the control chart , I think. So I can compare with the limits of the chart. But I don´t know how to do it.

      Or maybe there is another way.

      Could you please give me a code example ?

      Thanks

      Comment


        #4
        hbolorin You have essentially answered your own question, I think. Why not just treat 0 and 950 as indicative of being at (or beyond) the boundary of the chart? After all, unless you want to know exactly where outside the chart the cursor is, it really is irrelevant what the values are outside of that range. At most, you lose the slimmest of border areas when the mouse is right on the edge, but surely that is not a major impediment? Anything in the range 1-949 can be considered within the chart, and 0 or 950 is outside it. Just a thought.

        Thanks.
        Multi-Dimensional Managed Trading
        jeronymite
        NinjaTrader Ecosystem Vendor - Mizpah Software

        Comment


          #5
          Hello hbolorin,

          Thank you for your patience.

          Aside from the ideas that jeronymite mentioned, you should also be able to take advantage of the Control.MouseEnter and Control.MouseLeave events. Since you are working with the area of the chart, you could test out using ChartControl.MouseEnter and ChartControl.MouseLeave. More information may be found at the following publicly available links from the Microsoft Documentation:



          Please let us know if we may be of further assistance.

          Comment


            #6
            Thank you for the answers.

            Emily, that is perfect. It is what I was looking for. Jeronymate, godd idea but Emlily´s is better

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            649 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            370 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            109 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            574 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            576 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X