invisible c# windows application

  • Thread starter Thread starter christopher green
  • Start date Start date
C

christopher green

I am writing a C# windows application but I don't want to display any
forms, effectively I want it to be invisible on the desktop apart from
an icon in the system tray. How can I do this? I know I can start an
application minimised but I don't want to display any forms.

Thanks for your assistance.
 
A) Don't create any forms
B) If for some reason you have to, then use form.Hide();
 
christopher green said:
I am writing a C# windows application but I don't want to display any
forms, effectively I want it to be invisible on the desktop apart from
an icon in the system tray. How can I do this? I know I can start an
application minimised but I don't want to display any forms.

Just don't create any forms then. There's nothing forcing you to.

I don't know offhand how you put an icon in the system tray, but I'm
sure there's code around the net to help you with that part.
 
I am writing a C# windows application but I don't want to display any
forms, effectively I want it to be invisible on the desktop apart from
an icon in the system tray. How can I do this? I know I can start an
application minimised but I don't want to display any forms.

A WinForms app doesn't *have* to have any forms in it - it's not
compulsory.. :-)
 
Hi

This is the code I'm using to do that, I saw a post today that do not
require P/invoke but I havent test it yet

using System.Runtime.InteropServices;

[DllImport("user32.dll",EntryPoint="SetForegroundWindow")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern bool MessageBeep(int type);
[DllImport("user32.dll")]
public static extern int SetWindowLong( IntPtr window, int index, int
value);
[DllImport("user32.dll")]
public static extern int GetWindowLong( IntPtr window, int index);
[DllImport("winmm.dll")]
public static extern int sndPlaySound(string lpszSoundName , int uFlags)
;


const int GWL_EXSTYLE = -20;
const int WS_EX_TOOLWINDOW = 0x00000080;
const int WS_EX_APPWINDOW = 0x00040000;

//This is the icon when there are no orders in the system
Icon normalIcon;


public Form1()
{
this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);

// The Icon property sets the icon that will appear
// in the systray for this application.
notifyIcon1.Icon = normalIcon;

// The ContextMenu property sets the menu that will
// appear when the systray icon is right clicked.
notifyIcon1.ContextMenu = this.contextMenu1;

// The Text property sets the text that will be displayed,
// in a tooltip, when the mouse hovers over the systray icon.
notifyIcon1.Text = "the text";
notifyIcon1.Visible = true;


//Remove from the taskbar and also from alt+tab
this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
this.Visible = false;
this.ShowInTaskbar = false;

int windowStyle = GetWindowLong(Handle, GWL_EXSTYLE);
SetWindowLong(Handle, GWL_EXSTYLE, windowStyle | WS_EX_TOOLWINDOW);
}


Cheers,
 
Ignacio said:
Hi

This is the code I'm using to do that, I saw a post today that do not
require P/invoke but I havent test it yet

using System.Runtime.InteropServices;

[DllImport("user32.dll",EntryPoint="SetForegroundWindow")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern bool MessageBeep(int type);
[DllImport("user32.dll")]
public static extern int SetWindowLong( IntPtr window, int index,
int
value);
public static extern int GetWindowLong( IntPtr window, int index);
[DllImport("winmm.dll")]
public static extern int sndPlaySound(string lpszSoundName , int
uFlags)


I suggest also to the the "System.Security.SupressUnmanagedCodeSecurity"
for better performance.

So change the attributes to:
[DllImport("xxx.dll"), System.Security.SupressUnmanagedCodeSecurity]


--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp

Do you need daily reports from your server?
http://sourceforge.net/projects/srvreport/
 
Back
Top