Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I've deployed a project on client PC. Is it possible to add a shortcut key to invoke this program. How to make it minimized to the tray when it's triggered? (User can still open it by clicking the icon in the tray) Is it possible to do this on programming level?

share|improve this question

migrated from stackoverflow.com Aug 23 '12 at 19:33

This question came from our site for professional and enthusiast programmers.

    
possible duplicate of How to start a program using a hotkey in C#? –  Jürgen Thelen Aug 23 '12 at 19:33

2 Answers 2

You cannot set a hotkey using C# code... Your application need to run if you want to set anything, and you want to trigger it to run using a shortkey... Ofcourse this can be set in t he settings of your application... rightclick your application, settings en find the item that lets you specify a shortkey to start the application.

To the tray icon question. You just need to hide your form, i'm assuming have build a forms application for this.

You can do this with the following code:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace MyTrayApp
{
    public class SysTrayApp : Form
    {
        [STAThread]
        public static void Main()
        {
            Application.Run(new SysTrayApp());
        }

        private NotifyIcon  trayIcon;
        private ContextMenu trayMenu;

        public SysTrayApp()
        {
            // Create a simple tray menu with only one item.
            trayMenu = new ContextMenu();
            trayMenu.MenuItems.Add("Exit", OnExit);

            // Create a tray icon. In this example we use a
            // standard system icon for simplicity, but you
            // can of course use your own custom icon too.
            trayIcon      = new NotifyIcon();
            trayIcon.Text = "MyTrayApp";
            trayIcon.Icon = new Icon(SystemIcons.Application, 40, 40);

            // Add menu to tray icon and show it.
            trayIcon.ContextMenu = trayMenu;
            trayIcon.Visible     = true;
        }

        protected override void OnLoad(EventArgs e)
        {
            Visible       = false; // Hide form window.
            ShowInTaskbar = false; // Remove from taskbar.

            base.OnLoad(e);
        }

        private void OnExit(object sender, EventArgs e)
        {
            Application.Exit();
        }

        protected override void Dispose(bool isDisposing)
        {
            if (isDisposing)
            {
                // Release the icon resource.
                trayIcon.Dispose();
            }

            base.Dispose(isDisposing);
        }
    }
}

this code was taken from http://alanbondo.wordpress.com/2008/06/22/creating-a-system-tray-app-with-c/ which works like a charm!

share|improve this answer
    
haha - posted at the same time AND used the same example - great minds eh? –  Wolf5370 Aug 23 '12 at 12:53
    
Great minds think alike indeed ;) –  bkwint Aug 23 '12 at 12:55

You can use a product like Auto Hotkeys which is free and good for defining hotkeys (and auto spell checking etc).

For system Tray, the usual way to do this is to not have the project as a forms project, but as a console project that calls a form. This way the form is not used until it is called from the system tray context menu - Look into NotifyIcon - Simple example Here. The other (less tidy) way is to have the main from hiden at the start, but if the form is not needed, it is a waste of respources really.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.