所以我想要的是当你按下我打开游戏所在的表格2的按钮时。形式1的按钮将成为我的游戏主菜单,你点击它然后打开游戏并关闭菜单(如果有可能这样做而没有2个表格而只是使用1分享如何我可以这样做)
表格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 :(得分: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();
}
}
}
答案 1 :(得分: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();
}