ProgLab1_winforms/GUI/MenuForm.cs

145 lines
5.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using laba3.Core;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static laba3.Core.Menu;
namespace ProgLab1.GUI
{
public partial class MenuForm : Form
{
public MenuForm(Menu consoleMenu)
{
InitializeComponent();
this.Text = "Меню";
this.Width = 800;
this.Height = 600;
AddMenu(consoleMenu);
this.FormClosing += new FormClosingEventHandler((object sender, FormClosingEventArgs e) => { e.Cancel = !Dialogs.Exit(); });
}
public MenuForm(Menu consoleMenu, Action onAny)
{
InitializeComponent();
this.Text = "Меню";
this.Width = 800;
this.Height = 600;
AddMenu(consoleMenu, onAny);
this.FormClosing += new FormClosingEventHandler((object sender, FormClosingEventArgs e) => { e.Cancel = !Dialogs.Exit(); });
}
public void SwitchToForm(Form form)
{
form.Show();
this.Hide();
}
/// <summary>
/// Adds menu to GUI
/// </summary>
/// <param name="consoleMenu">TUI menu</param>
private void AddMenu(Menu consoleMenu)
{
FlowLayoutPanel buttonPanel = new FlowLayoutPanel
{
Dock = DockStyle.Fill, // Растягиваем на всю форму
FlowDirection = FlowDirection.TopDown, // Кнопки располагаются сверху вниз
WrapContents = false // Запрет переноса
};
Label menuLabel = new Label
{
Text = consoleMenu.GetTitle(), // Текст заголовка
Font = new System.Drawing.Font("Arial", 16, System.Drawing.FontStyle.Bold), // Стиль шрифта
TextAlign = System.Drawing.ContentAlignment.MiddleCenter, // Выравнивание текста по центру
Dock = DockStyle.Top, // Заголовок сверху
Height = 40 // Высота заголовка
};
buttonPanel.Controls.Add(menuLabel);
foreach (MenuOption option in consoleMenu.GetOptions())
{
Button button = new Button
{
Text = option.Name,
Width = buttonPanel.Width - 10,
Height = 40,
Margin = new Padding(5),
};
// Связываем обработчик с OnSelect из консольного меню
button.Click += (sender, e) => option.Action();
buttonPanel.Controls.Add(button);
}
this.Controls.Add(buttonPanel);
}
private void AddMenu(Menu consoleMenu, Action onAny)
{
FlowLayoutPanel buttonPanel = new FlowLayoutPanel
{
Dock = DockStyle.Fill, // Растягиваем на всю форму
FlowDirection = FlowDirection.TopDown, // Кнопки располагаются сверху вниз
WrapContents = false // Запрет переноса
};
Label menuLabel = new Label
{
Text = consoleMenu.GetTitle(), // Текст заголовка
Font = new System.Drawing.Font("Arial", 16, System.Drawing.FontStyle.Bold), // Стиль шрифта
TextAlign = System.Drawing.ContentAlignment.MiddleCenter, // Выравнивание текста по центру
Dock = DockStyle.Top, // Заголовок сверху
Height = 40 // Высота заголовка
};
buttonPanel.Controls.Add(menuLabel);
foreach (MenuOption option in consoleMenu.GetOptions())
{
Button button = new Button
{
Text = option.Name,
Width = buttonPanel.Width - 10,
Height = 40,
Margin = new Padding(5),
};
// Связываем обработчик с OnSelect из консольного меню
button.Click += (sender, e) => option.Action();
button.Click += (sender, e) => onAny();
buttonPanel.Controls.Add(button);
}
this.Controls.Add(buttonPanel);
}
private void FormClosingHandler(object sender, FormClosingEventArgs e)
{
// Предотвращаем закрытие окна
DialogResult result = MessageBox.Show("Вы уверены, что хотите закрыть окно?", "Подтверждение", MessageBoxButtons.YesNo);
if (result == DialogResult.No)
{
e.Cancel = true; // Отменяем закрытие окна
}
else
{
e.Cancel = false; // Разрешаем закрытие окна
}
}
}
}