10211 : How can I draw a rectangle on the TimeItemArea to a group of timeitems?

Question

How can I draw a rectangle on the TimeItemArea to  a group of timeitems?

Answer

The answer below explains how to use the user drawing mechanism to achieve a square that is dragged out with a mouse click… Once you have this functioning you want to iterate the time items and see if they are inside the rectangle, if so select them. That part is not shown in this sample…

      // What if I want to draw a square in time item area, starting from a click point and following the mouse?
      // The solution is marked with “#1”

      //#1
      private bool mouseisCurrentlyDown=false;
      private Point mousewaspressedat=new Point(0,0);
      private Point mouseisat=new Point(0,0);
      private void gantt1_OnTimeItemAreaMouseDown(object sender, MouseEventArgs e)
      {
        //#1
        mouseisCurrentlyDown=true;
        mousewaspressedat.X=e.X;
        mousewaspressedat.Y=e.Y;
      }

      private void gantt1_OnTimeItemAreaMouseUp(object sender, MouseEventArgs e)
      {
        //#1
        mouseisCurrentlyDown=false;
        gantt1.TimeItemArea.Invalidate();
      }

      private void gantt1_OnTimeItemAreaMouseMove(object sender, MouseEventArgs e)
      {
        //#1
        mouseisat.X=e.X;
        mouseisat.Y=e.Y;
        if (mouseisCurrentlyDown && gantt1.MouseMoveKind==MouseMoveKind.none)
        {
          // We do not want the frame to be drawn on normal Gantt actions, only when the Gantt is not doing anything else
          gantt1.TimeItemArea.Invalidate();
        }
      }

      private void gantt1_OnTimeItemAreaPaintForeground(OffscreenDraw aOffscreenDraw, OffscreenDrawArgs e)
      {
        // You can easily draw in the back- or foreground of the Grid, DateScaler or TimeItemArea
        e.G.FillRectangle(new SolidBrush(Color.Black),aOffscreenDraw.CalcLeft+100,aOffscreenDraw.CalcTop+100,90,90);
        e.G.DrawString(“Front”,new Font(“Helvetica”,14),new SolidBrush(Color.Red),aOffscreenDraw.CalcLeft+100,aOffscreenDraw.CalcTop+100);

        //#1
        if (mouseisCurrentlyDown && gantt1.MouseMoveKind==MouseMoveKind.none)
        {
          e.G.DrawRectangle(new Pen(Color.Green),mousewaspressedat.X,mousewaspressedat.Y,mouseisat.X-mousewaspressedat.X,mouseisat.Y-mousewaspressedat.Y);
        }
      }

Leave a Reply