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:
Create a WinForms Project to act as a startup for my game.
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.
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.
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!
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:
Create a new MonoGame Windows project.
Add a WinForms form (Form1) to it.
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.
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
Related
I've a C# WinForm application. Currently it runs from a desktop shortcut. But I would like to add it in system startup. User can decide whether it will run on startup or not.
If it runs on system startup, I would like to minimize it on system tray, otherwise it will run on task-bar.
Is there any way to check whether it is being launched on startup or not?
Your application wont be able to detect (by itself) it was launched at startup or by normal user launching. However you can pass arguments to your application and then have your application respond correctly. Here is a basic example
First start in program.cs main method. Now by default you dont see the startup arguments passed in. However adding the parameter string[] args to the main() method will expose the command arguments. Such as
static class Program
{
public static bool LaunchedViaStartup { get; set; }
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Program.LaunchedViaStartup = args != null && args.Any(arg => arg.Equals("startup", StringComparison.CurrentCultureIgnoreCase));
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
Now the code is simple, we set a static variable to the Program class called LaunchedViaStartup then before the program starts our main form we check if the command arguments contains our special startup argument (via Linq). The name for this argument is simply up to you.
Now in our main form (yes basic) we have access to this property for the lifetime of the application.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
MessageBox.Show(this, string.Format("Lanched Via Startup Arg:{0}", Program.LaunchedViaStartup));
}
}
Finally to test this you can simple open the Project Properties window and set the Command line arguments similar to the screenshot below.
Finally to test the Startup argument outside of visual studio add the startup argument to your shortcut, such as the screenshot below.
Send commend line arguments when you run from start up (define it in shortcut path).
Get the arguments in your application main and take decision based on the arguments. Now, It's up to you how you achieve it.
Check MSDN
Here it is for WinForm
I am using c# and have a console application. Now, to integrate GUI, I added a windows form application. Thing is, the windows form and the console application need to communicate but the functions built inside the console app don't work till the windows form is closed.
Its like the windows form is overlapping it.
From what you write I think I understand that you write your console application code after Application.Run(new Form1());
and so your code probably looks like this:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
//Some more things to do here
}
}
This is not the way to go. a project can be either a console app. or a winforms project.
If you need them both to run together , use two projects.
If you need them to communicate , use a communication object, such as WCF framework server/client.
You can control the lunch behaviour by simply going to Solution property page, Common properties,Start up project. Over there you can specify which program to run first in the action section.
Create a new console project and add a reference to System.Windows.Forms. Add a new form to your project. As an example, I added one button to the form and set it's DialogResult to Ok.
In your program's main method, create an instance of your form and open it using Show or ShowDialog. Here's an example:
static void Main(string[] args)
{
Console.WriteLine("Opening window...");
var result = new TestForm().ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK)
Console.WriteLine("Form closed by button.");
else
Console.WriteLine("Form closed otherwise.");
Console.ReadLine();
}
From your form any location within your program you can use the static Console class to access the console. Here's an example that prints some status info from the form's constructor:
public partial class TestForm : Form
{
public TestForm()
{
InitializeComponent();
Console.WriteLine("Form initialized.");
}
}
If you already have created your project as class library or windows forms project, right-click it, navigate to properties and switch the "Output type" to "Console application".
I have developed a program for Windows 7 and it runs on my computer as it should (in release mode). However, when I copy and paste the project folder to my external HDD and try it on a different computer, it 'runs' but nothing really shows up. I will try to post relevant code:
class App : Application
{
[STAThread()]
static void Main()
{
new App();
}
/// <summary>
/// Starts application with splash screen
/// </summary>
public App()
{
StartupUri = new System.Uri("SplashScreen.xaml", UriKind.Relative);
Run();
}
}
Even though this screen is never visible, my MessageBox is shown.
//constructor
public SplashScreen()
{
//generated method
InitializeComponent();
System.Windows.MessageBox.Show("WHY ME??");
mw = new MainWindow();
mw.Show();
}
After the splash screen, the main window should open, but it doesn't AND this MessageBox never shows up.
public MainWindow()
{
//Windows generated
InitializeComponent();
System.Windows.MessageBox.Show("WHY ME??");
}
As I mentioned, the program runs as it is supposed to in both release and debug mode, but then when I bring it to another computer it only shows "WHY ME??" once instead of twice like it should. Any ideas?
Turns out there was a lot wrong with my code. One of the largest problems I had was hard-coded file paths to the computer it was running on. However, what really helped me with all computer-migration related problems was adding the following code to each class:
In Constructor:
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
Create handling function:
void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
System.Windows.MessageBox.Show(e.ExceptionObject.ToString());
}
It is slow and tetious however it does a pretty good job of assisting in locating source of problems. So there was multiple things wrong with my program, but adding the above code helped solve a lot of issues.
I have been researching how to create a hybrid winform and CLI app... I started out my app as Winforms, now I am adding CLI to it... It seems to work but has a few issues I want to figure out how to fix and have not been able to do so , probably due to my lack of experience with C#.
If i have output type in VS set to "windows application", and use the code below i am able to run the winform portion for GUI, and also from command line am able to give it parameters and it works, well at least it outputs consolewrites i have coded in, i have a seperate c# class file that has my main code so it is seperate from winform GUI and my eventual cli code, they both will just feed user input to this other "main" c# class.. anyway here is the code.
[STAThreadAttribute]
[DllImport("kernel32.dll")]
static extern bool AttachConsole(int dwProcessId);
private const int ATTACH_PARENT_PROCESS = -1;
static void Main(string[] args)
{
if (args.Length == 0)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new form_Main());
}
else
{
AttachConsole(ATTACH_PARENT_PROCESS);
cli_main cli = new cli_main();
cli.start_cli(args);
}
}
well it works in gui, i can access my menus and create different win forms, moment i click a button to perform an action i get the following exception:
System.Threading.ThreadStateException was unhandled by user code
Message=Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. This exception is only raised if a debugger is attached to the process.
if i then change the output type to "console application", it works perfectly in all manners in cli and GUI, BUT when it opens the winform/GUI portion i get this ugly CMD window that will not go away.. here is the code i used, basically just what i started with before i added the above code.
static void Main(string[] args)
{
if (args.Length == 0)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new form_Main());
}
else
{
cli_main cli = new cli_main();
cli.start_cli(args);
}
}
again with my lack of knowledge on C# i am hoping someone can point me to a solution . I would prefer to keep the app as output "console application" and find a way to hide the console that is opened when i start the winform/gui portion..?? thanks in advance.
Did you do what it said in the error message?
Ensure that your Main function has STAThreadAttribute marked on it
In the code you pasted, the STAThread attribute is not marking the Main method, it is marking the AttachConsole external function. Move that to where it ought to be and you shouldn't have any problems.
If your application is a console application, it will get a console window automatically if there isn't already one attached. That's the point of making it a console application. You can use FindWindow and ShowWindow(SW_HIDE) to hide it at runtime but it will still flash on-screen briefly.
If you plan to start your application from an existing console window most of the time, you should keep it as a console application, since it will inherit the parent process's console window by default. If you plan to start your application from a UI shortcut or from other non-console processes, you should probably make it a Windows application and allocate the console as needed.
Thanks for all, but some changes for previous pattern
It works so:
static void Main(string[] args)
{
// Test pattern
args = new string[] { "-flag1", "value1", "-flag2", "value2", "-flag3", "value3" };
if (args.Length == 0)
MainWinApp();
else
MainCLI(args);
}
[STAThread]
static MainWinApp()
{
// Your code for start GUI application
}
[STAThreadAttribute]
static void MainCLI(string[] args)
{
// Your code for CLI application
}
I'd like to create an application using C# that...:
Can be run as a Windows application, with a GUI (it'll indicate progress, status, etc.)
OR
Can be run as a Windows service, without a GUI
Is it possible? Can someone get me started?
I guess the alternative is that I could create a Windows service, and then a separate GUI application which can poll the Windows service to fetch data from it (progress, status, etc.).
In that case... how can I fetch data from the Windows service from my GUI application?
I'm doing something similar to what you're asking for. I have programmed it so that if you send the command line parameter "/form" to the executable, it will pop up a windows form instead of running as a service.
As far as running the background job itself, in both cases you will need to do some sort of threading (perhaps with a timer) to do the work and report status back to your form asynchronously. This would be a whole different discussion topic on creating threaded GUI apps.
The "form or service" code goes something like this:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
private static void Main(string[] args)
{
if (args.Length > 0 && args[0] == "/form")
{
var form = new MainForm();
Application.Run(form);
return;
}
var ServicesToRun = new ServiceBase[]
{
new BackgroundService()
};
ServiceBase.Run(ServicesToRun);
}
}
I've never done an app that can be run as a windows service or a GUI, but we have a lot of apps that you can switch between console app and windows service using a compiler flag. (I just saw the answer with the cmd line arg - that might even be better!)
We usually then just use a compiler flag to switch between the two. Here's an example... I didn't completely think this through, but it might give you a start:
#define RUN_AS_SERVICE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.ServiceProcess;
namespace WindowsFormsApplication1
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
#if RUN_AS_SERVICE
System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[]
{
new MyService()
};
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
#else
// Run as GUI
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
#endif
}
}
public class MyService : ServiceBase
{
protected override void OnStart(string[] args)
{
// Start your service
}
protected override void OnStop()
{
// Stop your service
}
}
}
Build the actual application in libraries. You can then add any UI (I say this loosely, as service is not really a UI) you desire. As long as you think of the app as a windows aplication and a service application, you are developing two applications. If you think of the application as the business problem it solves, you will then think of the windows forms UI and the service UI, which is much saner for your needs.
While this may sound sane, you would be surprised how many applications need a complete overwrite to become a different UI type. thank you for the question. It convinces me there is a need for the book I am writing. :-)
You should go with the latter option. Create your service and then a seperate GUI app. Most of the plumbing for doing all this is already provided for you in the framework. Have a look at the ServiceController class.
I saw this thread, it might have some more info for you
How to write c# service that I can also run as a winforms program?