0

So I want to have a winforms GUI for my game... But I can't seem to find a GUI lib that is tested to work with Monogame. That's why I have another plan:

  1. Create a WinForms Project to act as a startup for my game.
  2. Then, when the user clicks "Start Game" in that project's main form, the winforms project should look for my Monogame project executable and start it, then the form should close.
  3. However I want to pass an argument from the WinForms exe to the Monogame exe. This has the purpose of somewhat preventing users from starting the game through the Monogame exe and use the Winforms "Starter" instead. Which leads to step #4.
  4. If no argument is passed to the Monogame project it should close.

So should I do it like in console applications, pass a string array to the main void of the Monogame app like this:

using System;

namespace myGame
{
#if WINDOWS || LINUX
    /// <summary>
    /// The main class.
    /// </summary>
    public static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            using (var game = new Game1())
                game.Run();
            if (args[0] != "startFromWinForms")
            {
                // Find a way to close or
                // on the other hand I think
                // it will throw an exception??
            }
        }
    }
#endif
}

How would you do that? What approach do you recommend? Also code examples/ideas explained would be great!

Johny P.
  • 648
  • 1
  • 9
  • 31

2 Answers2

0

Like this?

    [STAThread]
    static void Main(string[] args)
    {
        if (args[0] == "startFromWinForms")
        {
            using (var game = new Game1())
                game.Run();
        }
    }

If you have an argiment, you will start your game, otherwise nothing happens

Backs
  • 24,430
  • 5
  • 58
  • 85
0

Let's take a step back and look at your original requirement.

I want to have a winforms GUI for my game ... when the user clicks "Start Game" [the game will start]

I just tried a simple solution that I think solves your problem without needing to create 2 projects. All I had to do was:

  1. Create a new MonoGame Windows project.
  2. Add a WinForms form (Form1) to it.
  3. Write the following code..

In the Main() method:

    [STAThread]
    static void Main()
    {
        var form = new Form1();

        if (form.ShowDialog() == DialogResult.OK)
        {
            using (var game = new Game1())
                game.Run();
        }
    }

And in the Form1 button click event:

    private void StartGameButton_Click(object sender, EventArgs e)
    {
        DialogResult = DialogResult.OK;
    }

And that's it. You don't need to pass anything on the command line. You'll only have 1 exe and it meets all of the requirements. MonoGame works exactly as it does now and the WinForms code stays isolated from the game.

craftworkgames
  • 9,437
  • 4
  • 41
  • 52
  • I'm not an expert in winforms but is there any way to do the logic with normal form and not a dialog box? Since I'm using windows 10 most forms have the same border style for me but in previous windows versions I can't be sure it's what I want... – Johny P. Aug 19 '15 at 11:38
  • The `ShowDialog` method just makes sure that the code pauses while the form is shown. I don't think it changes the border style at all. – craftworkgames Aug 19 '15 at 11:45
  • Also for people that might need such solution for the future... If your form's buttons appear with no styling you can type `Application.EnableVisualStyles();` in the form constructor to fix it. – Johny P. Aug 19 '15 at 12:41