Exemple #1
0
        static void Main()
        {
            //  Instantiate a log that maintains a running record of the recognised voice commands.  This log
            //  will be displayed within a ListBox in the main form, frmGAVPI.  We specify a callback method so
            //  that we may inform an already open frmGAVPI to update the ListBox with the log content.

            try {

                Log = new Logging< string >( GAVPI.OnLogMessage );

            } catch( Exception ) { throw; }

            Settings = new Settings();
            Profile = new Profile( null );

            vi = new InputEngine();

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            //
            //  To ensure that only a single instance of the application can be executed at a time, we'll use
            //  Mutex ownership to determine if we're already running.  I used http://www.guidgenerator.com/
            //  to create a Globally Unique ID (GUID) as the name of a Mutex, which the application will attempt
            //  to secure before running proper.  If the Mutex can't be secured it means our application is
            //  already running.
            //

            Mutex LockApplicationInstance;
            bool OnlyApplicationInstance = false;

            //
            //  We'll do something useful if the application is already running by sending it a message asking
            //  it to show itself.  This may be achieved through the native Win32 API PostMessage (an asynchronous
            //  call).  But regardless of whether another instance of the application is running or now, we should
            //  request a message that is unique to our application.
            //

            if( ( WM_OPEN_EXISTING_INSTANCE = Win32_APIs.RegisterWindowMessage( "WM_OPEN_EXISTING_INSTANCE" ) ) == 0 ) {

                throw new Win32Exception();

            }

            //  Now we can check to see if our application already running...

            if( ( ( LockApplicationInstance = new Mutex( true, APPLICATION_ID, out OnlyApplicationInstance ) ) != null ) &&
                !OnlyApplicationInstance ) {

                //
                //  It is already running.  Now assuming the user wants to do something productive, let's bring the
                //  existing instance to the front.  PostMessage is non-blocking native Win32 call, so will return
                //  immediately, whose intent is picked up in the frmGAVPI.WndProc() method of the existing instance.
                //

                Win32_APIs.PostMessage( (IntPtr)Win32_APIs.HWND_BROADCAST,
                                        WM_OPEN_EXISTING_INSTANCE,
                                        IntPtr.Zero,
                                        IntPtr.Zero );

                //  We can happily quit now.

                return;

            }  //  if()

            //
            //  A system tray icon and associated menu offers an alternative UI, providing the key functionality
            //  of the application in a convenient menu.
            //
            //  So, let's set the Tray Icon's tooltip name, use an instance of the application's icon (from the
            //  project's resources, and provide a handler for when a user double-clicks the system tray icon.

            sysTrayIcon = new NotifyIcon();

            sysTrayIcon.Icon  = Properties.Resources.gavpi;
            sysTrayIcon.Visible = true;
            sysTrayIcon.DoubleClick += new System.EventHandler( OnDoubleClickIcon );
            sysTrayIcon.Text = APPLICATION_TITLE;

            //  Our system tray icon's context menu consists of items that may be enabled or disabled depending
            //  on the available workflow.  By default, however, their initial states should be as declared.

            sysTrayMenu = new ContextMenu();

            //  Our MRU menu.  We'll add it to the top of the system tray icon's context menu.

            ProfileMRU = new MRU( "Recent", OnMRUListItem );

            sysTrayMenu.MenuItems.Add( ProfileMRU.GetMenu() );
            sysTrayMenu.MenuItems.Add( "Open Profile", LoadProfile );
            sysTrayMenu.MenuItems.Add( "Modify", OpenProfileEditor ).Enabled = false;
            sysTrayMenu.MenuItems.Add( "-" );
            sysTrayMenu.MenuItems.Add( "Show Log", OpenMainWindow );
            sysTrayMenu.MenuItems.Add( "-" );
            sysTrayMenu.MenuItems.Add( "Listen", StartListening ).Enabled = false;
            sysTrayMenu.MenuItems.Add( "Stop", StopListening ).Enabled = false;
            sysTrayMenu.MenuItems.Add( "-" );
            sysTrayMenu.MenuItems.Add( "Settings", OpenSettings);
            sysTrayMenu.MenuItems.Add( "-" );
            sysTrayMenu.MenuItems.Add( "Exit", Exit );

            //  And now we can attach the menu to the system tray icon.

            sysTrayIcon.ContextMenu = sysTrayMenu;

            //  Now let's load the MRU items before running the application propper.

            ProfileMRU.Deserialize();

            //  Let's commence monitoring process startup and automatically open Profiles if any have been
            //  associated with an executable and the option to do so has been set.

            if( Properties.Settings.Default.EnableAutoOpenProfile ) EnableAutoOpenProfile( null, null );

            //  And display the main window upon start if specified in the configuration.

            if( Properties.Settings.Default.ShowGAVPI ) OpenMainWindow( null, null );

            Application.Run();

            //  We don't want the garbage collector to think our Mutex is up for grabs before we close the program,
            //  so let's protect it.

            GC.KeepAlive( LockApplicationInstance );

            LockApplicationInstance.ReleaseMutex();
        }
Exemple #2
0
 //
 //  static public void OnMRUListItem( MRU.MRUItem )
 //
 //  Our MRU management class accepts OnMRUListItem as a callback function, informing the user of the
 //  MRU item that was selected.  An object of type MRU.MRUItem is passed as the only argument.  If the
 //  associated Profile cannot be opened it will be removed from the MRU list.
 //
 public static void OnMRUListItem( MRU.MRUItem item )
 {
     if( !LoadProfile( ( string ) item.ItemData ) ) ProfileMRU.Remove( item.ItemText );
 }