1

所以我在那个类中有一个名为Common.cs的类,我有一个创建表单实例的函数frmMainGame.cs,然后调用一个应该调用这个新表单实例的方法,但由于某种原因,它没有。因此,当使用mainGame.Show() 显示表单时;表格只是挂在那里,我不能用它做任何事情。

这是代码:

frmMainGame.cs

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 System.Net.Sockets;
using System.Threading;

namespace Ghoul_Engine
{ 

public partial class frmMainGame : Form
{

 public delegate void openGame();
 public delegate void refreshGame();
 public delegate void updateScreen(Bitmap image);

 drawing draw;


 public bool isTyping = false;


    public Graphics g;

    public List<String> Chat = new List<String>();

    public List<String> chat
    {
        get { return Chat; }
    }

    public frmMainGame()
    {

        draw = new drawing(this);

        InitializeComponent();

    }

    public Size size{
        get { return this.Size; }
    }

    public void showGame()
    {
        if (InvokeRequired)
        {
            MessageBox.Show("Invoke");
            BeginInvoke(new openGame(showGame), null);
        }
        else
        {
            this.Show();
            this.Focus();
        }
    }


    public void refresh()
    {
        if (this.InvokeRequired)
        {
            this.Invoke(new refreshGame(refresh), null);
        }
        else
        {
            this.Refresh();
        }
    }
    private void frmMainGame_Load(object sender, EventArgs e)
    {
        this.DoubleBuffered = true;
        draw.drawText(new string[] { "GHOUL Game Engine Alpha V0.0.1" }, 12, 0, 0, Brushes.Yellow);
        clientNetHandler handlers = new clientNetHandler();

    }

    public void update(Bitmap image)
    {
        if (InvokeRequired)
        {
            this.Invoke(new updateScreen(update), new object[] { image });
        }
        else
        {
            pbScreen.Image = image;
        }
    }


    private void frmMainGame_Close(object sender, FormClosingEventArgs e)
    {

        Program.Network.Send("logout", Program.netHandlers.getDataByte(clientNetHandler.cNetType.Logout));
        //Program.Network.closeSocket();
        Program.mainMenu.showMainMenu();
        Program.mainGame = new frmMainGame();
    }

    private void tmrRefreshScreen_Tick(object sender, EventArgs e)
    {

    }

    private void frmMainGame_KeyPress(object sender, KeyPressEventArgs e)
    {

    }

    private void pbScreen_Click(object sender, EventArgs e)
    {
        draw.drawText(Chat.ToArray(), 12, 0, 30, Brushes.Green);
    }
}
}

Common.Cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.Drawing;

namespace Ghoul_Engine
{
class Common
{


     public Common()
    {
    }
     public void innnitiateGame(String login)
     {

         Program.mainMenu.hideMainMenu();
         frmMainGame mainGame = new frmMainGame();
         mainGame.showGame();
     }

    public void preExit()
    {
            Program.Network.closeSocket();
    }

}
}
4

2 回答 2

2

您可能需要稍微重新设计您的类结构。我建议您不要在 Common.cs 中创建主窗体的实例,而是按照正常的方式进行操作,即在 program.cs 中创建主窗体的实例,Application.Run()以便它为您安装消息泵,保留您的套接字通信类在一个单独的文件中(可能就像它们一样),然后在你的主窗体中实例化它们。如果您必须在多个表单中使用这些通信类,那么继续在 program.cs 中创建它们的实例并将它们作为参数传递给不同的表单/其他模块。

于 2013-06-10T03:04:05.430 回答
2

您需要一个消息泵,它是通过将表单实例传递给创建的Application.Run()

     Application.Run(new frmMainGame());
于 2013-06-10T02:34:20.933 回答