Windowless tray icon application - c#

OK I am a total newbie at WPF but I have to develop what's in the title with wpf but not relying on MVVM.
I have followed this:
WPF Application that only has a tray icon
I found the first answer the one with the hardcodet lib but find it too difficult and MVVM biased.
So followed the second one and everything seemed fine except that in the end it doesn't work.
So in my App.xaml.cs I have put:
public partial class App : Application
{
private System.Windows.Forms.NotifyIcon notifyIcon = null;
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
UnityCore.Initialize();
}
protected override void OnActivated(EventArgs e)
{
notifyIcon = new System.Windows.Forms.NotifyIcon();
notifyIcon.Click += NotifyIcon_Click;
notifyIcon.DoubleClick += NotifyIcon_DoubleClick;
Stream iconStream = Application.GetResourceStream(new Uri("pack://application:,,,/Resources/Images/ITA.png")).Stream;
notifyIcon.Icon = new System.Drawing.Icon(iconStream);
notifyIcon.Visible = true;
base.OnActivated(e);
}
private void NotifyIcon_DoubleClick(object sender, EventArgs e)
{
Console.Beep();//show main window
}
private void NotifyIcon_Click(object sender, EventArgs e)
{
Console.Beep();//show main window
}
}
The main window at start is transparent of minimized to make it invisible.
At this stage I hoped to see the ITA flag in the tray icon and then at click or double click restore the main window.
But I don't see a damn thing in the tray.
I think that the flag resource is properly set here's my solution
Thanx for any help.

Related

Who is displaying the ContextMenu: the form or the notification icon?

I have the same ContextMenu assigned to a Form and to a NotifyIcon.
this.ContextMenu = this.contextMenu;
this.notifyIcon.ContextMenu = this.ContextMenu;
In the Popup event of the context menu I'm trying to find out who is displaying the context menu: the form (such as a right click on the form), or the notify icon (a right click on the notification icon):
private void ContextMenu_Popup(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine(this.contextMenu.SourceControl.Name);
}
However, I always get the form as the source control, even if I right click on the notification icon.
I'm using C#, .NET Framework 4.6 and Windows Forms.
Maybe, a quickest way is to have a flag:
private bool fromIcon;
private void notifyIcon1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
fromIcon = true;
}
}
private void ContextMenu_Popup(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine(fromIcon.ToString());
fromIcon = false;
}

C# set icon of shortcut when setting icon of application

I'm trying to set the icon of my application (visible in the taskbar). The icon is correct when I run the .exe itself or run it from visual studio, but this doesn't apply when starting from a shortcut.. The icon inside the application itself, top left corner, is correct.
Code used to set the icon:
var assemblyDirectory = Directory.GetCurrentDirectory();
var iconUri = new Uri(Path.Combine(assemblyDirectory, resourceName), UriKind.Absolute);
Icon= BitmapFrame.Create(iconUri);
I'm trying to add a red circle indicating changes in my application to the users, which is done by changing between 2 icons.
Any idea on how to set the icon of a shortcut at runtime, or about how to show a red circle in the taskbar-icon indicating changes?
Although it is impossible to set the assembly icon during runtime (it is read-only), you can use NotifyIcon to display an icon in system tray and that is fairly easy to change during runtime.
Here is an example of how to implement a NotifyIcon:
https://www.codeproject.com/Tips/627796/Doing-a-NotifyIcon-program-the-right-way
Note: Since you are using WPF, you need to include a reference to Windows.Forms in your application.
Edit: just in case the link goes dead, here some code example for Program.cs, needs an "icon" imported as resource:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MyCustomApplicationContext());
}
public class MyCustomApplicationContext : ApplicationContext
{
public static NotifyIcon trayIcon;
public MyCustomApplicationContext()
{
trayIcon = new NotifyIcon()
{
Icon = icon,
ContextMenu = new ContextMenu(new MenuItem[]
{
new MenuItem("Exit", Exit),
new MenuItem("Do something", DoSomething),
}),
Visible = true,
BalloonTipText = "My Application",
BalloonTipTitle = "My Application",
Text = "My Application Text"
};
trayIcon.Click += new System.EventHandler(trayIcon_Click);
}
private void trayIcon_Click(object sender, System.EventArgs e)
{
//Do something
}
void DoSomething(object sender, EventArgs e)
{
}
void Exit(object sender, EventArgs e)
{
// Hide tray icon, otherwise it will remain shown until user mouses over it
trayIcon.Visible = false;
Application.Exit();
}
}

XAML enable/disable view from another view

I'm a beginner and having some dicciculties with XAML.
I have a main view A, which has a button to open a pop-up-window B. When this happens, Window A should still be visible and openened, but disabled. I've done this in code behind (maybe not the cleanest way, but the only way I know how). The code i used for this is the following:
//Code behind from view A
private void X-Button_Click(object sender, RoutedEventArgs e)
{
var BWindow = new BView();
BWindow.Show();
this.IsEnabled = false;
}
I would like to get window A enabled again once i close window B, but i can t seem to get this work. Any help would be very much appreciated.
You could do it in the following way.
You register yourself on the Closed event of the window, and when it gets closed, you unregister the event, and re-enable the this form.
private void Button_Click(object sender, RoutedEventArgs e)
{
Window BWindow = new BWindow();
BWindow.Show();
BWindow.Closed += BWindow_Closed;
this.IsEnabled = false;
}
void BWindow_Closed(object sender, EventArgs e)
{
Window win = sender as Window;
if (win != null)
{
win.Closed -= BWindow_Closed;
}
this.IsEnabled = true;
}
I assume you are looking for modal window. See similar question asked here: How do make modal dialog in WPF?
The solution is in using ShowDialog method from Window class. See here for reference:
http://msdn.microsoft.com/en-us/library/system.windows.window.showdialog.aspx
Modal window is the concept when you open new window B from the existing window A. While the B is open, the A is disabled and cannot be used. Window A become active only when the B is closed.

c# make a program minimize to task bar

I am trying to make my application minimize to task bar/tray
Here is my code so far that I have pulled together from other SO posts and other people seem to have it working but my app minimizes to the tray but when I click it in the tray it doesn't reopen.
private void Form1_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
this.notifyIcon1.Visible = true;
this.notifyIcon1.ShowBalloonTip(500);
this.Hide();
}
}
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
}
just to explain again the problem is the application minimizes to the tray but when I click on the icon it doesn't restore the app to normal. Instead it does nothing.
I found my problem.
what I didn't do was this
Set the NotifyIcon's visible property to false in the property editor. Now go to the property editor of Form1, click on the little lightning symbol to access the events, and double click on the Resize event, and change the code to:
and I also didn't do this
Finally we need the code to make the program show up again when the icon is double clicked. So double click on NotifyIcon1 in the designer,
I found this information here
Dreamincode
This is what I have used in the past but I fund a copy online at the link below
if (FormWindowState.Minimized == this.WindowState)
{
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(500);
this.Hide();
}
found here: http://www.codeproject.com/Articles/27599/Minimize-window-to-system-tray

How to call the mainwindow object from a second object created from main window in c# .net

Being a very first user in Windows Form Development I want to ask a simple question ...
I created a form(MainWindow.cs) within the solution which opens at the time of running that solution.
Latter I created a second form(SecondWindow.cs) and created a event so that it can be called from the first window by clicking a button.When the second window loded up the first window(MainWindow.cs) will be disabled.
Now I want to enable the MainWindow.cs when the second window is closed.
How to do That...
A simple solution I already have is to hide the MainWindow.cs and latter on closing the second window make a new object of first window and show it.But it is not a good way i think because there is already a object created by .net framework for first window automatically, so why we should create a new object of Mainwindow.cs .
Code Of First Window ( MainWindow.cs ) :
private void priceControllToolStripMenuItem_Click(object sender, EventArgs e)
{
SecondWindow price = new SecondWindow();
this.Enabled = false;
price.Show();
}
Code Of Second Window ( On closing SecondWindow.cs )
private void price_controll_FormClosed(object sender, FormClosedEventArgs e)
{
// what will goes here to make the MainWindow.cs to enable state
}
Use price.ShowDialog() to show second form as modal dialog. Main form will be disabled until you close second form:
private void priceControllToolStripMenuItem_Click(object sender, EventArgs e)
{
using(SecondWindow price = new SecondWindow())
price.ShowDialog();
}
You can pass the main form as an owner to the second form
private void priceControllToolStripMenuItem_Click(object sender, EventArgs e)
{
SecondWindow price = new SecondWindow() { Owner = this };
this.Enabled = false;
price.Show();
}
Then you can reference it from the second form.
private void price_controll_FormClosed(object sender, FormClosedEventArgs e)
{
Owner.Enabled = true;
}

Categories

Resources