Пример #1
0
        private void canvasPictureBox_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (GuiControls.Control.FocusedControl != null)
            {
                if (e.KeyCode == Keys.Delete)
                {
                    var removeControl = GuiControls.Control.FocusedControl;
                    var parent        = removeControl.RealParent ?? form;

                    RecursiveRemove(removeControl);

                    parent.Focus();

                    controlComboBox.SelectedIndex = 0;
                }
                else if (e.Control && e.KeyCode == Keys.C)
                {
                    if (GuiControls.Control.FocusedControl == form)
                    {
                        return;
                    }

                    var copiedControl = GuiControls.Control.FocusedControl.Copy();
                    Clipboard.SetData(
                        "OSHVisualGuiControl",
                        ControlSerializer.Serialize(copiedControl).ToString()
                        );
                }
                else if (e.Control && e.KeyCode == Keys.X)
                {
                    if (GuiControls.Control.FocusedControl == form)
                    {
                        return;
                    }

                    Clipboard.SetData(
                        "OSHVisualGuiControl",
                        ControlSerializer.Serialize(GuiControls.Control.FocusedControl).ToString()
                        );

                    RecursiveRemove(GuiControls.Control.FocusedControl);

                    controlComboBox.SelectedIndex = 0;
                }
                else if (e.Control && e.KeyCode == Keys.V)
                {
                    if (!Clipboard.ContainsData("OSHVisualGuiControl"))
                    {
                        return;
                    }

                    if (Clipboard.GetData("OSHVisualGuiControl") is string serializedControl)
                    {
                        var copiedControl = ControlSerializer.Deserialize(XElement.Parse(serializedControl));
                        if (copiedControl != null)
                        {
                            ContainerControl parent = null;
                            if (copiedControl is GuiControls.TabPage && !(GuiControls.Control.FocusedControl is GuiControls.TabControl))
                            {
                                MessageBox.Show("A TabPage needs to be inserted into a TabControl.");
                                return;
                            }
                            if (GuiControls.Control.FocusedControl is ContainerControl)
                            {
                                parent = GuiControls.Control.FocusedControl as ContainerControl;
                            }
                            else
                            {
                                parent = GuiControls.Control.FocusedControl.RealParent;
                            }

                            //check if name already exists
                            if (ControlManager.Instance().FindByName(copiedControl.Name, null) != null)
                            {
                                //then change the name
                                copiedControl.Name = copiedControl.DefaultName + ControlManager.Instance().GetControlCountPlusOne(copiedControl.GetType());
                                //and change the location
                                copiedControl.Location = copiedControl.Location.Add(new Point(10, 10));
                            }

                            parent.AddControl(copiedControl);

                            AddControlToList(copiedControl);
                            if (copiedControl is ContainerControl)
                            {
                                foreach (var control in (copiedControl as ContainerControl).PreOrderVisit())
                                {
                                    if (control is ScalableControl)
                                    {
                                        RegisterEvents(control);

                                        AddControlToList(control);
                                    }
                                }
                                controlComboBox.SelectedItem = copiedControl;
                            }

                            RegisterEvents(copiedControl);
                        }
                    }
                }
            }
        }
Пример #2
0
        private void canvasPictureBox_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(ControlType)))
            {
                var type = (ControlType)e.Data.GetData(typeof(ControlType));

                var cm = ControlManager.Instance();
                GuiControls.Control newControl = null;
                switch (type)
                {
                case ControlType.Button:
                    var button = new GuiControls.Button();
                    button.Text = button.Name = button.DefaultName + cm.GetControlCountPlusOne(typeof(GuiControls.Button));
                    newControl  = button;
                    break;

                case ControlType.CheckBox:
                    var checkBox = new GuiControls.CheckBox();
                    checkBox.Text = checkBox.Name = checkBox.DefaultName + cm.GetControlCountPlusOne(typeof(GuiControls.CheckBox));
                    newControl    = checkBox;
                    break;

                case ControlType.ColorBar:
                    var colorBar = new ColorBar();
                    colorBar.Name = colorBar.DefaultName + cm.GetControlCountPlusOne(typeof(ColorBar));
                    newControl    = colorBar;
                    break;

                case ControlType.ColorPicker:
                    var colorPicker = new GuiControls.ColorPicker();
                    colorPicker.Name = colorPicker.DefaultName + cm.GetControlCountPlusOne(typeof(GuiControls.ColorPicker));
                    newControl       = colorPicker;
                    break;

                case ControlType.ComboBox:
                    var comboBox = new GuiControls.ComboBox();
                    comboBox.Text = comboBox.Name = comboBox.DefaultName + cm.GetControlCountPlusOne(typeof(GuiControls.ComboBox));
                    newControl    = comboBox;
                    break;

                case ControlType.GroupBox:
                    var groupBox = new GuiControls.GroupBox();
                    groupBox.Text = groupBox.Name = groupBox.DefaultName + cm.GetControlCountPlusOne(typeof(GuiControls.GroupBox));
                    newControl    = groupBox;
                    break;

                case ControlType.Label:
                    var label = new GuiControls.Label();
                    label.Text = label.Name = label.DefaultName + cm.GetControlCountPlusOne(typeof(GuiControls.Label));
                    newControl = label;
                    break;

                case ControlType.LinkLabel:
                    var linkLabel = new GuiControls.LinkLabel();
                    linkLabel.Text = linkLabel.Name = linkLabel.DefaultName + cm.GetControlCountPlusOne(typeof(GuiControls.LinkLabel));
                    newControl     = linkLabel;
                    break;

                case ControlType.ListBox:
                    var listBox = new GuiControls.ListBox();
                    listBox.Name = listBox.DefaultName + cm.GetControlCountPlusOne(typeof(GuiControls.ListBox));
                    newControl   = listBox;
                    break;

                case ControlType.Panel:
                    var panel = new GuiControls.Panel();
                    panel.Name = panel.DefaultName + cm.GetControlCountPlusOne(typeof(GuiControls.Panel));
                    newControl = panel;
                    break;

                case ControlType.PictureBox:
                    var pictureBox = new GuiControls.PictureBox();
                    pictureBox.Name = pictureBox.DefaultName + cm.GetControlCountPlusOne(typeof(GuiControls.PictureBox));
                    newControl      = pictureBox;
                    break;

                case ControlType.ProgressBar:
                    var progressBar = new GuiControls.ProgressBar();
                    progressBar.Name = progressBar.DefaultName + cm.GetControlCountPlusOne(typeof(GuiControls.ProgressBar));
                    newControl       = progressBar;
                    break;

                case ControlType.RadioButton:
                    var radioButton = new GuiControls.RadioButton();
                    radioButton.Text = radioButton.Name = radioButton.DefaultName + cm.GetControlCountPlusOne(typeof(GuiControls.RadioButton));
                    newControl       = radioButton;
                    break;

                case ControlType.TabControl:
                    var tabControl = new GuiControls.TabControl();
                    tabControl.Name = tabControl.DefaultName + cm.GetControlCountPlusOne(typeof(GuiControls.TabControl));
                    var tempTabPage = new GuiControls.TabPage();
                    tempTabPage.Text = tempTabPage.Name = tempTabPage.DefaultName + cm.GetControlCountPlusOne(typeof(GuiControls.TabPage));
                    tabControl.AddTabPage(tempTabPage);
                    AddControlToList(tempTabPage);
                    newControl = tabControl;
                    break;

                case ControlType.TabPage:
                    var tabPage = new GuiControls.TabPage();
                    tabPage.Text = tabPage.Name = tabPage.DefaultName + cm.GetControlCountPlusOne(typeof(GuiControls.TabPage));
                    newControl   = tabPage;
                    break;

                case ControlType.TextBox:
                    var textBox = new GuiControls.TextBox();
                    textBox.Name = textBox.Text = textBox.DefaultName + cm.GetControlCountPlusOne(typeof(GuiControls.TextBox));
                    newControl   = textBox;
                    break;

                case ControlType.Timer:
                    var timer = new GuiControls.Timer();
                    timer.Name = timer.DefaultName + cm.GetControlCountPlusOne(typeof(GuiControls.Timer));
                    newControl = timer;
                    break;

                case ControlType.TrackBar:
                    var trackBar = new GuiControls.TrackBar();
                    trackBar.Name = trackBar.DefaultName + cm.GetControlCountPlusOne(typeof(GuiControls.TrackBar));
                    newControl    = trackBar;
                    break;

                case ControlType.HotkeyControl:
                    var hotkeyControl = new HotkeyControl();
                    hotkeyControl.Name = hotkeyControl.DefaultName + cm.GetControlCountPlusOne(typeof(HotkeyControl));
                    newControl         = hotkeyControl;
                    break;
                }
                if (newControl == null)
                {
                    return;
                }

                RegisterEvents(newControl);

                AddControlToList(newControl);

                var location = canvasPictureBox.PointToClient(new Point(e.X, e.Y));
                var parent   = FindContainerControlUnderMouse(location);
                newControl.Location = location.Substract(parent.ContainerAbsoluteLocation);
                parent.AddControl(newControl);
            }
        }