I want to launch an application minimized or not depending on it was launched by system on startup (Windows) or not minimized if it was launched by user (double clicked on it).
I've made a converter program so far which will open in windows start up. I've used this to success.
RegistryKey rk = Registry.CurrentUser.OpenSubKey
("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
rk.SetValue("MyApplicationName", Application.ExecutablePath);
rk.Close();
rk.Dispose();
I've only put these codes to startup. I don't check if it's already in regedit because its not adding if it's already there. After I've set these codes to my program I've restart my computer and my program came at startup but it come in center of my screen like normal startup. Can I check if my program is started by windows on startup and set it to startup? Normally my program hides itself to system tray if I click "x" on top right corner. I've to right click->exit to actually close my program.
My question is: Is there any way to check how was launched by the system (Windows) or by the user?
As they are both started with the same user it is going to be hard to detect. As an alternative, you could have a parameter in your application telling it to start reduced or not. By default it opens the window and if the parameter is set you do not show it. You will just have to add this parameter in the execution command stored in the registry.
Here are some code:
Program.cs, you look if the -minimized argument is provided and you pass this information to your Form class
using System;
using System.Linq;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
static class Program
{
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1(args.Contains("-minimized")));
}
}
}
Form1.cs: if the boolean minimized is true, start minimized
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1(bool minimized)
{
InitializeComponent();
if (minimized)
{
WindowState = FormWindowState.Minimized;
}
}
}
}
Then you can start your program providing it the -minimized argument:
.\WindowsFormsApp1.exe -minimized
or not
.\WindowsFormsApp1.exe
If I understood correctly, you want to launch your application minimized, right?
In that case, you might need to add this code:
If you're developing a WinForms app
this.WindowState = FormWindowState.Minimized;
If you're developing a WPF app
this.WindowState = WindowState.Minimized;
Related
I would like to run my winForm through the button I have created in Revit API, but I am new in this field and I am a bit stuck at the moment.
Here in my Command.cs I am stating what button does after clicking on it. Where instead of displaying "Hello World" I would like it to open my winForm.
Is there any way how can I do that? Do I need to somehow link my winForm application to this one?
namespace SetElevation
{
[Transaction(TransactionMode.Manual)]
class Command : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
TaskDialog.Show("SetElevation", "Hello World!");
return Result.Succeeded;
}
}
}
Here is my winForm Program.cs application
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinFormTest
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
welcome to Revit addin development. You’re close.
I presume if you made your addin work above, that it is compiling as a DLL, not a standalone EXE. Your Winform app appears to be a separate EXE application.
To make this work, you’ll want to add your Form1 to the DLL project. Once you’ve got it in there, you can change TaskDialog.Show to instead these two lines:
var myForm - new Form1();
myForm.ShowDialog();
With that, you’re on your way.
I have a cmd application when i pressed insert while aplication is maximized run normal
but when application is minimized not work
What could I do
please help me
using System;
using System.Runtime.InteropServices;
class MainClass
{
static void Main()
{
ConsoleKeyInfo keypress;
keypress = Console.ReadKey(); // read keystrokes
if (keypress.Key == ConsoleKey.Insert)
{
Console.Write("One ");
}
}
}
not recognized key insert while is minimized
I think, you are looking for Global Hooks.
How can it work when the command line is minimized? It doesn't have any focus and your key strokes aren't registered.
The same can be said for almost any program - try it with notepad for instance
Edit: if you want to register to global key events, use glob hooks like TcKs suggested
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 a Visual C# project using windowsforms that does not exit when the X is clicked on any form OTHER than the first form. I think this may have something to do with my form switching?
Currently, I have a Template.CS which is exactly what it sounds like. All of my usage forms extend this by:
public partial class Welcome : ADL.template
Then, I switch between forms by invoking this method:
public static void formSwitch(Form in_form, Form out_form)
{
in_form.Hide();
out_form.Show();
}
Called by:
Program.formSwitch(this, new frmUserInput());
What i think is happening here is, the X is closing the Form NOT the application because the starting form is Hidden, not closed. Is there a better way for me to switch between forms?
Thanks!
Well before answering your question, I should point out that Hide doesn't actually close your form, it only (as the name implies) hides it. So as time goes on, you'll keep piling on forms until you either run out of GDI objects or out of memory, either way you'll crash.
You are kind of correct about the reason why your application isn't closing though: even though you close the current form, all your other forms are still loaded so your application won't end.
To fix this, the best way would be to actually close your forms when you don't need them anymore. You won't even have to add any code to close your application then.
Now if you don't want to do that for whatever reason, you can always just call Application.Exit. I strongly discourage you to pursue this "solution" though.
Edit: as for a possible solution, you could change Program.cs to something like:
static class Program
{
static Form NextForm=new frmLogin(); // or whatever your first form is
static public void SetNext(Form next) { NextForm=next; }
static void Main()
{
while(NextForm!=null)
{
Form _next=NextForm;
NextForm=null; // so it closes at the end
Application.Run(NextForm);
}
}
}
And then your formSwitch would become:
public static void formSwitch(Form in_form, Form out_form)
{
Program.SetNext(out_form);
in_form.Close();
}
It looks weird because your workflow is weird for a Windows program. This is more the workflow of a 1970 FORTRAN program running in DOS.
The default Windows Forms application behaviour is: the application is closed when the MAIN window is closed.
The main window is the first one you've created. When you called Hide() on this window, it is rendered invisible (but still exists). So, closing the second window doesn't close the application.
You can edit the code in the ~/Program.cs file as the following:
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form f = new Form();
f.Show();
Application.Run();
}
}
This way, the application wont close until you call Application.Exit() exclusivly.