home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2005 November / PCWELT_11_2005.ISO / pcwsoft / Commandbar-Source.z.exe / CommandBar / CtrlMHook.cs < prev    next >
Encoding:
Text File  |  2002-06-10  |  3.7 KB  |  130 lines

  1. // Pavel Zolnikov[http://www.codeproject.com/script/profile/whos_who.asp?id=35980], 2002
  2.  
  3. using System;
  4. using Microsoft.Win32;
  5. using System.Collections;
  6. using System.Windows.Forms;
  7. using System.Runtime.InteropServices;
  8.  
  9. using SHDocVw;
  10.  
  11. using ZCommon;
  12. using BandObjects;
  13.  
  14. namespace CommandBar
  15. {
  16.     /// <summary>
  17.     /// Handles Ctrl+M key combination in explorer.
  18.     /// </summary>
  19.     /// <remarks>
  20.     /// It is implementd as a Browser Helper Object so it gets loaded into explorer.exe process and is attached to every instance of windows explorer(window).
  21.     /// Once attached, it sets WindowsHook to process all keyboard events. If Ctrl+M was pressed it shows or hides Command Prompt Bar.
  22.     /// </remarks>
  23.     [ComVisible(true)]
  24.     [Guid("3F1AB67E-12AA-352E-B4E0-A5F1810B60DD")]
  25.     public class CtrlMHook: WindowsHook, IObjectWithSite
  26.     {
  27.         /// <summary>
  28.         /// Reference to the hosting explorer.
  29.         /// </summary>
  30.         WebBrowserClass explorer;
  31.  
  32.         Object site;
  33.  
  34.         /// <summary>
  35.         /// BHO object must implement IObjectWithSite to get access to the host.
  36.         /// </summary>
  37.         /// <param name="pUnkSite"></param>
  38.         void IObjectWithSite.SetSite(Object newSite)
  39.         {
  40.             if( site != null )
  41.                 Marshal.ReleaseComObject( site );
  42.  
  43.             site = newSite;
  44.             if( site != null )
  45.             {
  46.                 IServiceProviderCOM sp = site as IServiceProviderCOM;
  47.                 Guid guid = ExplorerGUIDs.IID_IWebBrowserApp;
  48.                 Guid riid = ExplorerGUIDs.IID_IUnknown;
  49.  
  50.                 object wba;
  51.                 sp.QueryService( 
  52.                     ref guid,
  53.                     ref riid,
  54.                     out wba );
  55.                 
  56.                 explorer = (WebBrowserClass)Marshal.CreateWrapperOfType(
  57.                     wba as IWebBrowser,
  58.                     typeof(WebBrowserClass));
  59.             
  60.                 //ready to process Ctrl+M key, starting
  61.                 HookMe(2);//WH_KEYBOARD
  62.             }
  63.             else
  64.             {
  65.                 if ( explorer != null )
  66.                 {
  67.                     Marshal.ReleaseComObject( explorer );
  68.                     explorer = null;
  69.                 }
  70.                 //explorer is about to close, freeing resources
  71.                 UnHookMe();
  72.             }
  73.         }
  74.  
  75.         void IObjectWithSite.GetSite(ref Guid riid, out Object outSite)
  76.         {
  77.             outSite = site;            
  78.         }
  79.  
  80.         /// <summary>
  81.         /// Handles keyboard events of the process. If Ctrl+M is pressed toggles Visible state of the CommandBar.
  82.         /// </summary>
  83.         /// <param name="ncode"></param>
  84.         /// <param name="wParam">key code</param>
  85.         /// <param name="lParam">not used</param>
  86.         /// <returns></returns>
  87.         protected override Int32 HookCallBack(Int32 ncode, Int32 wParam, Int32 lParam)
  88.         {
  89.             if( wParam == (Int32)Keys.M )//its about M key
  90.                 if( Control.ModifierKeys == Keys.Control )// and Ctrl is currently pressed
  91.                     if( (lParam & 0xc0000000) == 0 )//key went from 'up' to 'down' state(ensures that we handle CtrlM only once)
  92.                     {    
  93.                         bool show = true;
  94.                         CommandBarObj bar = CommandBarObj.GetBarFromExplorer(explorer);
  95.                         if( bar != null )
  96.                             show = !bar.Visible;
  97.  
  98.                         CommandBarObj.ShowBrowserBar( show, explorer );
  99.                         return 1;//we processed it
  100.                     }
  101.             
  102.             return base.HookCallBack( ncode, wParam, lParam);
  103.         }
  104.  
  105.         /// <summary>
  106.         /// Called when derived class is registered as a COM server.
  107.         /// </summary>
  108.         [ComRegisterFunctionAttribute]
  109.         public static void Register(Type t)
  110.         {
  111.             string guid = t.GUID.ToString("B");
  112.             
  113.             Registry.LocalMachine.CreateSubKey(
  114.                 @"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Browser Helper Objects\" + guid );                
  115.         }
  116.    
  117.         /// <summary>
  118.         /// Called when derived class is unregistered as a COM server.
  119.         /// </summary>
  120.         [ComUnregisterFunctionAttribute]
  121.         public static void Unregister(Type t)
  122.         {
  123.             string guid = t.GUID.ToString("B");
  124.  
  125.             Registry.LocalMachine.CreateSubKey(
  126.                 @"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Browser Helper Objects")
  127.                     .DeleteSubKey(guid,false);
  128.         }
  129.     }
  130. }