доработаны формы-меню
This commit is contained in:
		
							parent
							
								
									6256105b42
								
							
						
					
					
						commit
						f440212793
					
				| @ -34,8 +34,8 @@ class Program | |||||||
|         mainMenu.AddOption("About me", () => PrintAboutMe()); |         mainMenu.AddOption("About me", () => PrintAboutMe()); | ||||||
|         mainMenu.AddOption("Array sort", () => new ArraySortDemo().Run()); |         mainMenu.AddOption("Array sort", () => new ArraySortDemo().Run()); | ||||||
|         mainMenu.AddOption("Snake game", () => { |         mainMenu.AddOption("Snake game", () => { | ||||||
|             SnakeGame game = new SnakeGame(difficulty, sizex, sizey); |             mainMenuForm.Hide(); | ||||||
|             game.start(); |             snakeDiffMenuForm.Show(); | ||||||
|         }); |         }); | ||||||
|         mainMenu.AddOption("Exit", () => Exit()); |         mainMenu.AddOption("Exit", () => Exit()); | ||||||
| 
 | 
 | ||||||
| @ -44,8 +44,13 @@ class Program | |||||||
|         Utils.Arrays withTwoParams = new Utils.Arrays(10, 100); |         Utils.Arrays withTwoParams = new Utils.Arrays(10, 100); | ||||||
| 
 | 
 | ||||||
|         mainMenuForm = new MenuForm(mainMenu); |         mainMenuForm = new MenuForm(mainMenu); | ||||||
|         snakeDiffMenuForm = new MenuForm(difficultyMenu); |         snakeDiffMenuForm = new MenuForm(difficultyMenu, () => { snakeDiffMenuForm.SwitchToForm(snakeSizeMenuForm); }); | ||||||
|         snakeSizeMenuForm = new MenuForm(sizeMenu); |         snakeSizeMenuForm = new MenuForm(sizeMenu, () =>  | ||||||
|  |         { | ||||||
|  |             SnakeGame game = new SnakeGame(difficulty, sizex, sizey); | ||||||
|  |             game.start(); | ||||||
|  |             snakeSizeMenuForm.SwitchToForm(mainMenuForm); | ||||||
|  |         }); | ||||||
| 
 | 
 | ||||||
|         Application.Run(mainMenuForm); |         Application.Run(mainMenuForm); | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -21,6 +21,22 @@ namespace ProgLab1.GUI | |||||||
|             this.Width = 800; |             this.Width = 800; | ||||||
|             this.Height = 600; |             this.Height = 600; | ||||||
|             AddMenu(consoleMenu); |             AddMenu(consoleMenu); | ||||||
|  |             this.FormClosing += new FormClosingEventHandler((object sender, FormClosingEventArgs e) => { e.Cancel = true; }); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         public MenuForm(Menu consoleMenu, Action onAny) | ||||||
|  |         { | ||||||
|  |             InitializeComponent(); | ||||||
|  |             this.Width = 800; | ||||||
|  |             this.Height = 600; | ||||||
|  |             AddMenu(consoleMenu, onAny); | ||||||
|  |             this.FormClosing += new FormClosingEventHandler((object sender, FormClosingEventArgs e) => { e.Cancel = true; }); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         public void SwitchToForm(Form form) | ||||||
|  |         { | ||||||
|  |             form.Show(); | ||||||
|  |             this.Hide(); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         /// <summary> |         /// <summary> | ||||||
| @ -66,5 +82,61 @@ namespace ProgLab1.GUI | |||||||
| 
 | 
 | ||||||
|             this.Controls.Add(buttonPanel); |             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;  // Разрешаем закрытие окна | ||||||
|  |             } | ||||||
|  |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user