首先,您必须将 Home_MouseMove 事件更改为非静态:
public void Home_MouseMove(object sender, MouseEventArgs e)
{
bomb.Stop();
bomb.Start();
}
转到 program.cs 文件并更改以下代码:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new FrmMain());
}
}
到这段代码:
static class Program
{
public static FrmMain MainForm;// add this line
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
MainForm = new FrmMain();// add this line
Application.Run(MainForm);
}
}
现在在 form2 上,您可以像这样在主窗体中调用 MouseMove:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// call MouseMove event in main form
Program.MainForm.FrmMain_MouseMove(null, null);
}
}