// Pavel Zolnikov[http://www.codeproject.com/script/profile/whos_who.asp?id=35980], 2002
using System;
using Microsoft.Win32;
using System.Collections;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using SHDocVw;
using ZCommon;
using BandObjects;
namespace CommandBar
{
/// <summary>
/// Handles Ctrl+M key combination in explorer.
/// </summary>
/// <remarks>
/// 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).
/// Once attached, it sets WindowsHook to process all keyboard events. If Ctrl+M was pressed it shows or hides Command Prompt Bar.
/// </remarks>
[ComVisible(true)]
[Guid("3F1AB67E-12AA-352E-B4E0-A5F1810B60DD")]
public class CtrlMHook: WindowsHook, IObjectWithSite
{
/// <summary>
/// Reference to the hosting explorer.
/// </summary>
WebBrowserClass explorer;
Object site;
/// <summary>
/// BHO object must implement IObjectWithSite to get access to the host.
/// </summary>
/// <param name="pUnkSite"></param>
void IObjectWithSite.SetSite(Object newSite)
{
if( site != null )
Marshal.ReleaseComObject( site );
site = newSite;
if( site != null )
{
IServiceProviderCOM sp = site as IServiceProviderCOM;
Guid guid = ExplorerGUIDs.IID_IWebBrowserApp;
Guid riid = ExplorerGUIDs.IID_IUnknown;
object wba;
sp.QueryService(
ref guid,
ref riid,
out wba );
explorer = (WebBrowserClass)Marshal.CreateWrapperOfType(
wba as IWebBrowser,
typeof(WebBrowserClass));
//ready to process Ctrl+M key, starting
HookMe(2);//WH_KEYBOARD
}
else
{
if ( explorer != null )
{
Marshal.ReleaseComObject( explorer );
explorer = null;
}
//explorer is about to close, freeing resources
UnHookMe();
}
}
void IObjectWithSite.GetSite(ref Guid riid, out Object outSite)
{
outSite = site;
}
/// <summary>
/// Handles keyboard events of the process. If Ctrl+M is pressed toggles Visible state of the CommandBar.