How to run .net windows forms application without visible windows?

I have a .net windows forms application that needs to be opened directly on the notification icon (in the system tray) without visible windows. I understand that I can do this in an onshown event or something like that. But if I do this, I will get a window flash. How can I avoid this outbreak? I tried to modify my Program.cs file to look like this:

 Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); MainForm frm = new MainForm(); frm.Visible = false; Application.Run(frm); 

However, this does not work because Application.Run() makes the view visible. I am pretty sure that there is a simple answer that I am missing. Any help is appreciated.

+4
source share
5 answers

There is an overload for Application.Run() , which does not accept any parameters and thus does not immediately display the form when the application starts. Of course, you will need to control what causes the application to stop working, as there is no initial or β€œbasic” form for monitoring it. For example, it will be your notification icon, which I am sure you can handle.

+4
source

If you do not need the main form at the time of launching your application, here is a link to an article that describes how to create only NotifyIcon.

+3
source

You can try setting WindowState to frm for Minimized along with ShowInTaskbar to false. Also, I'm not an expert, but I think you should handle the visibility logic in the MainForm constructor.

+1
source

Here is a code snippet from form I initialization method that does just that. The application launches in the tray, and a window shows when the user double-clicks the notification icon. I have methods that handle resizing, etc., which guarantee that the form will be closed only through the menu item.

 public MainForm() { ...code Resize += MainForm_Resize; notifyIcon.DoubleClick += NotifyIconDoubleClick; WindowState = FormWindowState.Minimized; Hide(); } private void MainForm_Resize(object sender, EventArgs e) { if (FormWindowState.Minimized == WindowState) Hide(); } private void NotifyIconDoubleClick(object sender, EventArgs e) { Show(); try { WindowState = FormWindowState.Normal; ...more code for other stuff }catch(yadda yadda) ...code } } 
0
source

Perhaps a little hacky, but you can create a borderless form ( FormBorderStyle.None ) and set the TransparencyKey for it BackColor on it, turn off ShowInTaskbar , and then pass this Application.Run () form. Voila. :)

0
source

Source: https://habr.com/ru/post/1311926/


All Articles