minimize app to system tray - no icon appears - c#

i need to minimize app to system tray (see my icon there).
But after starting the app, the icon dissapears from taskbar (that is fine) but i cannot see it in system tray (that is bad).
Where can be a mistake, please?
PS: i am using WPF.
This is inner code of my event:
System.Windows.Forms.NotifyIcon notifyIcon = new System.Windows.Forms.NotifyIcon();
if (WindowState.Minimized == this.WindowState)
{
notifyIcon.Visible = true;
notifyIcon.BalloonTipText = "Radek app";
notifyIcon.BalloonTipTitle = "Welcome Message";
notifyIcon.BalloonTipIcon = System.Windows.Forms.ToolTipIcon.Info;
notifyIcon.ShowBalloonTip(3000);
this.ShowInTaskbar = false;
}
else if (WindowState.Normal == this.WindowState)
{
this.WindowState = WindowState.Normal;
this.ShowInTaskbar = true;
notifyIcon.Visible = false;
}

That Info icon is for the balloon, not the TrayIcon itself, you should add your image (I recommend 16x16px png file) to your application resources, then you can use it like:
var iconHandle = Properties.Resources.YourIconImage.GetHicon();
NotifyIcon.Icon = System.Drawing.Icon.FromHandle(iconHandle);

You need to set the Icon as shown below:
notifyIcon.Icon = new System.Drawing.Icon(Path to your Icon);

Related

Can not Print in WPF

I have such a problem here. I have a window in my WPF project, and have a button, with what I want To print With printer that page. My click event for that button is this.
PrintDialog dlg = new PrintDialog();
Window currentMainWindow = Application.Current.MainWindow;
Application.Current.MainWindow = this;
if ((bool)dlg.ShowDialog().GetValueOrDefault())
{
Application.Current.MainWindow = currentMainWindow;
}
When I click on buton, the Print dialog is popped out.
Here
But When clicking on print, nothing happenes, the dialog is just closing, and no results, it is not working not with Adobe PDF, not with ARX CoSign...
What to do ? Thanks
The rough working
var printDoc = new PrintDocument()
var dlg = new PrintDialog()
If(dlg.ShowDialog() == DialogResult.OK)
{
printDoc.Document = [doc to print]
printDoc.Setting = dlg.settings
PrintDoc.print()
}
enter code here
PrintDialog dlg = new PrintDialog();
Window currentMainWindow = Application.Current.MainWindow;
Application.Current.MainWindow = this;
if ((bool)dlg.ShowDialog().GetValueOrDefault())
{
Application.Current.MainWindow = currentMainWindow; // do it early enough if the 'if' is entered
dlg.PrintVisual(this, "Certificate");
}

System tray message alert when result > 0

There is a function that user can send message each other and it's data saved to table. And I wrote that simple code below that check's new message from database:
lDataParameter.Add("msg", _msgEnd);
ultragrid1.DataSource = _msgEnd.Tables[0];
if (ultragrid1.Rows.Count > 0)
{
ultragrid1.Rows[0].Selected = true;
MessageBox.Show("You have" + ultragrid1.Rows.Count.ToString() + " 1 new message");
}
It works! Now I want to display that message box on system tray however app closed...
How to get my app on system tray?
Thanks guys. I did it.
public Form1()
{
InitializeComponent();
this.components = new System.ComponentModel.Container();
this.contextMenu1 = new System.Windows.Forms.ContextMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
// Initialize contextMenu1
this.contextMenu1.MenuItems.AddRange(
new System.Windows.Forms.MenuItem[] { this.menuItem1 });
// Initialize menuItem1
this.menuItem1.Index = 0;
this.menuItem1.Text = "E&xit";
this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
// Set up how the form should be displayed.
this.ClientSize = new System.Drawing.Size(292, 266);
this.Text = "Notify Icon Example";
// Create the NotifyIcon.
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 = new Icon("Icon.ico");
// 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 = "Form1 (NotifyIcon example)";
notifyIcon1.Visible = true;
// Handle the DoubleClick event to activate the form.
notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_DoubleClick);
}
Now I want to create more menu this.
menuItem1.Index = 1;
this.menuItem1.Text = "I&nfo";
How to create with it?

NotifyIcon and Form.Resize event in Windows application

I am trying to minimize my winapp to system tray. I have downloaded a sample project from codeproject. But it goes to systary on Form.Resize event. Code -
private void Form_Resize(object sender, EventArgs e)
{
notifyIcon1.BalloonTipTitle = "Minimize to Tray App";
notifyIcon1.BalloonTipText = "You have successfully minimized your form.";
if (FormWindowState.Minimized == this.WindowState)
{
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(500);
this.Hide();
}
else if (FormWindowState.Normal == this.WindowState)
{
notifyIcon1.Visible = false;
}
}
Is it necessary to handle it on resize event? Can i do it on button click event?
You can do this in your button. For obvious reasons you cannot rely on the WindowState in your button, because it can only be clicked when the window is no minimized to tray anyway.
private void button1_Click(object sender, EventArgs e)
{
notifyIcon1.BalloonTipTitle = "Minimize to Tray App";
notifyIcon1.BalloonTipText = "You have successfully minimized your form.";
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(500);
this.Hide();
}
This should work to "minimize" to tray. Although it should really be called hide-on-button-click-to-tray.

register desktop application shorcut key in shell of window

I want to register my desktop application shortcut in windows shell.so that if my desktop application is in minimized mode then i want to run the shortcut key i.e is set by me in desktop application that i have created.
My main motive is that when my application is in minimized mode then i want to run a shortcut key to take screenshot of active screen.
I have tried the below code that will minimized my desktop application.
public TemplatesCreation()
{
InitializeComponent();
trayMenu = new ContextMenu();
trayMenu.MenuItems.Add("Exit", OnExit);
trayIcon = new NotifyIcon();
trayIcon.Text = "MyTrayApp";
trayIcon.Icon = new Icon(SystemIcons.Application, 40, 40);
trayIcon.ContextMenu = trayMenu;
trayIcon.Visible = true;
protected override void OnLoad(EventArgs e)
{
Visible = true; // Hide form window.
ShowInTaskbar = false; // Remove from taskbar.
base.OnLoad(e);
}
private void OnExit(object sender, EventArgs e)
{
Application.Exit();
}
Thanks in advance

Set system tray notifyicon.icon to a pic in images folder

I've tried several things and eventualy just put the image directly inside C:\Users\Gebruiker\Documents\Visual Studio 2012\Projects\FolderMonitor\FolderMonitor\bin\Debug. This works for now, but idealy I'd like to set the notifyIcon.Icon = new Icon("folder.ico") to an image inside a images folder in the solution. But I can't figure out how this works..
public FolderMonitorApplicationContext()
{
this.monitor = new Monitor();
notifyIcon = new NotifyIcon();
notifyIcon.Icon = new Icon("folder.ico");
notifyIcon.Text = "Folder Monitor";
notifyIcon.Visible = true;
contextMenu = new ContextMenuStrip();
openMonitor = new ToolStripMenuItem();
exitApplication = new ToolStripMenuItem();
notifyIcon.ContextMenuStrip = contextMenu;
notifyIcon.DoubleClick += NotifyIcon_DoubleClick;
openMonitor.Text = "Open";
openMonitor.Click += new EventHandler(OpenMonitor_Click);
contextMenu.Items.Add(openMonitor);
exitApplication.Text = "Exit..";
exitApplication.Click += new EventHandler(ExitApplication_Click);
contextMenu.Items.Add(exitApplication);
}
So it's currently working, but not how it i'd like it to work. Hope you can help me out here, thanks in advance.
After adding the picture file to your project, bring up the item properties by clicking on it, and pressing F4. Under Build Action, change it to "Embedded Resource".
You can access embedded resources in Stream form like this:
public FolderMonitorApplicationContext()
{
this.monitor = new Monitor();
notifyIcon = new NotifyIcon();
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(
"<project namespace>.<folder path>" + "filename.ico"))
{
notifyIcon.Icon = new Icon(stream);
}
notifyIcon.Text = "Folder Monitor";
notifyIcon.Visible = true;
contextMenu = new ContextMenuStrip();
openMonitor = new ToolStripMenuItem();
exitApplication = new ToolStripMenuItem();
notifyIcon.ContextMenuStrip = contextMenu;
notifyIcon.DoubleClick += NotifyIcon_DoubleClick;
openMonitor.Text = "Open";
openMonitor.Click += new EventHandler(OpenMonitor_Click);
contextMenu.Items.Add(openMonitor);
exitApplication.Text = "Exit..";
exitApplication.Click += new EventHandler(ExitApplication_Click);
contextMenu.Items.Add(exitApplication);
}
Use the same method to insert the icon on the form.
To find the same code in your application to copy in your systemtray please:
check if, in the file .resx, there is the icon called with $ symbol(example:"$this.icon")
find your code to include the icon on the [name of form].desiner.cs.
example of code in my application:
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(systemtray));
trayIcon.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));

Categories