zoukankan      html  css  js  c++  java
  • Web调用宿主窗体的方法

    window窗体加载一个WebBrowse控件,加载一个页面,页面中可以使用脚本语言调用external.方法(参数) 这样的方法执行并获取宿主窗体中的数据。

    在这里主要使一个窗体类继承几个WebCom。包括[ICustomDoc、ICustomMethods.....] ,具体各个类的定义可以网上搜索一下,也可通知我,Mail给你。

    下面主要介绍一下如何实现一个调用方法的过程。

    1.定一个窗体类,分别继承与WebCom的那些借口。

    2.在窗口类中实现接口的方法。

    3.定义ICustomMethods 接口,在这个类中定义自己的方法,参数及返回值。

    例如:


    public interface ICustomMethods : IDispatch
    {
    string MyCustomMethod([MarshalAs(UnmanagedType.BStr)] string theCaption);

    }

    4.form类实现ICustomMethods接口,实现MyCustomMethod方法。

    例如

    public string MyCustomMethod([MarshalAs(UnmanagedType.BStr)] string theCaption)
    {
    return "This is a Test";

    }

    5. 在form的构造函数中,注册告诉WebBrowse现在form是他的宿主。

    代码如下:

    #region 必须的
    object obj = axWebBrowser1.GetOcx();
    IOleObject oc = obj as IOleObject;
    oc.SetClientSite(this);
    #endregion

    6.现在就可以写一个HTML,使用javaScript ,调用form中的方法了。

    var xmlstr;
    xmlstr=external.MyCustomMethod("");


    此外,还可以在实现其他接口的过程中改变WebBrowse的一些其他的行为,

    例出alert的时候,弹出自己定义的鼠标右键:(通过实现IDocHostShowUI中的ShowMessage来实现)

    public uint ShowMessage(IntPtr hwnd, string msgText, string msgCaption,
    uint dwType, string lpstrHelpFile, uint dwHelpContext,
    out int lpResult)
    {

    lpResult = 0;

    // return one of these values
    const int MessageHandled = 0; // MsHtml won't display its MessageBox
    // const int MessageNotHandled = 1; // MsHtml will display its MessageBox

    // show a custom message, using a new caption. Be default, messages displayed
    // by MsHtml have the caption "Microsoft Internet Explorer"
    MessageBox.Show(msgText, "MyCustomWebBrowser");
    return MessageHandled;
    }
    public uint ShowHelp(IntPtr hwnd, string pszHelpFile, uint uCommand, uint dwData,
    tagPOINT ptMouse, object pDispatchObjectHit)
    {
    // pDispatchObject will reference an object of type mshtml.HTMLDocumentClass, or
    // other class representing something on the HTML page.

    // tagPOINT will normally be (0,0), unless ShowHelp() was called as the result of
    // clicking on an HTML element.

    // return one of these values
    //const int HelpHandled = 0; // MsHtml won't display its Help window
    const int HelpNotHandled = 1; // MsHtml will display its Help window

    return HelpNotHandled;
    }


    又例如修改browse上的鼠标右键:(可以通过IDocHostUIHandler 中的ShowContextMenu的方法实现 )

    public uint ShowContextMenu(uint dwID, ref tagPOINT ppt, object pcmdtReserved, object pdispReserved)
    {
    // use this code to show a custom menu
    const int MenuHandled = 0;
    Point p = new Point(ppt.x, ppt.y);
    p = PointToClient(p);
    // myCustomContextMenu.Show(this, p);
    return MenuHandled;

    // use this code to let MsHtml shows its menu
    // const int MenuNotHandled = 1;
    // return MenuNotHandled;
    }

    设定Browser 的UI。
    public void GetHostInfo(ref DOCHOSTUIINFO theHostUIInfo)
    {
    // turn three flags on
    theHostUIInfo.dwFlags |= (uint) (DOCHOSTUIFLAG.DOCHOSTUIFLAG_SCROLL_NO |
    DOCHOSTUIFLAG.DOCHOSTUIFLAG_NO3DBORDER |
    DOCHOSTUIFLAG.DOCHOSTUIFLAG_DIALOG);
    }

    //屏蔽快捷键

    public uint TranslateAccelerator(ref tagMSG lpMsg, ref Guid pguidCmdGroup, uint nCmdID)
    {
    const int KillAccelerator = 0;
    const int AllowAccelerator = 1;
    const int WM_KEYDOWN = 0x0100;

    if (lpMsg.message != WM_KEYDOWN)
    // allow message
    return AllowAccelerator;

    if ( (Control.ModifierKeys & Keys.Control) != Keys.Control)
    return AllowAccelerator;

    // disable the Ctrl-N and Ctrl-P accelerators
    lpMsg.wParam &= 0xFF; // get the virtual keycode
    if ( (lpMsg.wParam == 'N') || ((lpMsg.wParam == 'P')) )
    return KillAccelerator;

    // allow everything else
    return AllowAccelerator;
    }

    通过以上的方法,就可以自定义自己的WebBrowser了,但是目前我发现,只有一个Form的类可以实现以上的方法,如果要写一个ToolBar的话,也想使用Externel的方法,我想没有办法实现这个过程,可能有,不过还有没查到,希望知道的朋友将发放Mail给我。

    另外,如果一个Form实现以上,那么Externel本来自带的那几个方法就不能使用了。比如使用external的方法添加到收常夹等方法。



  • 相关阅读:
    nginx之location、rewrite配置
    nio buffer
    分布式事务
    彻底剖析RMI底层源码 、手写轻量级RMI框架
    Java RMI详解
    Java提高篇——对象克隆(复制)
    序列化
    分布式通信-tcp/ip 广播
    分布式通信-tcp/ip 单播
    php 图像处理 抠图,生成背景透明png 图片
  • 原文地址:https://www.cnblogs.com/zhucl1006/p/897226.html
Copyright © 2011-2022 走看看