MSHTML events problem 
Author Message
 MSHTML events problem

I've wasted too much time on this, so I'm posting the code here
in the hope that someone might have an idea or two.

Last year Lutz Roeder posted some code that host
mshtml (rather than shdocvw) as an Active Document. The
code works, but I want to extend it to handle mshtml
events. I added a ref to mshtml and implemented it as
shown below, which I believe is correct. This also works,
but has an unwanted side-effect. When I hook up the event
handler, the mshtml doc. no longer accepts keyboard input.
The main result you will see is that  you cannot select text
in the control. Unhook the event handler and it works
again. I've posted the code below. Ideas anyone?

Tim

using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace Reflector.HtmlBrowser
{
 /// <summary>
 /// Implements MSHTML as active document in a panel
 /// Posted to microsoft.dotnet.framework.general by
 /// Lutz Roeder. Slightly amended to handle events.
 /// </summary>

 public delegate void HtmlNavigateEventHandler(Object s,
 HtmlNavigateEventArgs e);

 public class HtmlNavigateEventArgs
 {
  String target;

  public HtmlNavigateEventArgs(String target)
  {
   this.target = target;
  }

  public String Target
  {
   get { return target; }
  }
 }

 public class HtmlNavigator : Panel
 {
  public event HtmlNavigateEventHandler Navigate;
  HtmlSite site;
  String url = String.Empty;
  String document = "<html><body><h1>Test</h1></body></html>";

  //OnClick event handler.
  bool OnClick(mshtml.IHTMLEventObj evobj)
  {
   // you can put a breakpoint here and verify it gets called
   return false;
  }

  void SetDocEvents()
  {
   object m_htmldoc = site.Document;

   if (m_htmldoc != null)
   {
    //get the documents2 events interface
    mshtml.HTMLDocumentEvents2_Event docevents2 = (mshtml.HTMLDocumentEvents2_Event)m_htmldoc;

    //set the handler
    docevents2.onclick += new mshtml.HTMLDocumentEvents2_onclickEventHandler(OnClick);
   }

  }

  Boolean IsCreated
  {
   get { return (site != null) && (site.Document != null); }
  }

  protected override void OnHandleCreated(EventArgs e)
  {
   Console.WriteLine("--");

   base.OnHandleCreated(e);

   // force creating Host handle since we need it to parent MSHTML
   IntPtr hostHandle = Handle;

   site = new HtmlSite(this);
   site.CreateDocument();
   site.ActivateDocument();

   // demo the bug by commenting out this line
   SetDocEvents();

   if (url != String.Empty) LoadUrl(url);
   if (document != String.Empty) LoadDocument(document);
  }

  protected override void OnHandleDestroyed(EventArgs e)
  {
   if (IsCreated)
   {
    site.CloseDocument();
    site = null;
   }

   base.OnHandleDestroyed(e);
  }

  public void LoadUrl(String url)
  {
   this.url = url;
   if (!IsCreated) return;
   IPersistMoniker persistMoniker = (IPersistMoniker) site.Document;
   IMoniker moniker = null;
   CreateURLMoniker(null, url, out moniker);
   IBindCtx bindContext = null;
   CreateBindCtx(0, out bindContext);
   persistMoniker.Load(1, moniker, bindContext, 0);
   persistMoniker = null;
   bindContext = null;
   moniker = null;
  }

  public void LoadDocument(String document)
  {
   this.document = document;
   if ((document == null) || (document == String.Empty)) return;
   if (!IsCreated) return;

   IStream stream = null;
   CreateStreamOnHGlobal(Marshal.StringToHGlobalUni(document), true, out
    stream);
   if (stream == null) return;

   IPersistStreamInit persistentStreamInit = (IPersistStreamInit)
    site.Document;
   if (persistentStreamInit != null)
   {
    persistentStreamInit.InitNew();
    persistentStreamInit.Load(stream);
    persistentStreamInit = null;
   }

   stream = null;
  }

  public override Boolean PreProcessMessage(ref Message message)
  {
   if ((message.Msg >= WM.KEYFIRST) && (message.Msg <= WM.KEYLAST))
    if (((message.Msg == WM.KEYDOWN) || (message.Msg == WM.KEYUP)) &&
     (((int)message.WParam == (int)'G') || ((int)message.WParam == (int)'H')))
    {
     // Prevent Ctrl+G and Ctrl+H from being passed on, because they bring up
     //native trident dialogs
            Keys modifiers = ModifierKeys;
     if ((modifiers & Keys.Control) == 0)
     {
      MSG msg = new MSG();
      msg.hwnd = message.HWnd;
      msg.message = message.Msg;
      msg.wParam = message.WParam;
      msg.lParam = message.LParam;
      msg.time = 0;
      msg.pt_x = 0;
      msg.pt_y = 0;
      return site.TranslateAccelerator(msg);
     }
    }

   return base.PreProcessMessage(ref message);
  }

  public void InvokeNavigate(String target)
  {
   if (Navigate != null) Navigate(this, new HtmlNavigateEventArgs(target));
  }

  [ClassInterface(ClassInterfaceType.None)]
   class HtmlSite : IOleClientSite, IOleContainer, IDocHostUIHandler,
   IOleInPlaceFrame, IOleInPlaceSite, IOleDocumentSite
  {
   HtmlNavigator container;
   IOleObject document;
   IOleDocumentView view;
   IOleInPlaceActiveObject activeObject;

   public HtmlSite(HtmlNavigator container)
   {
    if ((container == null) || (container.IsHandleCreated == false)) throw
                      new ArgumentException();
    this.container = container;
    container.Resize += new EventHandler(this.Container_Resize);
   }

   public Object Document
   {
    get { return document; }
   }

   public void CreateDocument()
   {
    Debug.Assert(document == null, "Must call Close before recreating.");

    Boolean created = false;
    try
    {
     document = (IOleObject) new HTMLDocument();
     document.SetClientSite(this);
     created = true;
    }
    finally
    {
     if (created == false)
      document = null;
    }
   }

   public void ActivateDocument()
   {
    RECT rect = new RECT();
    GetClientRect(container.Handle, rect);
    document.DoVerb(OLEIVERB.UIACTIVATE, IntPtr.Zero, this, 0,
     container.Handle, rect);
   }

   public void CloseDocument()
   {
    const int OLECLOSE_NOSAVE = 1;
    container.Resize -= new EventHandler(this.Container_Resize);
    if (document == null) return;
    document.Close(OLECLOSE_NOSAVE);
    document.SetClientSite(null);
    view = null;
    document = null;
    activeObject = null;
   }

   void Container_Resize(Object src, EventArgs e)
   {
    if (view == null) return;
    RECT rect = new RECT();
    GetClientRect(container.Handle, rect);
    view.SetRect(rect);
   }

   public Boolean TranslateAccelerator(MSG msg)
   {
    if (activeObject != null)
     if (activeObject.TranslateAccelerator(msg) != HRESULT.S_FALSE)
      return true;

    return false;
   }

   // IOleClientSite

   public int SaveObject()
   {
    return HRESULT.S_OK;
   }

   public int GetMoniker(int dwAssign, int dwWhichMoniker, out Object ppmk)
   {
    ppmk = null;
    return HRESULT.E_NOTIMPL;
   }

   public int GetContainer(out IOleContainer ppContainer)
   {
    ppContainer = (IOleContainer) this;
    return HRESULT.S_OK;
   }

   public int ShowObject()
   {
    return HRESULT.S_OK;
   }

   public int OnShowWindow(int fShow)
   {
    return HRESULT.S_OK;
   }

   public int RequestNewObjectLayout()
   {
    return HRESULT.S_OK;
   }

   // IOleContainer

   public void ParseDisplayName(Object pbc, String pszDisplayName, int[]
    pchEaten, Object[] ppmkOut)
   {
    throw new COMException(String.Empty, HRESULT.E_NOTIMPL);
   }

   public void EnumObjects(int grfFlags, Object[] ppenum)
   {
    throw new COMException(String.Empty, HRESULT.E_NOTIMPL);
   }

   public void LockContainer(int fLock)
   {
   }

   // IOleDocumentSite Implementation

   public int ActivateMe(IOleDocumentView pViewToActivate)
   {
    if (pViewToActivate == null) return HRESULT.E_INVALIDARG;
    RECT rect = new RECT();
    GetClientRect(container.Handle, rect);
    view = pViewToActivate;
    view.SetInPlaceSite((IOleInPlaceSite)this);
    view.UIActivate(1);
    view.SetRect(rect);
    view.Show(1);
    return HRESULT.S_OK;
   }

   // IOleInPlaceSite Implementation

   public IntPtr GetWindow()
   {
    return container.Handle;
   }

   public void ContextSensitiveHelp(int fEnterMode)
   {
    throw new COMException(String.Empty, HRESULT.E_NOTIMPL);
   }

   public int CanInPlaceActivate()
   {
    return HRESULT.S_OK;
   }

   public void OnInPlaceActivate()
   {
   }

   public void OnUIActivate()
   {
   }

   public void GetWindowContext(out IOleInPlaceFrame ppFrame, out Object
    ppDoc, RECT lprcPosRect, RECT lprcClipRect, tagOIFI lpFrameInfo)
   {
    ppDoc = null;
    ppFrame = (IOleInPlaceFrame) this;
    GetClientRect(container.Handle, lprcPosRect);
    GetClientRect(container.Handle, lprcClipRect);
    lpFrameInfo.cb = Marshal.SizeOf(typeof(tagOIFI));
    lpFrameInfo.fMDIApp = 0;
    lpFrameInfo.hwndFrame = container.Handle;
    lpFrameInfo.hAccel = IntPtr.Zero;
    lpFrameInfo.cAccelEntries = 0;
   }

   public int Scroll(Object scrollExtant)
   {
    return HRESULT.E_NOTIMPL;
   }

   public void OnUIDeactivate(int fUndoable)
   {
   }

   public void OnInPlaceDeactivate()
   {
   }

   public void DiscardUndoState()
   {
    throw new COMException(String.Empty, HRESULT.E_NOTIMPL);
   }

   public void DeactivateAndUndo()
   {
   }

   public int OnPosRectChange(RECT lprcPosRect)
   {
    return HRESULT.S_OK;
   }

   // IOleInPlaceFrame Implementation

   public void GetBorder(RECT lprectBorder)
   {
    throw new COMException(String.Empty, HRESULT.E_NOTIMPL);
   }

   public void RequestBorderSpace(RECT pborderwidths)
   {
    throw new COMException(String.Empty, HRESULT.E_NOTIMPL);
   }

   public void SetBorderSpace(RECT pborderwidths)
   {
    throw new COMException(String.Empty, HRESULT.E_NOTIMPL);
   }

   public void SetActiveObject(IOleInPlaceActiveObject pActiveObject, String
    pszObjName)
   {
    this.activeObject = pActiveObject;
   }

   public void InsertMenus(IntPtr hmenuShared, tagOleMenuGroupWidths
    lpMenuWidths)
   {
    throw new COMException(String.Empty, HRESULT.E_NOTIMPL);
   }

   public void
...

read more »



Fri, 25 Feb 2005 14:06:18 GMT  
 MSHTML events problem
This is a know bug.

/morten

Quote:
> I've wasted too much time on this, so I'm posting the code here
> in the hope that someone might have an idea or two.

> Last year Lutz Roeder posted some code that host
> mshtml (rather than shdocvw) as an Active Document. The
> code works, but I want to extend it to handle mshtml
> events. I added a ref to mshtml and implemented it as
> shown below, which I believe is correct. This also works,
> but has an unwanted side-effect. When I hook up the event
> handler, the mshtml doc. no longer accepts keyboard input.
> The main result you will see is that  you cannot select text
> in the control. Unhook the event handler and it works
> again. I've posted the code below. Ideas anyone?

> Tim

> using System;
> using System.Diagnostics;
> using System.IO;
> using System.Runtime.InteropServices;
> using System.Text;
> using System.Windows.Forms;

> namespace Reflector.HtmlBrowser
> {
>  /// <summary>
>  /// Implements MSHTML as active document in a panel
>  /// Posted to microsoft.dotnet.framework.general by
>  /// Lutz Roeder. Slightly amended to handle events.
>  /// </summary>

>  public delegate void HtmlNavigateEventHandler(Object s,
>  HtmlNavigateEventArgs e);

>  public class HtmlNavigateEventArgs
>  {
>   String target;

>   public HtmlNavigateEventArgs(String target)
>   {
>    this.target = target;
>   }

>   public String Target
>   {
>    get { return target; }
>   }
>  }

>  public class HtmlNavigator : Panel
>  {
>   public event HtmlNavigateEventHandler Navigate;
>   HtmlSite site;
>   String url = String.Empty;
>   String document = "<html><body><h1>Test</h1></body></html>";

>   //OnClick event handler.
>   bool OnClick(mshtml.IHTMLEventObj evobj)
>   {
>    // you can put a breakpoint here and verify it gets called
>    return false;
>   }

>   void SetDocEvents()
>   {
>    object m_htmldoc = site.Document;

>    if (m_htmldoc != null)
>    {
>     //get the documents2 events interface
>     mshtml.HTMLDocumentEvents2_Event docevents2 =

(mshtml.HTMLDocumentEvents2_Event)m_htmldoc;
Quote:

>     //set the handler
>     docevents2.onclick += new

mshtml.HTMLDocumentEvents2_onclickEventHandler(OnClick);

- Show quoted text -

Quote:
>    }

>   }

>   Boolean IsCreated
>   {
>    get { return (site != null) && (site.Document != null); }
>   }

>   protected override void OnHandleCreated(EventArgs e)
>   {
>    Console.WriteLine("--");

>    base.OnHandleCreated(e);

>    // force creating Host handle since we need it to parent MSHTML
>    IntPtr hostHandle = Handle;

>    site = new HtmlSite(this);
>    site.CreateDocument();
>    site.ActivateDocument();

>    // demo the bug by commenting out this line
>    SetDocEvents();

>    if (url != String.Empty) LoadUrl(url);
>    if (document != String.Empty) LoadDocument(document);
>   }

>   protected override void OnHandleDestroyed(EventArgs e)
>   {
>    if (IsCreated)
>    {
>     site.CloseDocument();
>     site = null;
>    }

>    base.OnHandleDestroyed(e);
>   }

>   public void LoadUrl(String url)
>   {
>    this.url = url;
>    if (!IsCreated) return;
>    IPersistMoniker persistMoniker = (IPersistMoniker) site.Document;
>    IMoniker moniker = null;
>    CreateURLMoniker(null, url, out moniker);
>    IBindCtx bindContext = null;
>    CreateBindCtx(0, out bindContext);
>    persistMoniker.Load(1, moniker, bindContext, 0);
>    persistMoniker = null;
>    bindContext = null;
>    moniker = null;
>   }

>   public void LoadDocument(String document)
>   {
>    this.document = document;
>    if ((document == null) || (document == String.Empty)) return;
>    if (!IsCreated) return;

>    IStream stream = null;
>    CreateStreamOnHGlobal(Marshal.StringToHGlobalUni(document), true, out
>     stream);
>    if (stream == null) return;

>    IPersistStreamInit persistentStreamInit = (IPersistStreamInit)
>     site.Document;
>    if (persistentStreamInit != null)
>    {
>     persistentStreamInit.InitNew();
>     persistentStreamInit.Load(stream);
>     persistentStreamInit = null;
>    }

>    stream = null;
>   }

>   public override Boolean PreProcessMessage(ref Message message)
>   {
>    if ((message.Msg >= WM.KEYFIRST) && (message.Msg <= WM.KEYLAST))
>     if (((message.Msg == WM.KEYDOWN) || (message.Msg == WM.KEYUP)) &&
>      (((int)message.WParam == (int)'G') || ((int)message.WParam ==
(int)'H')))
>     {
>      // Prevent Ctrl+G and Ctrl+H from being passed on, because they bring
up
>      //native trident dialogs
>             Keys modifiers = ModifierKeys;
>      if ((modifiers & Keys.Control) == 0)
>      {
>       MSG msg = new MSG();
>       msg.hwnd = message.HWnd;
>       msg.message = message.Msg;
>       msg.wParam = message.WParam;
>       msg.lParam = message.LParam;
>       msg.time = 0;
>       msg.pt_x = 0;
>       msg.pt_y = 0;
>       return site.TranslateAccelerator(msg);
>      }
>     }

>    return base.PreProcessMessage(ref message);
>   }

>   public void InvokeNavigate(String target)
>   {
>    if (Navigate != null) Navigate(this, new

HtmlNavigateEventArgs(target));

- Show quoted text -

Quote:
>   }

>   [ClassInterface(ClassInterfaceType.None)]
>    class HtmlSite : IOleClientSite, IOleContainer, IDocHostUIHandler,
>    IOleInPlaceFrame, IOleInPlaceSite, IOleDocumentSite
>   {
>    HtmlNavigator container;
>    IOleObject document;
>    IOleDocumentView view;
>    IOleInPlaceActiveObject activeObject;

>    public HtmlSite(HtmlNavigator container)
>    {
>     if ((container == null) || (container.IsHandleCreated == false)) throw
>                       new ArgumentException();
>     this.container = container;
>     container.Resize += new EventHandler(this.Container_Resize);
>    }

>    public Object Document
>    {
>     get { return document; }
>    }

>    public void CreateDocument()
>    {
>     Debug.Assert(document == null, "Must call Close before recreating.");

>     Boolean created = false;
>     try
>     {
>      document = (IOleObject) new HTMLDocument();
>      document.SetClientSite(this);
>      created = true;
>     }
>     finally
>     {
>      if (created == false)
>       document = null;
>     }
>    }

>    public void ActivateDocument()
>    {
>     RECT rect = new RECT();
>     GetClientRect(container.Handle, rect);
>     document.DoVerb(OLEIVERB.UIACTIVATE, IntPtr.Zero, this, 0,
>      container.Handle, rect);
>    }

>    public void CloseDocument()
>    {
>     const int OLECLOSE_NOSAVE = 1;
>     container.Resize -= new EventHandler(this.Container_Resize);
>     if (document == null) return;
>     document.Close(OLECLOSE_NOSAVE);
>     document.SetClientSite(null);
>     view = null;
>     document = null;
>     activeObject = null;
>    }

>    void Container_Resize(Object src, EventArgs e)
>    {
>     if (view == null) return;
>     RECT rect = new RECT();
>     GetClientRect(container.Handle, rect);
>     view.SetRect(rect);
>    }

>    public Boolean TranslateAccelerator(MSG msg)
>    {
>     if (activeObject != null)
>      if (activeObject.TranslateAccelerator(msg) != HRESULT.S_FALSE)
>       return true;

>     return false;
>    }

>    // IOleClientSite

>    public int SaveObject()
>    {
>     return HRESULT.S_OK;
>    }

>    public int GetMoniker(int dwAssign, int dwWhichMoniker, out Object
ppmk)
>    {
>     ppmk = null;
>     return HRESULT.E_NOTIMPL;
>    }

>    public int GetContainer(out IOleContainer ppContainer)
>    {
>     ppContainer = (IOleContainer) this;
>     return HRESULT.S_OK;
>    }

>    public int ShowObject()
>    {
>     return HRESULT.S_OK;
>    }

>    public int OnShowWindow(int fShow)
>    {
>     return HRESULT.S_OK;
>    }

>    public int RequestNewObjectLayout()
>    {
>     return HRESULT.S_OK;
>    }

>    // IOleContainer

>    public void ParseDisplayName(Object pbc, String pszDisplayName, int[]
>     pchEaten, Object[] ppmkOut)
>    {
>     throw new COMException(String.Empty, HRESULT.E_NOTIMPL);
>    }

>    public void EnumObjects(int grfFlags, Object[] ppenum)
>    {
>     throw new COMException(String.Empty, HRESULT.E_NOTIMPL);
>    }

>    public void LockContainer(int fLock)
>    {
>    }

>    // IOleDocumentSite Implementation

>    public int ActivateMe(IOleDocumentView pViewToActivate)
>    {
>     if (pViewToActivate == null) return HRESULT.E_INVALIDARG;
>     RECT rect = new RECT();
>     GetClientRect(container.Handle, rect);
>     view = pViewToActivate;
>     view.SetInPlaceSite((IOleInPlaceSite)this);
>     view.UIActivate(1);
>     view.SetRect(rect);
>     view.Show(1);
>     return HRESULT.S_OK;
>    }

>    // IOleInPlaceSite Implementation

>    public IntPtr GetWindow()
>    {
>     return container.Handle;
>    }

>    public void ContextSensitiveHelp(int fEnterMode)
>    {
>     throw new COMException(String.Empty, HRESULT.E_NOTIMPL);
>    }

>    public int CanInPlaceActivate()
>    {
>     return HRESULT.S_OK;
>    }

>    public void OnInPlaceActivate()
>    {
>    }

>    public void OnUIActivate()
>    {
>    }

>    public void GetWindowContext(out IOleInPlaceFrame ppFrame, out Object
>     ppDoc, RECT lprcPosRect, RECT lprcClipRect, tagOIFI lpFrameInfo)
>    {
>     ppDoc = null;
>     ppFrame = (IOleInPlaceFrame) this;
>     GetClientRect(container.Handle, lprcPosRect);
>     GetClientRect(container.Handle, lprcClipRect);
>     lpFrameInfo.cb = Marshal.SizeOf(typeof(tagOIFI));
>     lpFrameInfo.fMDIApp = 0;
>     lpFrameInfo.hwndFrame = container.Handle;
>     lpFrameInfo.hAccel = IntPtr.Zero;
>     lpFrameInfo.cAccelEntries = 0;
>    }

>    public int Scroll(Object scrollExtant)
>    {
>     return HRESULT.E_NOTIMPL;
>    }

>    public void OnUIDeactivate(int fUndoable)
>    {
>    }

>    public void OnInPlaceDeactivate()
>    {
>    }

>    public void DiscardUndoState()
>    {
>     throw new COMException(String.Empty, HRESULT.E_NOTIMPL);
>    }

>    public void

...

read more »



Sat, 26 Feb 2005 05:41:25 GMT  
 MSHTML events problem

Quote:

> This is a know bug.

> /morten

Hi Morten,

Thanks for the comment. Do you mean a bug in the Framework
itself? Is this documented somewhere? Have you any more details
or info re. workarounds?

Tim



Sat, 26 Feb 2005 05:54:31 GMT  
 MSHTML events problem
The following article addresses how to handle the event in MSHTML.

Q312777 HOW TO: Handle Document Events in a Visual C# .NET Application
http://support.microsoft.com/support/kb/articles/q312/7/77.asp

Please check your codes based on the above article to see whether there are
some big differences.

Sincerely,

Kevin
Microsoft Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Get Secure! - www.microsoft.com/security

--------------------

| Subject: MSHTML events problem
| Date: Mon, 9 Sep 2002 07:06:18 +0100
| Lines: 1056
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2600.0000
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000

| Newsgroups:
microsoft.public.dotnet.framework.interop,microsoft.public.dotnet.languages.
csharp
| NNTP-Posting-Host: 213.104.76.21
| Path: cpmsftngxa08!tkmsftngp01!tkmsftngp11
| Xref: cpmsftngxa08 microsoft.public.dotnet.languages.csharp:91027
microsoft.public.dotnet.framework.interop:7945
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| I've wasted too much time on this, so I'm posting the code here
| in the hope that someone might have an idea or two.
|
| Last year Lutz Roeder posted some code that host
| mshtml (rather than shdocvw) as an Active Document. The
| code works, but I want to extend it to handle mshtml
| events. I added a ref to mshtml and implemented it as
| shown below, which I believe is correct. This also works,
| but has an unwanted side-effect. When I hook up the event
| handler, the mshtml doc. no longer accepts keyboard input.
| The main result you will see is that  you cannot select text
| in the control. Unhook the event handler and it works
| again. I've posted the code below. Ideas anyone?
|
| Tim
|
| using System;
| using System.Diagnostics;
| using System.IO;
| using System.Runtime.InteropServices;
| using System.Text;
| using System.Windows.Forms;
|
| namespace Reflector.HtmlBrowser
| {
|  /// <summary>
|  /// Implements MSHTML as active document in a panel
|  /// Posted to microsoft.dotnet.framework.general by
|  /// Lutz Roeder. Slightly amended to handle events.
|  /// </summary>
|
|  public delegate void HtmlNavigateEventHandler(Object s,
|  HtmlNavigateEventArgs e);
|
|  public class HtmlNavigateEventArgs
|  {
|   String target;
|
|   public HtmlNavigateEventArgs(String target)
|   {
|    this.target = target;
|   }
|
|   public String Target
|   {
|    get { return target; }
|   }
|  }
|
|  public class HtmlNavigator : Panel
|  {
|   public event HtmlNavigateEventHandler Navigate;
|   HtmlSite site;
|   String url = String.Empty;
|   String document = "<html><body><h1>Test</h1></body></html>";
|
|
|   //OnClick event handler.
|   bool OnClick(mshtml.IHTMLEventObj evobj)
|   {
|    // you can put a breakpoint here and verify it gets called
|    return false;
|   }
|
|   void SetDocEvents()
|   {
|    object m_htmldoc = site.Document;
|
|    if (m_htmldoc != null)
|    {
|     //get the documents2 events interface
|     mshtml.HTMLDocumentEvents2_Event docevents2 =
(mshtml.HTMLDocumentEvents2_Event)m_htmldoc;
|
|     //set the handler
|     docevents2.onclick += new
mshtml.HTMLDocumentEvents2_onclickEventHandler(OnClick);
|    }
|
|   }
|
|   Boolean IsCreated
|   {
|    get { return (site != null) && (site.Document != null); }
|   }
|
|   protected override void OnHandleCreated(EventArgs e)
|   {
|    Console.WriteLine("--");
|
|    base.OnHandleCreated(e);
|
|    // force creating Host handle since we need it to parent MSHTML
|    IntPtr hostHandle = Handle;
|
|    site = new HtmlSite(this);
|    site.CreateDocument();
|    site.ActivateDocument();
|
|    // demo the bug by commenting out this line
|    SetDocEvents();
|
|    if (url != String.Empty) LoadUrl(url);
|    if (document != String.Empty) LoadDocument(document);
|   }
|
|   protected override void OnHandleDestroyed(EventArgs e)
|   {
|    if (IsCreated)
|    {
|     site.CloseDocument();
|     site = null;
|    }
|
|    base.OnHandleDestroyed(e);
|   }
|
|   public void LoadUrl(String url)
|   {
|    this.url = url;
|    if (!IsCreated) return;
|    IPersistMoniker persistMoniker = (IPersistMoniker) site.Document;
|    IMoniker moniker = null;
|    CreateURLMoniker(null, url, out moniker);
|    IBindCtx bindContext = null;
|    CreateBindCtx(0, out bindContext);
|    persistMoniker.Load(1, moniker, bindContext, 0);
|    persistMoniker = null;
|    bindContext = null;
|    moniker = null;
|   }
|
|   public void LoadDocument(String document)
|   {
|    this.document = document;
|    if ((document == null) || (document == String.Empty)) return;
|    if (!IsCreated) return;
|
|    IStream stream = null;
|    CreateStreamOnHGlobal(Marshal.StringToHGlobalUni(document), true, out
|     stream);
|    if (stream == null) return;
|
|    IPersistStreamInit persistentStreamInit = (IPersistStreamInit)
|     site.Document;
|    if (persistentStreamInit != null)
|    {
|     persistentStreamInit.InitNew();
|     persistentStreamInit.Load(stream);
|     persistentStreamInit = null;
|    }
|
|    stream = null;
|   }
|
|   public override Boolean PreProcessMessage(ref Message message)
|   {
|    if ((message.Msg >= WM.KEYFIRST) && (message.Msg <= WM.KEYLAST))
|     if (((message.Msg == WM.KEYDOWN) || (message.Msg == WM.KEYUP)) &&
|      (((int)message.WParam == (int)'G') || ((int)message.WParam ==
(int)'H')))
|     {
|      // Prevent Ctrl+G and Ctrl+H from being passed on, because they
bring up
|      //native trident dialogs
|             Keys modifiers = ModifierKeys;
|      if ((modifiers & Keys.Control) == 0)
|      {
|       MSG msg = new MSG();
|       msg.hwnd = message.HWnd;
|       msg.message = message.Msg;
|       msg.wParam = message.WParam;
|       msg.lParam = message.LParam;
|       msg.time = 0;
|       msg.pt_x = 0;
|       msg.pt_y = 0;
|       return site.TranslateAccelerator(msg);
|      }
|     }
|
|    return base.PreProcessMessage(ref message);
|   }
|
|   public void InvokeNavigate(String target)
|   {
|    if (Navigate != null) Navigate(this, new
HtmlNavigateEventArgs(target));
|   }
|
|   [ClassInterface(ClassInterfaceType.None)]
|    class HtmlSite : IOleClientSite, IOleContainer, IDocHostUIHandler,
|    IOleInPlaceFrame, IOleInPlaceSite, IOleDocumentSite
|   {
|    HtmlNavigator container;
|    IOleObject document;
|    IOleDocumentView view;
|    IOleInPlaceActiveObject activeObject;
|
|    public HtmlSite(HtmlNavigator container)
|    {
|     if ((container == null) || (container.IsHandleCreated == false)) throw
|                       new ArgumentException();
|     this.container = container;
|     container.Resize += new EventHandler(this.Container_Resize);
|    }
|
|    public Object Document
|    {
|     get { return document; }
|    }
|
|    public void CreateDocument()
|    {
|     Debug.Assert(document == null, "Must call Close before recreating.");
|
|     Boolean created = false;
|     try
|     {
|      document = (IOleObject) new HTMLDocument();
|      document.SetClientSite(this);
|      created = true;
|     }
|     finally
|     {
|      if (created == false)
|       document = null;
|     }
|    }
|
|    public void ActivateDocument()
|    {
|     RECT rect = new RECT();
|     GetClientRect(container.Handle, rect);
|     document.DoVerb(OLEIVERB.UIACTIVATE, IntPtr.Zero, this, 0,
|      container.Handle, rect);
|    }
|
|    public void CloseDocument()
|    {
|     const int OLECLOSE_NOSAVE = 1;
|     container.Resize -= new EventHandler(this.Container_Resize);
|     if (document == null) return;
|     document.Close(OLECLOSE_NOSAVE);
|     document.SetClientSite(null);
|     view = null;
|     document = null;
|     activeObject = null;
|    }
|
|    void Container_Resize(Object src, EventArgs e)
|    {
|     if (view == null) return;
|     RECT rect = new RECT();
|     GetClientRect(container.Handle, rect);
|     view.SetRect(rect);
|    }
|
|    public Boolean TranslateAccelerator(MSG msg)
|    {
|     if (activeObject != null)
|      if (activeObject.TranslateAccelerator(msg) != HRESULT.S_FALSE)
|       return true;
|
|     return false;
|    }
|
|    // IOleClientSite
|
|    public int SaveObject()
|    {
|     return HRESULT.S_OK;
|    }
|
|    public int GetMoniker(int dwAssign, int dwWhichMoniker, out Object
ppmk)
|    {
|     ppmk = null;
|     return HRESULT.E_NOTIMPL;
|    }
|
|    public int GetContainer(out IOleContainer ppContainer)
|    {
|     ppContainer = (IOleContainer) this;
|     return HRESULT.S_OK;
|    }
|
|    public int ShowObject()
|    {
|     return HRESULT.S_OK;
|    }
|
|    public int OnShowWindow(int fShow)
|    {
|     return HRESULT.S_OK;
|    }
|
|    public int RequestNewObjectLayout()
|    {
|     return HRESULT.S_OK;
|    }
|
|    // IOleContainer
|
|    public void ParseDisplayName(Object pbc, String pszDisplayName, int[]
|     pchEaten, Object[] ppmkOut)
|    {
|     throw new COMException(String.Empty, HRESULT.E_NOTIMPL);
|    }
|
|    public void EnumObjects(int grfFlags, Object[] ppenum)
|    {
|     throw new COMException(String.Empty, HRESULT.E_NOTIMPL);
|    }
|
|    public void LockContainer(int fLock)
|    {
|    }
|
|    // IOleDocumentSite Implementation
|
|    public int ActivateMe(IOleDocumentView pViewToActivate)
|    {
|     if (pViewToActivate == null) return HRESULT.E_INVALIDARG;
|     RECT rect = new RECT();
|     GetClientRect(container.Handle, rect);
|     view = pViewToActivate;
|     view.SetInPlaceSite((IOleInPlaceSite)this);
|     view.UIActivate(1);
|     view.SetRect(rect);
|     view.Show(1);
|     return HRESULT.S_OK;
|    }
|
|    // IOleInPlaceSite Implementation
|
|    public IntPtr GetWindow()
|    {
|     return container.Handle;
|    }
|
|    public void ContextSensitiveHelp(int fEnterMode)
|    {
|     throw new
...

read more »



Sat, 26 Feb 2005 15:48:57 GMT  
 MSHTML events problem

Quote:
> The following article addresses how to handle the event in MSHTML.

> Q312777 HOW TO: Handle Document Events in a Visual C# .NET Application
> http://support.microsoft.com/support/kb/articles/q312/7/77.asp

> Please check your codes based on the above article to see whether there are
> some big differences.

Hi Kevin,

Thanks for your comment. Yes, I've checked through that article. I also
have Adam Nathan's excellent book .NET and COM
(http://www.amazon.com/exec/obidos/ASIN/067232170X/itwriting-20)
and I've tried implementing both the Raw Approach described there (using
IConnectionPointContainter and FindConnectionPoint) as well as the
event abstraction as also described by Adam and in Q312777. In every case,
the events do fire but I get this very undesirable side-effect. The sample code I
posted demos this well. What I'd like to know is:

1. Is this an interop bug? If so, will it be fixed in the next service pack?
2. Is it a problem with some detail of how the COM interfaces in the posted
code are declared or implemented? If so, please help me track it down
as I've pretty much run out of ideas.
3. A search through the arcives shows that Q311298 may be relevant. This
refers to a problem with the BeforeNavigate event, and although a different
issue it might shed some light. However this article is no longer available on
microsoft.com. Any ideas why it was removed - I haven't been able to find
a copy.

Tim



Sat, 26 Feb 2005 16:56:13 GMT  
 MSHTML events problem

Quote:
> The following article addresses how to handle the event in MSHTML.

> Q312777 HOW TO: Handle Document Events in a Visual C# .NET Application
> http://support.microsoft.com/support/kb/articles/q312/7/77.asp

> Please check your codes based on the above article to see whether there are
> some big differences.

Kevin,

Is there any news on this? These were my main questions, posted earlier this week:

1. Is this an interop bug? If so, will it be fixed in the next service pack?
2. Is it a problem with some detail of how the COM interfaces in the posted
code are declared or implemented? If so, please help me track it down
as I've pretty much run out of ideas.

I don't mind if it is a bug - well, I'd rather there not be bugs, but they always exist.
However I would like to know whether this is an issue I can sort, or whether an
update to the Framework is needed.

Tim



Mon, 28 Feb 2005 16:38:03 GMT  
 MSHTML events problem
Hi Tim,

I am perfoming some research on this, and will get back to you with more
information soon.

Sincerely,

Kevin
Microsoft Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Get Secure! - www.microsoft.com/security

--------------------


| Subject: Re: MSHTML events problem
| Date: Thu, 12 Sep 2002 09:38:03 +0100
| Lines: 25
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106

| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: 213.104.76.21
| Path: cpmsftngxa08!tkmsftngp01!tkmsftngp11
| Xref: cpmsftngxa08 microsoft.public.dotnet.languages.csharp:91857
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|


| > The following article addresses how to handle the event in MSHTML.
| >
| > Q312777 HOW TO: Handle Document Events in a Visual C# .NET Application
| > http://support.microsoft.com/support/kb/articles/q312/7/77.asp
| >
| > Please check your codes based on the above article to see whether there
are
| > some big differences.
|
| Kevin,
|
| Is there any news on this? These were my main questions, posted earlier
this week:
|
| 1. Is this an interop bug? If so, will it be fixed in the next service
pack?
| 2. Is it a problem with some detail of how the COM interfaces in the
posted
| code are declared or implemented? If so, please help me track it down
| as I've pretty much run out of ideas.
|
| I don't mind if it is a bug - well, I'd rather there not be bugs, but
they always exist.
| However I would like to know whether this is an issue I can sort, or
whether an
| update to the Framework is needed.
|
| Tim
|
|
|



Mon, 28 Feb 2005 22:04:27 GMT  
 MSHTML events problem
I have another one possibly:
My app is capturing 'onactivate' and 'ondeactivate' element events.
The event handlers get called correctly, however after the
'ondeactivate' event handler has completed, the element with focus
does not get changed to the clicked on.   I have tried to force a
focus change, but of course this fired the event again...  (DOH)

I would rather use the 'onfocusin' and 'onfocusout' events anyway,
however they simply dont fire...

Anyone else having these troubles?

Thanks in advance...

-BB

On Mon, 9 Sep 2002 07:06:18 +0100, "Tim Anderson"

Quote:

>I've wasted too much time on this, so I'm posting the code here
>in the hope that someone might have an idea or two.

>Last year Lutz Roeder posted some code that host
>mshtml (rather than shdocvw) as an Active Document. The
>code works, but I want to extend it to handle mshtml
>events. I added a ref to mshtml and implemented it as
>shown below, which I believe is correct. This also works,
>but has an unwanted side-effect. When I hook up the event
>handler, the mshtml doc. no longer accepts keyboard input.
>The main result you will see is that  you cannot select text
>in the control. Unhook the event handler and it works
>again. I've posted the code below. Ideas anyone?

>Tim

>using System;
>using System.Diagnostics;
>using System.IO;
>using System.Runtime.InteropServices;
>using System.Text;
>using System.Windows.Forms;

>namespace Reflector.HtmlBrowser
>{
> /// <summary>
> /// Implements MSHTML as active document in a panel
> /// Posted to microsoft.dotnet.framework.general by
> /// Lutz Roeder. Slightly amended to handle events.
> /// </summary>

> public delegate void HtmlNavigateEventHandler(Object s,
> HtmlNavigateEventArgs e);

> public class HtmlNavigateEventArgs
> {
>  String target;

>  public HtmlNavigateEventArgs(String target)
>  {
>   this.target = target;
>  }

>  public String Target
>  {
>   get { return target; }
>  }
> }

> public class HtmlNavigator : Panel
> {
>  public event HtmlNavigateEventHandler Navigate;
>  HtmlSite site;
>  String url = String.Empty;
>  String document = "<html><body><h1>Test</h1></body></html>";

>  //OnClick event handler.
>  bool OnClick(mshtml.IHTMLEventObj evobj)
>  {
>   // you can put a breakpoint here and verify it gets called
>   return false;
>  }

>  void SetDocEvents()
>  {
>   object m_htmldoc = site.Document;

>   if (m_htmldoc != null)
>   {
>    //get the documents2 events interface
>    mshtml.HTMLDocumentEvents2_Event docevents2 = (mshtml.HTMLDocumentEvents2_Event)m_htmldoc;

>    //set the handler
>    docevents2.onclick += new mshtml.HTMLDocumentEvents2_onclickEventHandler(OnClick);
>   }

>  }

>  Boolean IsCreated
>  {
>   get { return (site != null) && (site.Document != null); }
>  }

>  protected override void OnHandleCreated(EventArgs e)
>  {
>   Console.WriteLine("--");

>   base.OnHandleCreated(e);

>   // force creating Host handle since we need it to parent MSHTML
>   IntPtr hostHandle = Handle;

>   site = new HtmlSite(this);
>   site.CreateDocument();
>   site.ActivateDocument();

>   // demo the bug by commenting out this line
>   SetDocEvents();

>   if (url != String.Empty) LoadUrl(url);
>   if (document != String.Empty) LoadDocument(document);
>  }

>  protected override void OnHandleDestroyed(EventArgs e)
>  {
>   if (IsCreated)
>   {
>    site.CloseDocument();
>    site = null;
>   }

>   base.OnHandleDestroyed(e);
>  }

>  public void LoadUrl(String url)
>  {
>   this.url = url;
>   if (!IsCreated) return;
>   IPersistMoniker persistMoniker = (IPersistMoniker) site.Document;
>   IMoniker moniker = null;
>   CreateURLMoniker(null, url, out moniker);
>   IBindCtx bindContext = null;
>   CreateBindCtx(0, out bindContext);
>   persistMoniker.Load(1, moniker, bindContext, 0);
>   persistMoniker = null;
>   bindContext = null;
>   moniker = null;
>  }

>  public void LoadDocument(String document)
>  {
>   this.document = document;
>   if ((document == null) || (document == String.Empty)) return;
>   if (!IsCreated) return;

>   IStream stream = null;
>   CreateStreamOnHGlobal(Marshal.StringToHGlobalUni(document), true, out
>    stream);
>   if (stream == null) return;

>   IPersistStreamInit persistentStreamInit = (IPersistStreamInit)
>    site.Document;
>   if (persistentStreamInit != null)
>   {
>    persistentStreamInit.InitNew();
>    persistentStreamInit.Load(stream);
>    persistentStreamInit = null;
>   }

>   stream = null;
>  }

>  public override Boolean PreProcessMessage(ref Message message)
>  {
>   if ((message.Msg >= WM.KEYFIRST) && (message.Msg <= WM.KEYLAST))
>    if (((message.Msg == WM.KEYDOWN) || (message.Msg == WM.KEYUP)) &&
>     (((int)message.WParam == (int)'G') || ((int)message.WParam == (int)'H')))
>    {
>     // Prevent Ctrl+G and Ctrl+H from being passed on, because they bring up
>     //native trident dialogs
>            Keys modifiers = ModifierKeys;
>     if ((modifiers & Keys.Control) == 0)
>     {
>      MSG msg = new MSG();
>      msg.hwnd = message.HWnd;
>      msg.message = message.Msg;
>      msg.wParam = message.WParam;
>      msg.lParam = message.LParam;
>      msg.time = 0;
>      msg.pt_x = 0;
>      msg.pt_y = 0;
>      return site.TranslateAccelerator(msg);
>     }
>    }

>   return base.PreProcessMessage(ref message);
>  }

>  public void InvokeNavigate(String target)
>  {
>   if (Navigate != null) Navigate(this, new HtmlNavigateEventArgs(target));
>  }

>  [ClassInterface(ClassInterfaceType.None)]
>   class HtmlSite : IOleClientSite, IOleContainer, IDocHostUIHandler,
>   IOleInPlaceFrame, IOleInPlaceSite, IOleDocumentSite
>  {
>   HtmlNavigator container;
>   IOleObject document;
>   IOleDocumentView view;
>   IOleInPlaceActiveObject activeObject;

>   public HtmlSite(HtmlNavigator container)
>   {
>    if ((container == null) || (container.IsHandleCreated == false)) throw
>                      new ArgumentException();
>    this.container = container;
>    container.Resize += new EventHandler(this.Container_Resize);
>   }

>   public Object Document
>   {
>    get { return document; }
>   }

>   public void CreateDocument()
>   {
>    Debug.Assert(document == null, "Must call Close before recreating.");

>    Boolean created = false;
>    try
>    {
>     document = (IOleObject) new HTMLDocument();
>     document.SetClientSite(this);
>     created = true;
>    }
>    finally
>    {
>     if (created == false)
>      document = null;
>    }
>   }

>   public void ActivateDocument()
>   {
>    RECT rect = new RECT();
>    GetClientRect(container.Handle, rect);
>    document.DoVerb(OLEIVERB.UIACTIVATE, IntPtr.Zero, this, 0,
>     container.Handle, rect);
>   }

>   public void CloseDocument()
>   {
>    const int OLECLOSE_NOSAVE = 1;
>    container.Resize -= new EventHandler(this.Container_Resize);
>    if (document == null) return;
>    document.Close(OLECLOSE_NOSAVE);
>    document.SetClientSite(null);
>    view = null;
>    document = null;
>    activeObject = null;
>   }

>   void Container_Resize(Object src, EventArgs e)
>   {
>    if (view == null) return;
>    RECT rect = new RECT();
>    GetClientRect(container.Handle, rect);
>    view.SetRect(rect);
>   }

>   public Boolean TranslateAccelerator(MSG msg)
>   {
>    if (activeObject != null)
>     if (activeObject.TranslateAccelerator(msg) != HRESULT.S_FALSE)
>      return true;

>    return false;
>   }

>   // IOleClientSite

>   public int SaveObject()
>   {
>    return HRESULT.S_OK;
>   }

>   public int GetMoniker(int dwAssign, int dwWhichMoniker, out Object ppmk)
>   {
>    ppmk = null;
>    return HRESULT.E_NOTIMPL;
>   }

>   public int GetContainer(out IOleContainer ppContainer)
>   {
>    ppContainer = (IOleContainer) this;
>    return HRESULT.S_OK;
>   }

>   public int ShowObject()
>   {
>    return HRESULT.S_OK;
>   }

>   public int OnShowWindow(int fShow)
>   {
>    return HRESULT.S_OK;
>   }

>   public int RequestNewObjectLayout()
>   {
>    return HRESULT.S_OK;
>   }

>   // IOleContainer

>   public void ParseDisplayName(Object pbc, String pszDisplayName, int[]
>    pchEaten, Object[] ppmkOut)
>   {
>    throw new COMException(String.Empty, HRESULT.E_NOTIMPL);
>   }

>   public void EnumObjects(int grfFlags, Object[] ppenum)
>   {
>    throw new COMException(String.Empty, HRESULT.E_NOTIMPL);
>   }

>   public void LockContainer(int fLock)
>   {
>   }

>   // IOleDocumentSite Implementation

>   public int ActivateMe(IOleDocumentView pViewToActivate)
>   {
>    if (pViewToActivate == null) return HRESULT.E_INVALIDARG;
>    RECT rect = new RECT();
>    GetClientRect(container.Handle, rect);
>    view = pViewToActivate;
>    view.SetInPlaceSite((IOleInPlaceSite)this);
>    view.UIActivate(1);
>    view.SetRect(rect);
>    view.Show(1);
>    return HRESULT.S_OK;
>   }

>   // IOleInPlaceSite Implementation

>   public IntPtr GetWindow()
>   {
>    return container.Handle;
>   }

>   public void ContextSensitiveHelp(int fEnterMode)
>   {
>    throw new COMException(String.Empty, HRESULT.E_NOTIMPL);
>   }

>   public int CanInPlaceActivate()
>   {
>    return HRESULT.S_OK;
>   }

>   public void OnInPlaceActivate()
>   {
>   }

>   public void OnUIActivate()
>   {
>   }

>   public void GetWindowContext(out IOleInPlaceFrame ppFrame, out Object
>    ppDoc, RECT lprcPosRect, RECT lprcClipRect, tagOIFI lpFrameInfo)
>   {
>    ppDoc = null;
>    ppFrame = (IOleInPlaceFrame) this;
>    GetClientRect(container.Handle, lprcPosRect);
>    GetClientRect(container.Handle, lprcClipRect);
>    lpFrameInfo.cb = Marshal.SizeOf(typeof(tagOIFI));
>    lpFrameInfo.fMDIApp = 0;
>    lpFrameInfo.hwndFrame = container.Handle;
>    lpFrameInfo.hAccel = IntPtr.Zero;
>    lpFrameInfo.cAccelEntries = 0;
>   }

>   public int Scroll(Object scrollExtant)
>   {
>    return HRESULT.E_NOTIMPL;
>   }

>   public void OnUIDeactivate(int fUndoable)
>   {
>   }

>   public void

...

read more »



Wed, 30 Mar 2005 12:19:15 GMT  
 MSHTML events problem

Quote:

> I have another one possibly:
> My app is capturing 'onactivate' and 'ondeactivate' element events.
> The event handlers get called correctly, however after the
> 'ondeactivate' event handler has completed, the element with focus
> does not get changed to the clicked on.   I have tried to force a
> focus change, but of course this fired the event again...  (DOH)

> I would rather use the 'onfocusin' and 'onfocusout' events anyway,
> however they simply dont fire...

Is this with the webbrowser ActiveX or by directly hosting MSHTML?

Tim



Wed, 30 Mar 2005 20:59:49 GMT  
 MSHTML events problem

Quote:
> Is this with the webbrowser ActiveX or by directly hosting MSHTML?

> Tim

Tim,

There's a difference?
What are the ways to include both of these methods in an app?

I have references to:
AxInterop.SHDocVw
Interop.SHDocVw
Microsoft.mshtml

And I have the following problems:
1. Works fine in VS.net, but fails after deploying to client even if it's
the same as the development machine.
2. If I hide the form, then show it again, the webbrowser object shows a
blank white page and my html stuff is gone.

Any help would be appreciated.
--

Thanks in Advance,
Byron



Sat, 02 Apr 2005 12:41:24 GMT  
 MSHTML events problem

Quote:

> > Is this with the webbrowser ActiveX or by directly hosting MSHTML?

> > Tim

> Tim,

> There's a difference?
> What are the ways to include both of these methods in an app?

Yes, some of us have been working on hosting MSHTML as an
ActiveX document. This is suitable if you want to work with HTML
but don't necessarily want to browse the web. You also have more
control over some aspects of its behaviour. I've also found that
hosting MSHTML in this way is more reliable than working with
the ActiveX control.

However, we are talking about lots of manually declared COM
interop functions and a certain amount of guesswork and
experimentation, so not for the fainthearted.

Quote:
> I have references to:
> AxInterop.SHDocVw
> Interop.SHDocVw
> Microsoft.mshtml

you only need the last of these, or not even that if you want a
stripped-down version. However, I've found it convenient to
set a reference to Microsoft.mshtml.

Quote:
> And I have the following problems:
> 1. Works fine in VS.net, but fails after deploying to client even if it's
> the same as the development machine.

Not had any deployment probs so far, although I require IE 5.5 or
higher.

Quote:
> 2. If I hide the form, then show it again, the webbrowser object shows a
> blank white page and my html stuff is gone.

I think this is working as designed. The web browser isn't meant to work
properly when non-visible. You have to reload the document.

Tim



Sun, 03 Apr 2005 04:32:49 GMT  
 MSHTML events problem
Tim,

Thanks for the response it clears up alot.  I would appreciate your comments
on the following ideas.

Quote:
> Yes, some of us have been working on hosting MSHTML as an
> ActiveX document. This is suitable if you want to work with HTML
> but don't necessarily want to browse the web. You also have more
> control over some aspects of its behaviour. I've also found that
> hosting MSHTML in this way is more reliable than working with
> the ActiveX control.

I won't be using it to browser the web, so it sounds like just using MSHTML
is the way to go.
My needs are simple (unless there's a better way).
1. Use navigation as a way to send commands to the host app.  For example
I'll have a coded link: #App.Doc1.GetDetails
Once the link is clicked I use the link as a way to control content and
cancel the navigation.  (Works great.)
2. Get a reference to an object via it's id, read and or set it's attribute
or content using innerHtml.

With that I can do alot.

Without using ActiveX how to set the BeforeNavigateEvent?  I've been using
the following which I think is giving me deployment problems:
------------------------
  private AxSHDocVw.AxWebBrowser fwbMain;
  private SHDocVw.WebBrowser_V1 fwbMain2; // define an IE3 compatible
webbrowser object.
  private IHTMLDocument3 _oDoc;//=(IHTMLDocument3)fwbMain.Document;
  private IHTMLElement _oID;
...
   object o=null;
   fwbMain.Navigate(_sPathApp + _sCache + _sMain,ref o,ref o,ref o,ref o);
   Application.DoEvents();

   object oOcx = fwbMain.GetOcx();
   try
   {
    fwbMain2 = oOcx as SHDocVw.WebBrowser_V1;
    fwbMain2.BeforeNavigate += new
SHDocVw.DWebBrowserEvents_BeforeNavigateEventHandler(this.fwbMain2_BeforeNav
igate);
   }
   catch (Exception ex)
   {
    Trace.WriteLine("Add BeforeNavigate event handler failed with " +
ex.Message);
   }

------------------------

The following works, but not all the styles work as they do if I used the
WebBrowser control in VB6:
------------------------
   _oDoc=(IHTMLDocument3) fwbMain.Document;
   _oID=(IHTMLElement)_oDoc.getElementById("oSystem");
   _oID.innerHTML=_stringBuilderMenu.ToString();

------------------------

I use the following event handlers, strange how I need to use 2 different
objects fwbMain and fwbMain2:
------------------------
private void fwbMain2_BeforeNavigate(string URL, int Flags, string
TargetFrameName, ref object PostData, string Headers, ref bool Processed)
private void fwbMain_DocumentComplete(object sender,
AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e)
------------------------

Quote:
> > 2. If I hide the form, then show it again, the webbrowser object shows a
> > blank white page and my html stuff is gone.

> I think this is working as designed. The web browser isn't meant to work
> properly when non-visible. You have to reload the document.

Reloading doesn't work, tried that.  Refreshing, setting the object to
visibility to true... tried all those.

--
Thanks in Advance,
Byron



Sun, 03 Apr 2005 13:27:57 GMT  
 MSHTML events problem

Quote:
> Tim,
> I won't be using it to browser the web, so it sounds like just using MSHTML
> is the way to go.
> My needs are simple (unless there's a better way).
> 1. Use navigation as a way to send commands to the host app.  For example
> I'll have a coded link: #App.Doc1.GetDetails
> Once the link is clicked I use the link as a way to control content and
> cancel the navigation.  (Works great.)
> 2. Get a reference to an object via it's id, read and or set it's attribute
> or content using innerHtml.

Using MSHTML *should* be the way to go, but there is this little problem
that forms the original topic of the thread. When you hook up an event
handler to document, it no longer works properly. I've got round this by
using the PropertyNotifyEvent, which lets me catch events like
DocumentComplete because it changes the ReadyState property. However,
that doesn't apply to all the document events. I think there may be a way
round it, but I've not been working on it too hard since I don't need the
document events. I do implement IDocHostUIHandler, which catches useful
things like mouse events in the UpdateUI method.

It's possible that .Net 1.1 may fix the events issue, who knows.

Quote:
> The following works, but not all the styles work as they do if I used the
> WebBrowser control in VB6:

I've had zero problems so far manipulating the MSHTML DOM.

Quote:
> > > 2. If I hide the form, then show it again, the webbrowser object shows a
> > > blank white page and my html stuff is gone.

This seems to work fine with MSHTML. I load a document, show, hide and
re-show the form, and the document persists OK. If you Refresh using IE's
popup menu it disappears though :-)  We'll be replacing this menu with our
own.

Tim



Mon, 04 Apr 2005 00:15:48 GMT  
 MSHTML events problem
On Sat, 12 Oct 2002 13:59:49 +0100, "Tim Anderson"

Quote:


>> I have another one possibly:
>> My app is capturing 'onactivate' and 'ondeactivate' element events.
>> The event handlers get called correctly, however after the
>> 'ondeactivate' event handler has completed, the element with focus
>> does not get changed to the clicked on.   I have tried to force a
>> focus change, but of course this fired the event again...  (DOH)

>> I would rather use the 'onfocusin' and 'onfocusout' events anyway,
>> however they simply dont fire...

>Is this with the webbrowser ActiveX or by directly hosting MSHTML?

>Tim

using the WebBrowser ActiveX.   Also, I've tried using DocumentEvents
instead recently .. the 'click' event is the only one that fires ..
but the 'click' then does not get sent to the element ...   :-(


Tue, 05 Apr 2005 15:26:04 GMT  
 MSHTML events problem
On Sat, 12 Oct 2002 13:59:49 +0100, "Tim Anderson"

Quote:


>> I have another one possibly:
>> My app is capturing 'onactivate' and 'ondeactivate' element events.
>> The event handlers get called correctly, however after the
>> 'ondeactivate' event handler has completed, the element with focus
>> does not get changed to the clicked on.   I have tried to force a
>> focus change, but of course this fired the event again...  (DOH)

>> I would rather use the 'onfocusin' and 'onfocusout' events anyway,
>> however they simply dont fire...

>Is this with the webbrowser ActiveX or by directly hosting MSHTML?

>Tim

Tim,
is using the ActiveX ..

thanks

-BB



Fri, 03 Jun 2005 14:31:17 GMT  
 
 [ 16 post ]  Go to page: [1] [2]

 Relevant Pages 

1. MSHTML events problem

2. mshtml HTMLDocumentClass event problem

3. MSHTML events workaround

4. MSHTML and Events????

5. SHDocVw.InternetExplorer and mshtml Document object problem

6. Problems using MSHTML control in CAxWindow

7. MFC ActiveX control/MSHTML.DLL unload problem

8. Problem writing an event to the Event Log

9. Error 'Cannot convert mshtml.IHTMLElement to mshtml.IHTMLDomNode..

10. Error 'Cannot convert mshtml.IHTMLElement to mshtml.IHTMLDomNode..

11. using SHDocVw and mshtml

12. MSHTML and IDM_COMPOSESETTINGS

 

 
Powered by phpBB® Forum Software