i am trying to get updates, when the view of a .net webbrowser control is updated. So i implemented the IViewObject of this one and tried to use setadvise, so that a function is called, when an update happened.
The interesting code is this:
Expand|Select|Wrap|Line Numbers
- IHTMLDocument2 htmlDocument = (IHTMLDocument2)this.webBrowser1.Document.DomDocument;
- IViewObject ViewObject = htmlDocument as IViewObject;
- if (ViewObject == null)
- {
- MessagheBox.Show("An error has occurred in initialization of the IViewObject of te WebBrowser.");
- }
- try
- {
- ViewObject.SetAdvise((int)DVASPECT.DVASPECT_CONTENT, 0, (IAdviseSink)this);
- }
- catch (Exception e_tmp)
- {
- MessageBox.Show(e_tmp.Message);
- }
But this doesnt work. I dont get any error, it just doesnt delivers any calls, when the view of the webbrowser changed. Any
MessageBox.Show("ss"); part gets never called (look at bottom, implemention of the IAdviseSink)
Complete Example:
Expand|Select|Wrap|Line Numbers
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Runtime.InteropServices.ComTypes;
- using System.Data;
- using System.Drawing;
- using System.Security.Permissions;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Windows.Forms;
- using mshtml;
- namespace WindowsApplication3
- {
- [ComVisible(true), ComImport()]
- [GuidAttribute("0000010d-0000-0000-C000-000000000046")]
- [InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
- public interface IViewObject
- {
- [return: MarshalAs(UnmanagedType.I4)]
- [PreserveSig]
- int Draw(
- //tagDVASPECT
- [MarshalAs(UnmanagedType.U4)] UInt32 dwDrawAspect,
- int lindex,
- IntPtr pvAspect,
- [In] IntPtr ptd,
- //[MarshalAs(UnmanagedType.Struct)] ref DVTARGETDEVICE ptd,
- IntPtr hdcTargetDev,
- IntPtr hdcDraw,
- [MarshalAs(UnmanagedType.Struct)] ref tagRECT lprcBounds,
- [MarshalAs(UnmanagedType.Struct)] ref tagRECT lprcWBounds,
- IntPtr pfnContinue,
- [MarshalAs(UnmanagedType.U4)] UInt32 dwContinue);
- void Freeze(
- [MarshalAs(UnmanagedType.U4)] UInt32 dwDrawAspect,
- int lindex,
- IntPtr pvAspect,
- out IntPtr pdwFreeze);
- void Unfreeze(
- [MarshalAs(UnmanagedType.U4)] int dwFreeze);
- void SetAdvise
- ([MarshalAs(UnmanagedType.U4)] int aspects,
- [MarshalAs(UnmanagedType.U4)] int advf,
- [MarshalAs(UnmanagedType.Interface)] IAdviseSink pAdvSink);
- void GetAdvise(
- IntPtr paspects,
- IntPtr advf,
- [Out, MarshalAs(UnmanagedType.Interface)] out IAdviseSink pAdvSink);
- }
- public class Form1 : Form, IAdviseSink
- {
- public Form1()
- {
- this.webBrowser1 = new System.Windows.Forms.WebBrowser();
- this.button1 = new System.Windows.Forms.Button();
- this.SuspendLayout();
- //
- // webBrowser1
- //
- this.webBrowser1.Location = new System.Drawing.Point(52, 96);
- this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20);
- this.webBrowser1.Name = "webBrowser1";
- this.webBrowser1.Size = new System.Drawing.Size(339, 166);
- this.webBrowser1.TabIndex = 0;
- this.webBrowser1.Url = new System.Uri("http://www.heise.de",system.UriKind.Absolute);
- //
- // button1
- //
- this.button1.Location = new System.Drawing.Point(95, 45);
- this.button1.Name = "button1";
- this.button1.Size = new System.Drawing.Size(75, 23);
- this.button1.TabIndex = 1;
- this.button1.Text = "button1";
- this.button1.UseVisualStyleBackColor = true;
- this.button1.Click += new System.EventHandler(this.button1_Click);
- //
- // Form1
- //
- this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
- this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
- this.ClientSize = new System.Drawing.Size(403, 266);
- this.Controls.Add(this.button1);
- this.Controls.Add(this.webBrowser1);
- this.Name = "Form1";
- this.Text = "Form1";
- this.ResumeLayout(false);
- }
- private System.Windows.Forms.WebBrowser webBrowser1;
- private System.Windows.Forms.Button button1;
- private void button1_Click(object sender, EventArgs e)
- {
- IHTMLDocument2 htmlDocument = (IHTMLDocument2)this.webBrowser1.Document.DomDocument;
- IViewObject ViewObject = htmlDocument as IViewObject;
- if (ViewObject == null)
- {
- MessageBox.Show("An error has occurred in initialization of the IViewObject of the WebBrowser.");
- }
- try
- {
- ViewObject.SetAdvise((int)DVASPECT.DVASPECT_CONTENT, 0, (IAdviseSink)this);
- }
- catch (Exception e_tmp)
- {
- MessageBox.Show(e_tmp.Message);
- }
- }
- // Implementation of the IAdviseSink
- public void OnDataChange(ref FORMATETC pFormat, ref STGMEDIUM pStg)
- {
- MessageBox.Show("ss");
- }
- public void OnViewChange(int dwAspect, int index)
- {
- MessageBox.Show("ss");
- }
- public void OnRename(IMoniker im)
- {
- MessageBox.Show("ss");
- }
- public void OnSave()
- {
- MessageBox.Show("ss");
- }
- public void OnClose()
- {
- MessageBox.Show("ss");
- }
- }
- static class Program
- {
- /// <summary>
- /// The main entry point for the application.
- /// </summary>
- [STAThread]
- static void Main()
- {
- Application.EnableVisualStyles();
- Application.SetCompatibleTextRenderingDefault(false);
- Application.Run(new Form1());
- }
- }
- }