2017-02-24 3 views
0

ボタンを押すとフォーム2を開きます。フォーム1にあるボタンは、ゲームのメインメニューになります。それをクリックすると、ゲームが開き、メニューが閉じます(2つのフォームを持つことでこれを行うことができ、私は、これが単一インスタンスを処理するために、フォームマネージャを使用して、非常に単純な例である)ボタンが1に近づき、フォーム2を開きます

形態1

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; 

namespace WindowsFormsApplication3 
{ 
    public partial class Form2 : Form 
    { 
     public Form2() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 

     } 
    } 
} 

形態2

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; 

namespace Spill 
{ 
    public partial class MainWindow : Form 

     { 
     Random _random; 


     public MainWindow() 
     { 
      InitializeComponent(); 
      _random = new Random(); 
     } 

     private void MainWindow_Load(object sender, EventArgs e) 
     { 

      Size s = new System.Drawing.Size(800, 600); 
      this.ClientSize = s; 
      this.FormBorderStyle = FormBorderStyle.FixedSingle; 
     } 

     private void MainWindow_KeyDown(object sender, KeyEventArgs e) 
     { 
      { 
       if (e.KeyCode == Keys.Left) 
       { 
        Player.Left -= 20; 
       } 

       if (e.KeyCode == Keys.Right) 
       { 
        Player.Left += 20; 
       } 

      } 
     } 

     private void timer1_Tick(object sender, EventArgs e) 
     { 
      int z = _random.Next(0, 10); 
      int x = _random.Next(0, 20); 
      int y = _random.Next(0, 30); 
      LargeEnemy.Left += z; 
      MediumEnemy.Left += x; 
      SmallEnemy.Left += y; 

     } 

     private void newGameToolStripMenuItem_Click(object sender, EventArgs e) 
     { 
      Application.Restart(); 
     } 

     private void quitGameToolStripMenuItem_Click(object sender, EventArgs e) 
     { 
      Application.Exit(); 
     } 

     private void LargeEnemy_Click(object sender, EventArgs e) 
     { 

     } 
    } 
} 

答えて

0

それは非常に簡単です:

この機能で
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
System.Data; 
System.Drawing; 
using System.Linq; 
System.Text; 
System.Threading.Tasks; 
System.Windows.Forms; 

namespace WindowsFormsApplication3 
{ 
public partial class Form2 : Form 
{ 
    public Form2() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     //hide the current winform 
     this.Hide(); 
     //create game winform 
     WindowsFormsApplication1 frmGame = new WindowsFormsApplication1(); 
     //show the game winform 
     frmGame.ShowDialog(); 
     //when the game winform closes show again the menu 
     this.Show(); 
    } 
} 
} 

と二番目の形式では、この

private void newGameToolStripMenuItem_Click(object sender, EventArgs e) 
    { 
     this.Close(); 
    } 

    private void quitGameToolStripMenuItem_Click(object sender, EventArgs e) 
    { 
     this.Close(); 
    } 
のようにそれらを設定します
0

これを行うことができます。このようにして、両方のフォームが存続し、可視性を更新するだけです。

アプリケーションを閉じるには、両方のフォームを正しく閉じる必要があります。

namespace WindowsFormsApplication1 
      { 

       static class FormManager 
       { 
        public static Form1 Game = new Form1(); 
        public static Form2 Menu = new Form2(); 

       } 

       /// <summary> 
       /// The main entry point for the application. 
       /// </summary> 
       [STAThread] 
       static void Main() 
       { 
        Application.EnableVisualStyles(); 
        Application.Run(FormManager.Game); 
       } 




       public partial class Form1 : Form 
       { 
        private void btn_Menu_Click(object sender, EventArgs e) 
        { 
         FormManager.Game.Hide(); 
         FormManager.Menu.Show(); 
        } 
       } 

       public partial class Form2 : Form 
       { 

        private void btn_Close_Click(object sender, EventArgs e) 
        { 
         FormManager.Menu.Hide(); 
         FormManager.Game.Show(); 
        } 
       } 


      } 
関連する問題