There are many cases where an application minimized to tray bar is very useful, generally for all applications running in the background (in background), such as antivirus, firewalls, etc..
The. NET framework provides us with a control "NotifyIcon" with which we can minimize our Windows Form application in the tray bar.
We will see an example, where we write a few lines of code in C # to minimize an application in the tray bar and restore it by double clicking on it.
- Open an existing Windows Form project, or create a new one.
- From the toolbar search NotifyIcon control and dredging the base form
- Set starting the following properties:
- Name: name of control, in my case I named it as "TrayIcon"
- Text: text with the name you want to appear when you position the mouse on the icon in the tray.
- Icon: the icon you want to display the application in the tray bar.
- Visible: false, that the application to start is not seen on the tray, we will do just visible when minimized.
- Add an event handler to the form's Resize event with the following code:
private void formBase_Resize (object sender, EventArgs e)
{ if (WindowState == Minimized FormWindowState.) { Hide (); traybar. Visible = true; } }
- Add an event handler to NotifyIcon.DoubleClick with following code:
private void trayBar_MouseDoubleClick (object sender, MouseEventArgs e)
{ Show (); WindowState = FormWindowState. Normal; traybar. Visible = false; }
At this point, our application is already minimized in the taskbar tray (instead of the taskbar) and will not appear until you double click on the icon in the System Tray.
If you also want to add a context menu, these are the steps to follow:
- Drager ContextMenuStrip control to your form.
- Right-clicking on the ContextMenuStrip control inserted, select Edit Menu.
- Add contextual menu options, such as "Restore" and "Close".
- Write the code to the click events of each of the menu options:
- Finally, set starting in the NotifyIcon ContextMenuStip property with the new contextual menu you just created.
private void toolStripMenuItemRestaurar_Click (object sender, EventArgs e)
{ Show (); WindowState = FormWindowState. Normal; traybar. Visible = false; }private void toolStripMenuItemSalir_Click (object sender, EventArgs e)
{ Application. Exit (); }
0 comments:
Post a Comment