Пример #1
0
        public override void OnMouseDown(CellContext sender, MouseEventArgs e)
        {
            base.OnMouseDown(sender, e);

            var pressed = e.Button & MouseButtons;

            if (pressed == MouseButtons.None)
            {
                return;
            }

            //[email protected]: Remember the point where the mouse down occurred. The DragSize indicates
            // the size that the mouse can move before a drag event should be started.
            Size dragSize = SystemInformation.DragSize;

            // [email protected]: Create a rectangle using the DragSize, with the mouse position being
            // at the center of the rectangle.
            m_DragSizeRectangle = new Rectangle(
                new Point(e.X - SystemInformation.DragSize.Width / 2,
                          e.Y - SystemInformation.DragSize.Height / 2), dragSize);

            GridVirtual grid = sender.Grid;

            //[email protected]: To Fix autoscrolling issue on pressing shift and then moving mouse
            //after releasing shift
            Range rg      = new Range(sender.Position);
            bool  inRange = false;

            if (grid.Selection.GetSelectionRegion() != null && grid.Selection.GetSelectionRegion().GetCellsPositions().Count > 1)
            {
                inRange = grid.Selection.GetSelectionRegion().IntersectsWith(rg);// return;
            }
            //Check the control and shift key status
            bool controlPress = ((Control.ModifierKeys & Keys.Control) == Keys.Control &&
                                 (grid.SpecialKeys & GridSpecialKeys.Control) == GridSpecialKeys.Control);

            bool shiftPress = ((Control.ModifierKeys & Keys.Shift) == Keys.Shift &&
                               (grid.SpecialKeys & GridSpecialKeys.Shift) == GridSpecialKeys.Shift);

            //[email protected]: Defect Fix - After using Shift to select continuous cells
            //click on one of the selected cell to initate drag and drop, this will reset
            //the selection. To avoid resetting of selection and enable autoscrolling.
            if (!shiftPress && !controlPress && inRange)
            {
                grid.Focus();


                if (grid.Selection.EnableDragSelection)
                {
                    //[email protected]: this condition is necessary for
                    //excel like selection in dataGrid
                    grid.Selection.ResetSelection(true);
                    grid.Selection.Focus(sender.Position, true);
                }

                /*[email protected]: when change is made to accomodate FreezePanes,
                 * //scrolltracking should be enabled to the whole dataArea. and not just scrollrect */
                // begin scroll tracking only if mouse was clicked
                // in scrollable area
                Rectangle scrollRect1 = grid.GetDataArea();
                if (scrollRect1.Contains(e.Location))
                {
                    BeginScrollTracking(grid);
                }

                grid.Selection.Invalidate();
                return;
            }

            //Default click handler
            if (shiftPress == false ||
                grid.Selection.EnableMultiSelection == false)
            {
                //Handle Control key
                bool mantainSelection = grid.Selection.EnableMultiSelection && controlPress;// && inRange;

                //If the cell is already selected and the user has the ctrl key pressed then deselect the cell
                if (controlPress && grid.Selection.IsSelectedCell(sender.Position) && grid.Selection.ActivePosition != sender.Position)
                {
                    grid.Selection.SelectCell(sender.Position, false);
                }
                else
                {
                    grid.Selection.Focus(sender.Position, !mantainSelection);
                }
            }
            else //handle shift key
            {
                //[email protected]: on pressing ctrl and then using shift it resets the selection - to avod this
                //grid.Selection.ResetSelection(true);
                Range rangeToSelect = new Range(grid.Selection.ActivePosition, sender.Position);
                grid.Selection.SelectRange(rangeToSelect, true);
            }

            /*[email protected]: when change is made to accomodate FreezePanes,
             *  //scrolltracking should be enabled to the whole dataArea. and not just scrollrect */
            // begin scroll tracking only if mouse was clicked
            // in scrollable area
            Rectangle scrollRect = grid.GetDataArea();

            if (scrollRect.Contains(e.Location))
            {
                BeginScrollTracking(grid);
            }

            grid.Selection.Invalidate();
        }