Winforms 在webBrowser中显示闪烁超高

Winforms 在webBrowser中显示闪烁超高,winforms,internet-explorer-8,c#-2.0,Winforms,Internet Explorer 8,C# 2.0,我通过IInternetSession.RegisterTimeFilter将MIME筛选器添加到我的WebBrowser控件 MIME类型为“text/html” 过滤器有一个错误,flash无法在某些站点显示,例如“http://cn.yahoo.com/". 我 尝试在标记前添加“”,无效 我直接返回数据,但falsh仍然不可见 我将IE安全级别设置为最低(internet、intranet、activex)。仍然无效 如果我不注册MIME过滤器,flash可以显示所有方式 有什么想法吗

我通过IInternetSession.RegisterTimeFilter将MIME筛选器添加到我的WebBrowser控件 MIME类型为“text/html”

过滤器有一个错误,flash无法在某些站点显示,例如“http://cn.yahoo.com/". 我

尝试在标记前添加“”,无效

我直接返回数据,但falsh仍然不可见

我将IE安全级别设置为最低(internet、intranet、activex)。仍然无效

如果我不注册MIME过滤器,flash可以显示所有方式

有什么想法吗

    [ComVisible(true)]
[Guid("1c470f4b-0486-4558-80c7-ad08d652a465"), ClassInterface(ClassInterfaceType.AutoDispatch)]
public class MimeHandler : IInternetProtocol, IInternetProtocolRoot, IInternetProtocolSink
{
    const int S_OK = 0x00000000;
    const int S_FALSE = 0x00000001;
    const int E_FAIL = unchecked((int)0x80004005);
    const int E_POINTER = unchecked((int)0x80004005);
    const int INET_E_ERROR_FIRST = unchecked((int)0x800C0002);
    const int INET_E_INVALID_URL = unchecked((int)0x800C0002);
    const int INET_E_NO_SESSION = unchecked((int)0x800C0003);
    const int INET_E_CANNOT_CONNECT = unchecked((int)0x800C0004);
    const int INET_E_RESOURCE_NOT_FOUND = unchecked((int)0x800C0005);
    const int INET_E_OBJECT_NOT_FOUND = unchecked((int)0x800C0006);
    const int INET_E_DATA_NOT_AVAILABLE = unchecked((int)0x800C0007);
    const int INET_E_DOWNLOAD_FAILURE = unchecked((int)0x800C0008);
    const int INET_E_AUTHENTICATION_REQUIRED = unchecked((int)0x800C0009);
    const int INET_E_NO_VALID_MEDIA = unchecked((int)0x800C000A);
    const int INET_E_CONNECTION_TIMEOUT = unchecked((int)0x800C000B);
    const int INET_E_INVALID_REQUEST = unchecked((int)0x800C000C);
    const int INET_E_UNKNOWN_PROTOCOL = unchecked((int)0x800C000D);
    const int INET_E_SECURITY_PROBLEM = unchecked((int)0x800C000E);
    const int INET_E_CANNOT_LOAD_DATA = unchecked((int)0x800C000F);
    const int INET_E_CANNOT_INSTANTIATE_OBJECT = unchecked((int)0x800C0010);
    const int INET_E_USE_DEFAULT_PROTOCOLHANDLER = unchecked((int)0x800C0011);
    const int INET_E_QUERYOPTION_UNKNOWN = unchecked((int)0x800C0013);
    const int INET_E_REDIRECT_FAILED = unchecked((int)0x800C0014);
    const int INET_E_REDIRECT_TO_DIR = unchecked((int)0x800C0015);
    const int INET_E_CANNOT_LOCK_REQUEST = unchecked((int)0x800C0016);

    private string cacheFileName = "";

    private string url;

    private MemoryStream dataStream = new MemoryStream();

    private IInternetProtocol urlMonProtocol;

    private IInternetProtocolSink urlMonProtocolSink;

    private byte[] buffer = null;
    private uint written = 0;

    private long totalSize = 0;

    protected MemoryStream Stream = new MemoryStream(0x8000);

    protected byte[] StreamBuffer = new byte[0x8000];

    private string MimeType = "";
    private string char_set = "";

    public MimeHandler()
    {

    }  

    public void Start(string szURL, IInternetProtocolSink Sink,
        IInternetBindInfo pOIBindInfo, uint grfPI, uint dwReserved)
    {
        MimeType = szURL;

        urlMonProtocol = (IInternetProtocol)Sink;  
        urlMonProtocolSink = Sink;  

        uint Fetched = 0;
        const int BINDSTRING_URL = 14;
        pOIBindInfo.GetBindString(BINDSTRING_URL, ref url, 1, ref Fetched);

        throw new COMException("", S_OK);
    }


    public void ReportProgress(uint ulStatusCode, string szStatusText)
    {
        uint bindStatus = Convert.ToUInt32(BINDSTATUS.BINDSTATUS_CACHEFILENAMEAVAILABLE);

        if (ulStatusCode == bindStatus)
        {
            cacheFileName = szStatusText;
        }

        urlMonProtocolSink.ReportProgress(ulStatusCode, szStatusText);
        throw new COMException("", S_OK);
    }


    public void ReportData(BSCF grfBSCF, uint ulProgress, uint ulProgressMax)
    {
        UInt32 resultOfRead;
        dataStream = new MemoryStream();

        do
        { //while result == S_OK there is still data  

            byte[] readBuffer = new byte[1024];

            uint totalRead;


            int size = Marshal.SizeOf(readBuffer[0]) * readBuffer.Length;


            IntPtr unmanagedPointer = Marshal.AllocHGlobal(size);

            Marshal.Copy(readBuffer, 0, unmanagedPointer, readBuffer.Length);

            // Call unmanaged code  
            resultOfRead = urlMonProtocol.Read(unmanagedPointer, Convert.ToUInt32(readBuffer.Length), out totalRead);


            byte[] receiveBuffer = new byte[1024];


            Marshal.Copy(unmanagedPointer, receiveBuffer, 0, 1024);

            dataStream.Write(receiveBuffer, 0, Convert.ToInt32(totalRead));

            //free memory  

            Marshal.FreeHGlobal(unmanagedPointer);

        } //while (resultOfRead == HRESULT.S_OK);
        while ((resultOfRead != S_FALSE) &&
            (resultOfRead != INET_E_DOWNLOAD_FAILURE) && (resultOfRead != INET_E_DATA_NOT_AVAILABLE));


        if (resultOfRead == HRESULT.S_FALSE)
        {
            //all data recieved  
            dataStream.Flush();

            totalSize = dataStream.Length;

            //INSERT MANIPULATION OF DATA!  

            HandleData();


            urlMonProtocolSink.ReportData(
                BSCF.BSCF_FIRSTDATANOTIFICATION | 
                BSCF.BSCF_LASTDATANOTIFICATION | 
                BSCF.BSCF_DATAFULLYAVAILABLE, Convert.ToUInt32(totalSize), Convert.ToUInt32(totalSize));

            urlMonProtocolSink.ReportResult(Convert.ToInt32(HRESULT.S_OK), HRESULT.S_OK, null);
        }
        throw new COMException("", S_OK);
    }  

    public UInt32 Read(System.IntPtr pv, uint cb, out uint pcbRead)
    {  
        pcbRead = 0;

        if (written < totalSize)
        {
            if (totalSize - written <= cb)
            {
                Marshal.Copy(buffer, (int)written, pv, (int)(totalSize - written));
                pcbRead = (uint)(totalSize - written);
                written += (uint)(totalSize - written);
                return (UInt32)S_FALSE; 
            }
            else
            {
                Marshal.Copy(buffer, (int)written, pv, (int)cb);
                written += cb;
                pcbRead = cb;
                return (UInt32)S_OK; 
            }
        }
        else
        { return (UInt32)S_FALSE; }
    }


    public void Resume()
    {
        urlMonProtocol.Resume();
        throw new COMException("", unchecked((int)0x80004001));
    }


    public void Terminate(uint dwOptions)
    {
        urlMonProtocol.Terminate(dwOptions);
        throw new COMException("", S_OK);
    }

    public void Seek(_LARGE_INTEGER dlibMove, uint dwOrigin, out _ULARGE_INTEGER plibNewPosition)
    {
        urlMonProtocol.Seek(dlibMove, dwOrigin, out plibNewPosition);
        throw new COMException("", S_OK);
    }


    public void LockRequest(uint dwOptions)
    {
        urlMonProtocol.LockRequest(dwOptions);
        throw new COMException("", S_OK);
    }


    public void UnlockRequest()
    {
        urlMonProtocol.UnlockRequest();
        throw new COMException("", S_OK);
    }


    public void Abort(int hrReason, uint dwOptions)
    {
        urlMonProtocol.Abort(hrReason, dwOptions);
        throw new COMException("", S_OK);
    }

    public void Suspend()
    {
        urlMonProtocol.Suspend();

        throw new COMException("", unchecked((int)0x80004001));
    }

    public void Continue(ref _tagPROTOCOLDATA pProtocolData)
    {
        urlMonProtocol.Continue(ref pProtocolData);
        throw new COMException("", S_OK);
    }

    public void Switch(ref _tagPROTOCOLDATA pProtocolData)
    {
        urlMonProtocolSink.Switch(ref pProtocolData);
        throw new COMException("", S_OK);
    }

    public void ReportResult(int hrResult, uint dwError, string szResult)
    {
        urlMonProtocolSink.ReportResult(hrResult, dwError, szResult);
        throw new COMException("", S_OK);
    }

    public static IHttpNegotiate GetHttpNegotiate(IInternetProtocolSink Sink)
    {  

         if ((Sink is IServiceProvider) == false)  

             throw new Exception("Error ProtocolSink does not support IServiceProvider.");  

         IServiceProvider Provider = (IServiceProvider) Sink;  

         object obj_Negotiate = new object();  

         Provider.QueryService(ref Guids.IID_IHttpNegotiate, ref Guids.IID_IHttpNegotiate, out obj_Negotiate);  

         return (IHttpNegotiate) obj_Negotiate;  
     }  


    public static BINDINFO GetBindInfo(IInternetBindInfo pOIBindInfo)
    {  
        BINDINFO BindInfo = new BINDINFO();  

        BindInfo.cbSize = (UInt32) Marshal.SizeOf(typeof (BINDINFO));  

        UInt32 AsyncFlag;  

        pOIBindInfo.GetBindInfo(out AsyncFlag, ref BindInfo);  

        return BindInfo;  
    }  


    private void HandleData()
    {
        buffer = new byte[(int)dataStream.Length];
        dataStream.Seek(0, SeekOrigin.Begin);
        dataStream.Read(buffer, 0, buffer.Length);
        written = 0;
        totalSize = buffer.Length;
        /*
        string content = BytesToString(buffer, out char_set);

        if (this.MimeType.IndexOf("javascript",StringComparison.OrdinalIgnoreCase) != -1)
        {

        }
        else if (this.MimeType.IndexOf("html", StringComparison.OrdinalIgnoreCase) != -1)
        {
            Regex reg_first = new Regex("<!DOCTYPE[^>]+?>",RegexOptions.IgnoreCase);
            Match m = reg_first.Match(content);
            if(m.Success)
            {
                int head = content.IndexOf(m.Value,StringComparison.OrdinalIgnoreCase);
                if (head != -1)
                {
                    string l = url.Length.ToString();
                    while (l.Length < 4)
                    {
                        l = "0" + l;
                    }

                    l = "(" + l + ")" + url;

                    content = content.Substring(0, head + m.Value.Length) +
                        "\r\n <!-- saved from url=" + l + " --> " +
                        content.Substring(head + m.Value.Length);
                }
            }
        }


        byte[] data = StringToBytes(content, char_set);

        dataStream = new MemoryStream(data);

        buffer = new byte[(int)dataStream.Length];
        dataStream.Seek(0, SeekOrigin.Begin);
        dataStream.Read(buffer, 0, buffer.Length);
        written = 0;
        totalSize = buffer.Length;*/
    }

    private string BytesToString(byte[] data,out string charSet)
    {
        charSet = "";
        string sHtml = Encoding.Default.GetString(data);
        string CharSet = "";

        Regex rg = new Regex("<meta.+?charset=(?<name>.*?)\".*?>", RegexOptions.IgnoreCase);
        Match m = rg.Match(sHtml);

        if(m.Success)
        {
            CharSet = m.Groups["name"].Value;
            charSet = CharSet;
        }

        if (CharSet.Length > 0)
        {
            sHtml = Encoding.GetEncoding(CharSet).GetString(data);
        }

        return sHtml;
    }

    private byte[] StringToBytes(string text,string charSet)
    {
        if (charSet.Length > 0)
        { return Encoding.GetEncoding(charSet).GetBytes(text); }
        else
        { return Encoding.Default.GetBytes(text); }
    }
}  
[ComVisible(true)]
[Guid(“1c470f4b-0486-4558-80c7-ad08d652a465”),ClassInterface(ClassInterfaceType.AutoDispatch)]
公共类MIMEMHandler:IInternetProtocolRoot、IInternetProtocolSink
{
常数int S_OK=0x00000000;
常量int S_FALSE=0x00000001;
const int E_FAIL=未检查((int)0x80004005);
常量int E_指针=未选中((int)0x80004005);
const int INET_E_ERROR_FIRST=未选中((int)0x800C0002);
const int INET_E_INVALID_URL=未选中((int)0x800C0002);
const int INET_E_NO_SESSION=未选中((int)0x800C0003);
const int INET\u E\u CANNOT\u CONNECT=未选中((int)0x800C0004);
const int INET_E_RESOURCE_NOT_FOUND=未选中((int)0x800C0005);
const int INET_E_OBJECT_NOT_FOUND=未选中((int)0x800C0006);
const int INET\u E\u DATA\u NOT\u AVAILABLE=未选中((int)0x800C0007);
const int INET_E_下载失败=未选中((int)0x800C0008);
const int INET_E_AUTHENTICATION_REQUIRED=未选中((int)0x800C0009);
const int INET\u E\u NO\u VALID\u MEDIA=未选中((int)0x800C000A);
const int INET\u E\u CONNECTION\u TIMEOUT=unchecked((int)0x800C000B);
const int INET_E_INVALID_REQUEST=未选中((int)0x800C000C);
const int INET_E_UNKNOWN_PROTOCOL=未选中((int)0x800C000D);
const int INET\u E\u SECURITY\u PROBLEM=unchecked((int)0x800C000E);
const int INET\u E\u无法加载数据=未选中((int)0x800C000F);
const int INET\u E\u无法实例化\u对象=未选中((int)0x800C0010);
const int INET_E_USE_DEFAULT_PROTOCOLHANDLER=unchecked((int)0x800C0011);
const int INET_E_QUERYOPTION_UNKNOWN=未选中((int)0x800C0013);
const int INET_E_REDIRECT_失败=未选中((int)0x800C0014);
const int INET_E_REDIRECT_TO_DIR=未选中((int)0x800C0015);
const int INET\u E\u CANNOT\u LOCK\u REQUEST=未选中((int)0x800C0016);
私有字符串cacheFileName=“”;
私有字符串url;
私有MemoryStream数据流=新MemoryStream();
专用网络协议;
私有互联网协议接收器urlMonProtocolSink;
专用字节[]缓冲区=null;
私有uint写入=0;
私有长totalSize=0;
受保护的MemoryStream流=新的MemoryStream(0x8000);
受保护字节[]StreamBuffer=新字节[0x8000];
私有字符串MimeType=“”;
私有字符串char_set=“”;
公共MimeHandler()
{
}  
public void Start(字符串szURL、IInternetProtocolSink、,
IIINTERNETBINDINFO pOIBindInfo、uint grfPI、uint DW保留)
{
MimeType=szURL;
urlMonProtocol=(IInternetProtocol)接收器;
urlMonProtocolSink=接收器;
uint-Fetched=0;
const int BINDSTRING_URL=14;
GetBindString(BINDSTRING\uURL,ref-URL,1,ref-Fetched);
抛出新的COMException(“,S_OK);
}
public void ReportProgress(uint-ulStatusCode,字符串szStatusText)
{
uint bindStatus=Convert.ToUInt32(bindStatus.bindStatus\u cachefilename可用);
if(ulStatusCode==bindStatus)
{
cacheFileName=szStatusText;
}
urlMonProtocolSink.ReportProgress(ulStatusCode,szStatusText);
抛出新的COMException(“,S_OK);
}
公共作废报告数据(BSCF grfBSCF、uint ulProgress、uint ulProgress MAX)
{
UInt32结果读取;
数据流=新的内存流();
做
{//result==S_OK时仍有数据
字节[]读缓冲区=新字节[1024];
uint-totalRead;
int size=Marshal.SizeOf(readBuffer[0])*readBuffer.Length;
IntPtr unmanagedPointer=Marshal.AllocHGlobal(大小);
Copy(readBuffer,0,非托管指针,readBuffer.Length);
//调用非托管代码
resultOfRead=urlMonProtocol.Read(非托管指针,Convert.ToUInt32(readBuffer.Length),out totalRead);
字节[]接收缓冲区=新字节[1024];
副本(非托管指针,接收缓冲区,0,1024);
dataStream.Write(receiveBuffer,0,Convert.ToInt32(totalRead));
//空闲内存
FreeHGlobal元帅(非托管点);
}//while(resultOfRead==HRESULT.S_OK);
while((resultOfRead!=S_FALSE)&&
(resultOfRead!=INET_E_下载失败)&(resultOfRead!=INET_E_数据不可用);
if(resultOfRead==HRESULT.S\u FALSE)
{
//收到的所有数据
dataStream.Flush();
totalSize=dataStream.Length;
//插入数据操作!
HandleData();
urlMonProtocolSink.ReportData(
BSCF.BSCF_FIRSTDATANOTIFICATION|
BSCF.BSCF_LASTDATANOTIFICATION|
BSCF.BSCF_datafully可用,Convert.ToUInt32(totalSize),Convert.ToUInt32(totalSize));
urlMonProtocolSink.ReportResult(转换为32(HRESULT.S_OK),HRESULT.S_OK,null);
}
抛出新的COMException(“,S_OK);
}  
公共UInt32读取(System.IntPtr pv、uint cb、out uint pcbRead)
{  
pcbRead=0;
如果(写入<总大小)
{

如果(totalSize-writed我已经解决了这个问题

[ComVisible(true)]
[Guid("1c470f4b-0486-4558-80c7-ad08d652a465"), ClassInterface(ClassInterfaceType.AutoDispatch)]
public class MimeHandler : IInternetProtocol, IInternetProtocolRoot, IInternetProtocolSink
{
.......
//private static ArrayList not = new ArrayList();

private static string newUrl = "";
public static string NewUrl //set it berfor Navigate
{
get
{ return newUrl; }
set
{ 
newUrl = value;
not.Clear();
}
}

private void HandleData()
{
    buffer = new byte[(int)dataStream.Length];
    dataStream.Seek(0, SeekOrigin.Begin);
    dataStream.Read(buffer, 0, buffer.Length);
    written = 0;
    totalSize = buffer.Length;

    string content = BytesToString(buffer, out char_set);

    if(string.Equals(url,newUrl))
    {
        Regex reg_js = new Regex("(?<head><script[^<>]+src=\"*)(?<url>[^\"\\s>]+)(?<end>\"*[^<>]*>\\s*</script>)", RegexOptions.IgnoreCase);
        MatchCollection mc_js = reg_js.Matches(content);
        foreach (Match m in mc_js)
        {
            if (!m.Groups["url"].Value.EndsWith(".js",StringComparison.OrdinalIgnoreCase))
            { not.Add(m.Groups["url"].Value); }
        }
    }

    byte[] data = StringToBytes(content, char_set);

    dataStream = new MemoryStream(data);

    buffer = new byte[(int)dataStream.Length];
    dataStream.Seek(0, SeekOrigin.Begin);
    dataStream.Read(buffer, 0, buffer.Length);
    written = 0;
    totalSize = buffer.Length;
}

public void Start(string szURL, IInternetProtocolSink Sink,
IInternetBindInfo pOIBindInfo, uint grfPI, uint dwReserved)
{
    uint Fetched = 0;
    const int BINDSTRING_URL = 14;
    pOIBindInfo.GetBindString(BINDSTRING_URL, ref url, 1, ref Fetched);

    for (int i = 0; i < not.Count;++i )
    {
        Regex reg_end = new Regex("\\.html?$");
        string t = (string)not[i];

        if (string.Equals(reg_end.Replace(t, ""), reg_end.Replace(url, "")))
        {
            throw new COMException("", INET_E_USE_DEFAULT_PROTOCOLHANDLER);
        }
    }

    MimeType = szURL;

    urlMonProtocol = (IInternetProtocol)Sink; 
    urlMonProtocolSink = Sink; 

    //System.IO.File.AppendAllText("C:\\url.txt", url + "\r\n");
    throw new COMException("", S_OK);
}

//.......
}
[ComVisible(true)]
[Guid(“1c470f4b-0486-4558-80c7-ad08d652a465”),ClassInterface(ClassInterfaceType.AutoDispatch)]
公共类MIMEMHandler:IInternetProtocolRoot、IInternetProtocolSink
{
.......
//私有静态数组列表