Пример #1
0
        /// <include file='doc\ControlCommandSet.uex' path='docs/doc[@for="ControlCommandSet.OnKeySize"]/*' />
        /// <devdoc>
        ///     Called for the various sizing commands we support.
        /// </devdoc>
        protected void OnKeySize(object sender, EventArgs e)
        {
            // Arrow keys.  Begin a drag if the selection isn't locked.
            //
            ISelectionService   selSvc = SelectionService;
            ISelectionUIService uiSvc  = SelectionUIService;

            if (uiSvc != null && selSvc != null)
            {
                //added to remove the selection rectangle: bug(54692)
                //
                uiSvc.Visible = false;
                object comp = selSvc.PrimarySelection;
                if (comp != null && comp is IComponent)
                {
                    PropertyDescriptor lockedProp = TypeDescriptor.GetProperties(comp)["Locked"];
                    if (lockedProp == null || (lockedProp.PropertyType == typeof(bool) && ((bool)lockedProp.GetValue(comp))) == false)
                    {
                        SelectionRules rules       = SelectionRules.Visible;
                        CommandID      cmd         = ((MenuCommand)sender).CommandID;
                        bool           invertSnap  = false;
                        int            moveOffsetX = 0;
                        int            moveOffsetY = 0;

                        if (cmd.Equals(MenuCommands.KeySizeHeightDecrease))
                        {
                            moveOffsetY = -1;
                            rules      |= SelectionRules.BottomSizeable;
                        }
                        else if (cmd.Equals(MenuCommands.KeySizeHeightIncrease))
                        {
                            moveOffsetY = 1;
                            rules      |= SelectionRules.BottomSizeable;
                        }
                        else if (cmd.Equals(MenuCommands.KeySizeWidthDecrease))
                        {
                            moveOffsetX = -1;
                            rules      |= SelectionRules.RightSizeable;
                        }
                        else if (cmd.Equals(MenuCommands.KeySizeWidthIncrease))
                        {
                            moveOffsetX = 1;
                            rules      |= SelectionRules.RightSizeable;
                        }
                        else if (cmd.Equals(MenuCommands.KeyNudgeHeightDecrease))
                        {
                            moveOffsetY = -1;
                            invertSnap  = true;
                            rules      |= SelectionRules.BottomSizeable;
                        }
                        else if (cmd.Equals(MenuCommands.KeyNudgeHeightIncrease))
                        {
                            moveOffsetY = 1;
                            invertSnap  = true;
                            rules      |= SelectionRules.BottomSizeable;
                        }
                        else if (cmd.Equals(MenuCommands.KeyNudgeWidthDecrease))
                        {
                            moveOffsetX = -1;
                            invertSnap  = true;
                            rules      |= SelectionRules.RightSizeable;
                        }
                        else if (cmd.Equals(MenuCommands.KeyNudgeWidthIncrease))
                        {
                            moveOffsetX = 1;
                            invertSnap  = true;
                            rules      |= SelectionRules.RightSizeable;
                        }
                        else
                        {
                            Debug.Fail("Unknown command mapped to OnKeySize: " + cmd.ToString());
                        }

                        if (uiSvc.BeginDrag(rules, 0, 0))
                        {
                            bool               snapOn        = false;
                            Size               snapSize      = Size.Empty;
                            IComponent         snapComponent = null;
                            PropertyDescriptor snapProperty  = null;

                            // Gets the needed snap information
                            //
                            IDesignerHost       host  = (IDesignerHost)GetService(typeof(IDesignerHost));
                            DesignerTransaction trans = null;

                            try {
                                if (host != null)
                                {
                                    GetSnapInformation(host, (IComponent)comp, out snapSize, out snapComponent, out snapProperty);

                                    if (selSvc.SelectionCount > 1)
                                    {
                                        trans = host.CreateTransaction(SR.GetString(SR.DragDropSizeComponents, selSvc.SelectionCount));
                                    }
                                    else
                                    {
                                        trans = host.CreateTransaction(SR.GetString(SR.DragDropSizeComponent, ((IComponent)comp).Site.Name));
                                    }

                                    if (snapProperty != null)
                                    {
                                        snapOn = (bool)snapProperty.GetValue(snapComponent);

                                        if (invertSnap)
                                        {
                                            snapOn = !snapOn;
                                            snapProperty.SetValue(snapComponent, snapOn);
                                        }
                                    }
                                }

                                if (snapOn && !snapSize.IsEmpty)
                                {
                                    moveOffsetX *= snapSize.Width;
                                    moveOffsetY *= snapSize.Height;
                                }


                                // Now move the controls the correct # of pixels.
                                //
                                uiSvc.DragMoved(new Rectangle(0, 0, moveOffsetX, moveOffsetY));
                                uiSvc.EndDrag(false);

                                if (host != null)
                                {
                                    if (invertSnap && snapProperty != null)
                                    {
                                        snapOn = !snapOn;
                                        snapProperty.SetValue(snapComponent, snapOn);
                                    }
                                }
                            }
                            finally {
                                if (trans != null)
                                {
                                    trans.Commit();
                                    uiSvc.Visible = true;
                                }
                            }
                        }
                    }
                }

                uiSvc.Visible = true;
            }
        }