home *** CD-ROM | disk | FTP | other *** search
- // Pavel Zolnikov[http://www.codeproject.com/script/profile/whos_who.asp?id=35980], 2002
-
- using System;
- using System.Drawing;
- using Microsoft.Win32;
- using System.Reflection;
- using System.Diagnostics;
- using System.Windows.Forms;
- using System.ComponentModel;
- using System.Security.Permissions;
- using System.Runtime.InteropServices;
-
- using SHDocVw;
-
- namespace BandObjects
- {
- /// <summary>
- /// Implements generic Band Object functionality.
- /// </summary>
- /// <example>
- /// [Guid("YOURGUID-GOES-HERE-YOUR-GUIDGOESHERE")]
- /// [BandObject("Hello World Bar", BandObjectStyles.Horizontal | BandObjectStyles.ExplorerToolbar , HelpText = "Shows bar that says hello.")]
- /// public class HelloWorldBar : BandObject
- /// { /*...*/ }
- /// </example>
- public class BandObject : UserControl, IObjectWithSite, IDeskBand, IDockingWindow, IOleWindow, IInputObject
- {
- /// <summary>
- /// Reference to the host explorer.
- /// </summary>
- [CLSCompliant(false)]
- WebBrowserClass explorer;
- protected IInputObjectSite BandObjectSite;
- /// <summary>
- /// This event is fired after reference to hosting explorer is retreived and stored in Explorer property.
- /// </summary>
- public event EventHandler ExplorerAttached;
-
- public event EventHandler ExplorerDetaching;
-
- [CLSCompliant(false)]
- public WebBrowserClass Explorer
- {
- get
- {
- return explorer;
- }
- }
-
- /// <summary>
- /// Title of the band object. Displayed at the left or on top of the band object.
- /// </summary>
- [Browsable(true)]
- [DefaultValue("")]
- public String Title
- {
- get
- {
- return title;
- }
- set
- {
- title = value;
- }
- }String title;
-
- /// <summary>
- /// Minimum size of the band object. Default value of -1 sets no minimum constraint.
- /// </summary>
- [Browsable(true)]
- [DefaultValue(typeof(Size),"-1,-1")]
- public Size MinSize
- {
- get
- {
- return minSize;
- }
- set
- {
- minSize = value;
- }
- }Size minSize = new Size(-1,-1);
-
- /// <summary>
- /// Maximum size of the band object. Default value of -1 sets no maximum constraint.
- /// </summary>
- [Browsable(true)]
- [DefaultValue(typeof(Size),"-1,-1")]
- public Size MaxSize
- {
- get
- {
- return maxSize;
- }
- set
- {
- maxSize = value;
- }
- }Size maxSize = new Size(-1,-1);
-
- /// <summary>
- /// Says that band object's size must be multiple of this size. Defauilt value of -1 does not set this constraint.
- /// </summary>
- [Browsable(true)]
- [DefaultValue(typeof(Size),"1,1")]
- public Size IntegralSize
- {
- get
- {
- return integralSize;
- }
- set
- {
- integralSize = value;
- }
- }Size integralSize = new Size(1,1);
-
-
- public virtual void GetBandInfo(
- Int32 bandID,
- Int32 viewMode,
- ref DESKBANDINFO info)
- {
- info.wszTitle = this.Title;
-
- info.ptActual.X = this.Size.Width;
- info.ptActual.Y = this.Size.Height;
-
- info.ptMaxSize.X = this.MaxSize.Width;
- info.ptMaxSize.Y = this.MaxSize.Height;
-
- info.ptMinSize.X = this.MinSize.Width;
- info.ptMinSize.Y = this.MinSize.Height;
-
- info.ptIntegral.X = this.IntegralSize.Width;
- info.ptIntegral.Y = this.IntegralSize.Height;
-
- info.dwModeFlags = DBIMF.DBIMF_VARIABLEHEIGHT;
-
- info.dwMask &= ~DBIM.BKCOLOR;
- }
-
- /// <summary>
- /// Called by explorer when band object needs to be showed or hidden.
- /// </summary>
- /// <param name="fShow"></param>
- public virtual void ShowDW(bool show)
- {
- if( show )
- Show();
- else
- Hide();
- }
-
- /// <summary>
- /// Called by explorer when window is about to close.
- /// </summary>
- public virtual void CloseDW(Int32 reserved)
- {
- Dispose( true );
- }
-
- /// <summary>
- /// Not used.
- /// </summary>
- public virtual void ResizeBorderDW(IntPtr borderRectangle, Object toolbarSite, bool reserved){}
-
- public virtual void GetWindow(out System.IntPtr hwnd)
- {
- hwnd = Handle;
- }
-
- public virtual void ContextSensitiveHelp(bool enterMode){}
-
- public virtual void SetSite(Object site)
- {
- if( site != BandObjectSite )
- {
- if( explorer != null )
- OnExplorerDetaching(EventArgs.Empty);
-
- if( BandObjectSite != null )
- Marshal.ReleaseComObject( BandObjectSite );
-
- if( explorer != null )
- {
- Marshal.ReleaseComObject( explorer );
- explorer = null;
- }
- }
-
- BandObjectSite = (IInputObjectSite)site;
- if( BandObjectSite != null )
- {
- //pUnkSite is a pointer to object that implements IOleWindowSite or something similar
- //we need to get access to the top level object - explorer itself
- //to allows this explorer objects also implement IServiceProvider interface
- //(don't mix it with System.IServiceProvider!)
- //we get this interface and ask it to find WebBrowserApp
- IServiceProviderCOM sp = (IServiceProviderCOM)BandObjectSite;
- Guid guid = ExplorerGUIDs.IID_IWebBrowserApp;
- Guid riid = ExplorerGUIDs.IID_IUnknown;
-
- try
- {
- object w;
- sp.QueryService(
- ref guid,
- ref riid,
- out w );
-
- //once we have interface to the COM object we can create RCW from it
- explorer = (WebBrowserClass)Marshal.CreateWrapperOfType(
- w as IWebBrowser,
- typeof(WebBrowserClass)
- );
-
- OnExplorerAttached(EventArgs.Empty);
- }
- catch( COMException )
- {
- //we anticipate this exception in case our object instantiated
- //as a Desk Band. There is no web browser service available.
- }
- }
- }
-
- public virtual void GetSite(ref Guid riid, out Object site)
- {
- site = BandObjectSite;
- }
-
- /// <summary>
- /// Called explorer when focus has to be chenged.
- /// </summary>
- public virtual void UIActivateIO(Int32 activate, ref MSG msg)
- {
- if( activate != 0 )
- {
- Control ctrl = GetNextControl(this, Control.ModifierKeys != Keys.Shift);//first or last
- this.Focus();
- if( ctrl != null ) ctrl.Select();
- }
- }
-
- public virtual Int32 HasFocusIO()
- {
- return this.ContainsFocus ? 0 : 1; //S_OK : S_FALSE;
- }
-
- /// <summary>
- /// Called by explorer to process keyboard events. Undersatands Tab and F6.
- /// </summary>
- /// <param name="msg"></param>
- /// <returns>S_OK if message was processed, S_FALSE otherwise.</returns>
- public virtual Int32 TranslateAcceleratorIO(ref MSG msg)
- {
- if( msg.message == 0x100 )//WM_KEYDOWN
- if( msg.wParam == (uint)Keys.Tab || msg.wParam == (uint)Keys.F6 )//keys used by explorer to navigate from control to control
- {
- Control next = GetNextControl( ActiveControl, Control.ModifierKeys != Keys.Shift );
-
- if( SelectNextControl(
- ActiveControl,
- ModifierKeys == Keys.Shift ? false : true,
- true,
- true,
- false )
- )
- return 0;//S_OK
- }
-
- return 1;//S_FALSE
- }
-
- /// <summary>
- /// Override this method to handle ExplorerAttached event.
- /// </summary>
- /// <param name="ea"></param>
- protected virtual void OnExplorerAttached(EventArgs ea)
- {
- if ( ExplorerAttached != null )
- ExplorerAttached(this, ea);
- }
-
- protected virtual void OnExplorerDetaching(EventArgs ea)
- {
- if ( ExplorerDetaching != null )
- ExplorerDetaching(this, ea);
- }
-
- /// <summary>
- /// Notifies explorer of focus change.
- /// </summary>
- protected override void OnGotFocus(System.EventArgs e)
- {
- base.OnGotFocus(e);
- NotifyExplorerGotFocus();
- }
- /// <summary>
- /// Notifies explorer of focus change.
- /// </summary>
- protected override void OnLostFocus(System.EventArgs e)
- {
- base.OnLostFocus(e);
- if( HasFocusIO() != 0 )//S_OK
- NotifyExplorerLostFocus();
- }
-
- public void NotifyExplorerLostFocus()
- {
- BandObjectSite.OnFocusChangeIS(this as IInputObject, 0);
- }
-
- public void NotifyExplorerGotFocus()
- {
- BandObjectSite.OnFocusChangeIS(this as IInputObject, 1);
- }
-
-
- /// <summary>
- /// Called when derived class is registered as a COM server.
- /// </summary>
- [ComRegisterFunctionAttribute]
- public static void Register(Type t)
- {
- string guid = t.GUID.ToString("B");
-
- RegistryKey rkClass = Registry.ClassesRoot.CreateSubKey(@"CLSID\"+guid );
- RegistryKey rkCat = rkClass.CreateSubKey("Implemented Categories");
-
- BandObjectAttribute[] boa = (BandObjectAttribute[])t.GetCustomAttributes(
- typeof(BandObjectAttribute),
- false );
-
- string name = t.Name;
- string help = t.Name;
- BandObjectStyles style = 0;
- if( boa.Length == 1 )
- {
- if( boa[0].Name != null )
- name = boa[0].Name;
-
- if( boa[0].HelpText != null )
- help = boa[0].HelpText;
-
- style = boa[0].Style;
- }
-
- rkClass.SetValue(null, name );
- rkClass.SetValue("MenuText", name );
- rkClass.SetValue("HelpText", help );
-
- if( 0 != (style & BandObjectStyles.Vertical) )
- rkCat.CreateSubKey("{00021493-0000-0000-C000-000000000046}");
-
- if( 0 != (style & BandObjectStyles.Horizontal) )
- rkCat.CreateSubKey("{00021494-0000-0000-C000-000000000046}");
-
- if( 0 != (style & BandObjectStyles.TaskbarToolBar) )
- rkCat.CreateSubKey("{00021492-0000-0000-C000-000000000046}");
-
- if( 0 != (style & BandObjectStyles.ExplorerToolbar) )
- Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Internet Explorer\Toolbar").SetValue(guid,name);
-
- }
-
- /// <summary>
- /// Called when derived class is unregistered as a COM server.
- /// </summary>
- [ComUnregisterFunctionAttribute]
- public static void Unregister(Type t)
- {
- string guid = t.GUID.ToString("B");
- BandObjectAttribute[] boa = (BandObjectAttribute[])t.GetCustomAttributes(
- typeof(BandObjectAttribute),
- false );
-
- BandObjectStyles style = 0;
- if( boa.Length == 1 ) style = boa[0].Style;
-
- if( 0 != (style & BandObjectStyles.ExplorerToolbar) )
- Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Internet Explorer\Toolbar").DeleteValue(guid,false);
-
- Registry.ClassesRoot.CreateSubKey(@"CLSID").DeleteSubKeyTree(guid);
- }
- }
- }