Running an windows app in memory

  • Thread starter Thread starter cameljs18
  • Start date Start date
C

cameljs18

Okay well maybe my heading isn't exactle right but what i want to do
is create a windows application that loads when the machine starts up
but instead of having it on the screen i just want it to be an icon on
the start bar in the right corner eg. Like where speaker volume
control is for instance. On top of this i also want to display a
message from that icon like a pop up warning.Is this possible
 
Actually, that is "on screen"; anyway, something like this? Note; you
actually want "at login", not "at startup", since it must be part of
the user's desktop session. A simple option is the user's start menu,
although there are many ways to auto-run something (mainly via the
registry).

Marc

Application.EnableVisualStyles();
using (NotifyIcon icon = new NotifyIcon())
{
icon.Visible = true;
icon.Icon = System.Drawing.SystemIcons.Shield;
icon.Text = "Boo!";
icon.BalloonTipText = "I'm a happy camper";
icon.BalloonTipTitle = "Jack says";
icon.BalloonTipIcon = ToolTipIcon.Warning;
icon.ShowBalloonTip(5000); // I guess via an event or
something
Application.Run();
}
 
btw, to add an exit menu:

[etc]
using (NotifyIcon icon = new NotifyIcon())
using (ContextMenuStrip menu = new ContextMenuStrip())
{
menu.Items.Add("Exit", null, delegate
{Application.Exit();});
icon.ContextMenuStrip = menu;
icon.Visible = true;
[etc]
 
btw, to add an exit menu:

[etc]
using (NotifyIcon icon = new NotifyIcon())
using (ContextMenuStrip menu = new ContextMenuStrip())
{
menu.Items.Add("Exit", null, delegate
{Application.Exit();});
icon.ContextMenuStrip = menu;
icon.Visible = true;
[etc]

Thanks a mil just the answer i wanted
 
[OP mailed me off-group]
I cant get the ContextManuStrip to show

Since it still relates to the same problem, I've answered back on-
group; below is a full example that works fine for me. If it doesn't
work, can you indicate what happens? The context menu should appear
when you right-click on the shield in the task bar. Also - if it
doesn't work could you indicate what OS you are using?

Cheers,

Marc

using System.Windows.Forms;
static class Program
{
static void Main()
{
Application.EnableVisualStyles();
using (NotifyIcon icon = new NotifyIcon())
using (ContextMenuStrip menu = new ContextMenuStrip())
{
icon.ContextMenuStrip = menu;
menu.Items.Add("Quit", null, delegate {
Application.Exit();
});
menu.Items.Add("Balloon", null, delegate {
icon.ShowBalloonTip(5000, "Itsy", "Bitsy",
ToolTipIcon.Info);
});
icon.Visible = true;
icon.Icon = System.Drawing.SystemIcons.Shield;
icon.Text = "Boo!";
Application.Run();
}
}
}
 
Also - if ContextMenuStrip simply doesn't work, you might also try the
older ContextMenu:

using System.Windows.Forms;
static class Program
{
static void Main()
{
Application.EnableVisualStyles();
using (NotifyIcon icon = new NotifyIcon())
using (ContextMenu menu = new ContextMenu())
{
icon.ContextMenu = menu;
menu.MenuItems.Add("Quit", delegate {
Application.Exit();
});
menu.MenuItems.Add("Balloon", delegate {
icon.ShowBalloonTip(5000, "Itsy", "Bitsy",
ToolTipIcon.Info);
});
icon.Visible = true;
icon.Icon = System.Drawing.SystemIcons.Shield;
icon.Text = "Boo!";
Application.Run();
}
}
}
 
Marc Gravell wrote : a nifty example of using a NotifyIcon in the
Program.cs at the start of a WinForms application (code snipped for
brevity).

Hi Marc,

In my development environment (Win XP, VS 2005, .NET 2.0) the call to :

Application.SetCompatibleTextRenderingDefault(false);

Must also occur before encountering any code that creates a WinForms object,
like a NotifyIcon.

I find the setting of the ShowBalloonTip of the icon parameter makes no
difference : the BalloonTip persists the same amount of time. This is true
whether I am running from within Visual Studio 2005 in debug mode, or if I
am running the application from a compiled .exe file.

A click event handler for the Icon does work nicely when added to your
example :

// note : using this "as is" does not filter out context-clicks,
// so if you are using a ContextMenuStrip on the NotifyIcon
// you need to write code to prevent a mess ...
//
icon.Click += new EventHandler
(
delegate
{
// Console.WriteLine("notify icon clicked");
icon.ShowBalloonTip(4000);
}
);

A side-effect, also, is sometimes ending up having more than one copy of the
icon in the taskbar, or having the icon persist in the system tray after you
close the app : although when you go mouse over the system tray, the
"ghosts" disappear. Refresh issue ?

I believe I experimented with a similar technique some months ago, and ended
up concluding that it was better to "go the whole hog" and write some
minimize to system tray code.

Does wrapping Application.Run in the clause of a Using statement have any
side-effects : it appears not, but I wonder ...

best, Bill Woodruff
dotScience
Chiang Mai, Thailand

"The greater the social and cultural differences between people, the more
magical the light that can spring from their contact." Milan Kundera,
Testaments Trahis
 
The side-effect of orphaned task icons happens when a process exits
unexpectedly; this is common when hitting "stop" in the debugger. The
app I'm working on at the moment has a tray icon; the last time I
glanced I had ~20 orphans from debugging sections of code ;-p Of
course, a wave of the mouse removes them. It shouldn't be a big
problem in live unless you routinely use task manager to kill exes.

The dispose should make no difference; the Run() will block until
Exit() is called, at which point you *want* everything to be
Dispose()d.

I wasn't sure how much of the rest was statement, and how much was
question/problem; is there anything else you need help with?

Marc
 
Hi Marc,

The problem I mentioned with the BalloonToolTip not going away as you might
expect depending on its timer value is, evidently, a well-known problem :

http://blog.lyalin.com/2007/12/showballoontip-problem-in-notifyicon.html

Has nothing to do with your code.

I found this link by going to MSDN and looking at the NotifyIcon docs.
Should have checked that first ! : sorry.

The issue of detecting a right-click in such a context is more interesting :
I thought I had once done that by testing the return value of the static
System.Windows.Forms.Control.MouseButtons in the usual way, but in that
place in the start-up process the mousebuttons state remains "none" (because
there's no Form ? no message pump ? duhhh ?). Maybe have to use an API ?

best, Bill Woodruff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top