private void ChangeActiveXStateUpTo(ActiveXHelper.ActiveXState state)
        {
            if (!this.GetActiveXHostState(ActiveXHelper.InTransition))
            {
                this.SetActiveXHostState(ActiveXHelper.InTransition, true);
                try
                {
                    while (state > this.activeXState)
                    {
                        switch (this.activeXState)
                        {
                        case ActiveXHelper.ActiveXState.Passive:
                            this.activeXInstance = UnsafeNativeMethods.CoCreateInstance <TActiveXClass, TActiveXInterface>(null, NativeMethods.tagCLSCTX.CLSCTX_INPROC_SERVER, NativeMethods.ActiveX.IID_IUnknown);
                            this.activeXState    = ActiveXHelper.ActiveXState.Loaded;
                            this.AttachInterfacesInternal();
                            continue;

                        case ActiveXHelper.ActiveXState.Loaded:
                            try
                            {
                                if ((this.activeXOleObject.GetMiscStatus(NativeMethods.tagDVASPECT.DVASPECT_CONTENT) & NativeMethods.tagOLEMISC.OLEMISC_SETCLIENTSITEFIRST) != 0)
                                {
                                    this.activeXOleObject.SetClientSite(this.ActiveXSite);
                                }
                            }
                            catch { }
                            if (!base.DesignMode)
                            {
                                this.StartEvents();
                            }
                            this.activeXState = ActiveXHelper.ActiveXState.Running;
                            continue;

                        case ActiveXHelper.ActiveXState.Running:
                            try
                            {
                                this.DoVerb(NativeMethods.OLEIVERB.OLEIVERB_INPLACEACTIVATE);
                            }
                            catch (Exception ex)
                            {
                                throw new TargetInvocationException(ResourcesHelper.GetString(ResourcesHelper.ActiveXNoWindowHandle, new object[] { base.GetType().Name }), ex);
                            }
                            ControlShim.CreateControl(this, true);
                            this.activeXState = ActiveXHelper.ActiveXState.InPlaceActive;
                            continue;

                        case ActiveXHelper.ActiveXState.InPlaceActive:
                            try
                            {
                                this.DoVerb(NativeMethods.OLEIVERB.OLEIVERB_UIACTIVATE);
                            }
                            catch (Exception ex)
                            {
                                throw new TargetInvocationException(ResourcesHelper.GetString(ResourcesHelper.ActiveXNoWindowHandle, new object[] { base.GetType().Name }), ex);
                            }
                            this.activeXState = ActiveXHelper.ActiveXState.UIActive;
                            continue;
                        }
                        //this.activeXState += ActiveXHelper.ActiveXState.Loaded;
                        this.activeXState = (ActiveXHelper.ActiveXState)((int)(this.activeXState) + (int)(ActiveXHelper.ActiveXState.Loaded));
                    }
                }
                finally
                {
                    this.SetActiveXHostState(ActiveXHelper.InTransition, false);
                }
            }
        }
        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
            case NativeMethods.WM_LBUTTONDOWN:
            case NativeMethods.WM_RBUTTONDOWN:
            case NativeMethods.WM_MBUTTONDOWN:
            case NativeMethods.WM_MOUSEACTIVATE:
                if (!base.DesignMode)
                {
                    this.Focus();
                }
                this.DefWndProc(ref m);
                return;

            case NativeMethods.WM_LBUTTONUP:
            case NativeMethods.WM_LBUTTONDBLCLK:
            case NativeMethods.WM_RBUTTONUP:
            case NativeMethods.WM_RBUTTONDBLCLK:
            case NativeMethods.WM_MBUTTONUP:
            case NativeMethods.WM_MBUTTONDBLCLK:
            case NativeMethods.x2055:
            case NativeMethods.WM_CONTEXTMENU:
            case NativeMethods.WM_ERASEBKGND:
            case NativeMethods.WM_SYSCOLORCHANGE:
            case NativeMethods.WM_SETCURSOR:
            case NativeMethods.WM_DRAWITEM:
                this.DefWndProc(ref m);
                return;

            case NativeMethods.WM_COMMAND:
                if (!ControlShim.ReflectMessageInternal(m.LParam, ref m))
                {
                    this.DefWndProc(ref m);
                }
                return;

            case NativeMethods.WM_HELP:
                base.WndProc(ref m);
                this.DefWndProc(ref m);
                return;

            case NativeMethods.WM_DESTROY:
                break;

            case NativeMethods.WM_KILLFOCUS:
                this.hwndFocus = m.WParam;
                try
                {
                    base.WndProc(ref m);
                    return;
                }
                finally
                {
                    this.hwndFocus = IntPtr.Zero;
                }

            default:
                if (m.Msg == ActiveXHelper.REGMSG_MSG)
                {
                    m.Result = (IntPtr)0x7b;
                }
                else
                {
                    base.WndProc(ref m);
                }
                return;
            }

            if ((this.activeXState >= ActiveXHelper.ActiveXState.InPlaceActive))
            {
                IntPtr window = this.activeXOleInPlaceObject.GetWindow();
                if (window != IntPtr.Zero)
                {
                    ApplicationShim.ParkHandle(new HandleRef(this.activeXOleInPlaceObject, window));
                }
            }
            if (base.RecreatingHandle)
            {
                this.activeXReloadingState = this.activeXState;
            }
            this.ChangeActiveXStateDownTo(ActiveXHelper.ActiveXState.Running);
            if (this.activeXWindow != null)
            {
                this.activeXWindow.ReleaseHandle();
            }
            this.OnHandleDestroyed(EventArgs.Empty);
        }