/// <summary>Initializes the <see cref="VisualControlBox" />.</summary> private void InitializeControlBox() { DoubleBuffered = true; UpdateStyles(); Anchor = AnchorStyles.Top | AnchorStyles.Right; BackColor = Color.Transparent; Size = new Size(100, 25); Padding = new Padding(0); _buttonSize = new Size(24, Height); _helpButton = new ControlBoxButton { Location = new Point(0, 0), Size = _buttonSize, Text = ControlBoxConstants.HelpText, OffsetLocation = new Point(0, 1) }; _helpButton.Click += OnHelpClick; _helpButton.VisibleChanged += Button_VisibleChanged; _minimizeButton = new ControlBoxButton { Location = new Point(_buttonSize.Width, 0), Size = _buttonSize, Text = ControlBoxConstants.MinimizeText, OffsetLocation = new Point(2, 0) }; _minimizeButton.Click += OnMinimizeClick; _minimizeButton.VisibleChanged += Button_VisibleChanged; _maximizeButton = new ControlBoxButton { Location = new Point(_buttonSize.Width * 2, 0), Size = _buttonSize, Text = ControlBoxConstants.MaximizeText, OffsetLocation = new Point(1, 1) }; _maximizeButton.Click += OnMaximizeClick; _maximizeButton.VisibleChanged += Button_VisibleChanged; _closeButton = new ControlBoxButton { Location = new Point(_buttonSize.Width * 3, 0), Size = _buttonSize, Text = ControlBoxConstants.CloseText, OffsetLocation = new Point(1, 2) }; _closeButton.Click += OnCloseClick; _closeButton.VisibleChanged += Button_VisibleChanged; _initialized = true; Controls.Add(_helpButton); Controls.Add(_minimizeButton); Controls.Add(_maximizeButton); Controls.Add(_closeButton); }
private void UpdateButtons(ControlBoxButtonState State, MouseEventArgs e) { // Определение положения управляющих кнопок Rectangle _minimizeBoundsBase = new Rectangle(Width - 57, 4, 16, 16), _maximizeBoundsBase = new Rectangle(Width - 39, 4, 16, 16), _closeBoundsBase = new Rectangle(Width - 21, 4, 16, 16); _minimizeBounds = (MaximizeBox) ? _minimizeBoundsBase : _maximizeBoundsBase; _maximizeBounds = _maximizeBoundsBase; _closeBounds = _closeBoundsBase; _statusBarBounds = new Rectangle(0, 0, Width - 1, _statusBarHeight); // Определение активной управляющей кнопки при перемещении мыши if (_minimizeBounds.Contains(e.Location)) { _button = ControlBoxButton.Minimize; } if (_maximizeBounds.Contains(e.Location)) { _button = ControlBoxButton.Maximize; } if (_closeBounds.Contains(e.Location)) { _button = ControlBoxButton.Close; } // Определение действий управляющих кнопок _buttonState = (State == ControlBoxButtonState.Down) ? ControlBoxButtonState.Down : ControlBoxButtonState.None; // Установка событий управляющих кнопок if (ControlBox) { if (MinimizeBox && State == ControlBoxButtonState.Down && _button == ControlBoxButton.Minimize) { if (Animation) { Window.Hide(this, 5); } else { WindowState = FormWindowState.Minimized; } } if (MaximizeBox && State == ControlBoxButtonState.Down && _button == ControlBoxButton.Maximize) { WindowState = FormWindowState.Maximized; } if (State == ControlBoxButtonState.Down && _button == ControlBoxButton.Close) { if (Animation) { Window.Close(this, 5); } else { Close(); } } } }
private void InitializeCaption(String name = "FormCaptionPanel") { /// /// Caption Panel /// FormCaptionPanel.Name = name; FormCaptionPanel.BackColor = Color.Transparent; FormCaptionPanel.Dock = DockStyle.Top; FormCaptionPanel.Height = TOPEXTENDWIDTH; FormCaptionPanel.Margin = new Padding(0); FormCaptionPanel.Paint += (s, e) => FormCaptionPanel.BackColor = Color.Transparent; /// /// Append global FormCaptionPanel (a Panel) into Form /// Controls.Add(FormCaptionPanel); /// /// Control Box /// FlowLayoutPanel pControlBox = new FlowLayoutPanel { Name = "ControlBox", FlowDirection = FlowDirection.LeftToRight, WrapContents = false, AutoSize = true, AutoSizeMode = AutoSizeMode.GrowAndShrink, BackColor = FormCaptionPanel.BackColor, Margin = new Padding(0), Padding = new Padding(0), Dock = DockStyle.Right }; int yPadding = RIGHTEXTENDWIDTH; int subHeight = yPadding * 2; int height = FormCaptionPanel.Height - subHeight; int width = FormCaptionPanel.Height + 10; ControlBoxButton btnExit = new ControlBoxButton() { ButtonAction = ControlBoxButtonAction.Exit, BackColor = FormCaptionPanel.BackColor, Margin = new Padding(0, yPadding, RIGHTEXTENDWIDTH, yPadding), Height = height, Width = width }; ControlBoxButton btnMin = new ControlBoxButton() { ButtonAction = ControlBoxButtonAction.Minimize, BackColor = FormCaptionPanel.BackColor, Margin = new Padding(0, yPadding, 0, yPadding), Height = height, Width = width, Visible = mMinimizeBox }; ControlBoxButton btnMax = new ControlBoxButton() { ButtonAction = ControlBoxButtonAction.Maximize, BackColor = FormCaptionPanel.BackColor, Margin = new Padding(0, yPadding, 0, yPadding), Height = height, Width = width, Visible = mMaximizeBox }; pControlBox.Controls.Add(btnMin); pControlBox.Controls.Add(btnMax); pControlBox.Controls.Add(btnExit); /// /// Caption Text and Image (FlowLayoutPanel) /// ImageAndTextPanel.Name = "ImageAndTextPanel"; ImageAndTextPanel.FlowDirection = FlowDirection.LeftToRight; ImageAndTextPanel.WrapContents = false; ImageAndTextPanel.Margin = new Padding(0); ImageAndTextPanel.Padding = new Padding(0); ImageAndTextPanel.AutoSize = true; ImageAndTextPanel.AutoSizeMode = AutoSizeMode.GrowAndShrink; ImageAndTextPanel.Dock = DockStyle.Fill; ImageAndTextPanel.BackColor = FormCaptionPanel.BackColor; /// /// Caption Image (Panel) /// CaptionImagePanel.Name = "CaptionImagePanel"; CaptionImagePanel.BackgroundImage = Icon.ToBitmap().ResizeImage(25); CaptionImagePanel.BackgroundImageLayout = ImageLayout.Center; CaptionImagePanel.Height = FormCaptionPanel.Height; CaptionImagePanel.Width = FormCaptionPanel.Height; CaptionImagePanel.Margin = new Padding(0); CaptionImagePanel.Padding = new Padding(0); CaptionImagePanel.BackColor = FormCaptionPanel.BackColor; /// /// Caption Text (Label) /// CaptionTextLabel.Name = "CaptionTextLabel"; CaptionTextLabel.Text = this.Text; CaptionTextLabel.TextAlign = ContentAlignment.MiddleLeft; CaptionTextLabel.Font = new Font(SystemFonts.CaptionFont.FontFamily.Name, 9.75F, FontStyle.Regular); CaptionTextLabel.AutoSize = false; CaptionTextLabel.AutoEllipsis = true; CaptionTextLabel.Width = 800; CaptionTextLabel.Height = FormCaptionPanel.Height; CaptionTextLabel.Margin = new Padding(0); CaptionTextLabel.Padding = new Padding(5, 0, 0, 0); CaptionTextLabel.BackColor = FormCaptionPanel.BackColor; CaptionTextLabel.ForeColor = mCaptionForeColor; /// /// Append CaptionImagePanel (a Panel) and CaptionTextLabel (a Label) /// into ImageAndTextPanel (a FlowLayoutPanel) /// ImageAndTextPanel.Controls.Add(CaptionImagePanel); ImageAndTextPanel.Controls.Add(CaptionTextLabel); /// /// Append local pControlBox (a FlowLayoutPanel) and ImageAndTextPanel(a FlowLayoutPanel) /// into FormCaptionPanel (a Panel) /// FormCaptionPanel.Controls.Add(pControlBox); FormCaptionPanel.Controls.Add(ImageAndTextPanel); /// /// Re-Apply contained item's height on resize /// FormCaptionPanel.Resize += (s, e) => { CaptionImagePanel.Height = FormCaptionPanel.Height; CaptionTextLabel.Height = FormCaptionPanel.Height; ImageAndTextPanel.Height = FormCaptionPanel.Height; int _subHeight = yPadding * 2; int _height = FormCaptionPanel.Height - subHeight; int _width = FormCaptionPanel.Height + 10; btnExit.Width = _width; btnMax.Width = _width; btnMin.Width = _width; btnExit.Height = _height; btnMax.Height = _height; btnMin.Height = _height; }; }