原帖地址:http://www.cnblogs.com/yinxufeng/p/5653920.html

Script与Native交互基础

1 参照SDK中“plain-win”的例子,自定义一个窗口类,继承自sciter::host< window >sciter::event_handler

  1. class window: public sciter::host<window>
  2. , public sciter::event_handler
  3. {
  4. HWND _hwnd;
  5. static LRESULT CALLBACK wnd_proc(HWND, UINT, WPARAM, LPARAM);
  6. static window* ptr(HWND hwnd);
  7. static bool init_class();
  8. public:
  9. // notification_handler traits:
  10. HWND get_hwnd() const { return _hwnd; }
  11. HINSTANCE get_resource_instance() const{ return ghInstance; }
  12. window();
  13. bool init(); // instance
  14. bool is_valid() const { return _hwnd != 0; }
  15. };

注意

HWND get_hwnd() const { return _hwnd; }

HINSTANCE get_resource_instance() const{ return ghInstance; }

必须要实现,否则host无法获取资源

2 在窗口初始化的时候,通过sciter::attach_dom_event_handler绑定Sciter DOM事件到该窗口上

  1. bool window::init()
  2. {
  3. SetWindowLongPtr(_hwnd, GWLP_USERDATA, LONG_PTR(this));
  4. setup_callback();
  5. // // attach event_handler to the window
  6. sciter::attach_dom_event_handler(_hwnd, this);
  7. load_file(L"res:default.htm");
  8. return true;
  9. }

简化方式

SDK中已经为每个操作系统平台定义了各自的“主”函数

  • sciter-sdk/include/sciter-win-main.cpp - on Windows
  • sciter-sdk/include/sciter-osx-main.mm - on OS X
  • sciter-sdk/include/sciter-gtk-main.cpp - on Linux

这里我们将sciter-win-main.cpp包含到我们项目中,自定义个窗口类,继承自sciter::window

Script调用Native

在自定义窗口类中,通过类似MFC的消息映射方式来绑定事件处理函数

  1. BEGIN_FUNCTION_MAP
  2. FUNCTION_0("helloWorld", helloWorld);
  3. FUNCTION_2("native_sum", native_sum); // 后面的2表示有2个参数
  4. FUNCTION_0("native_api", native_api);
  5. END_FUNCTION_MAP
  6. sciter::string helloWorld() { return L"Hello u-minimal World"; }
  7. int native_sum(sciter::value a, sciter::value b) { return a.d + b.d; }
  8. sciter::value native_api() {
  9. sciter::value api_map;
  10. sciter::value api_math_map;
  11. std::function<int(int,int)> native_sum = [](int a, int b) { return a + b; };
  12. std::function<int(int,int)> native_sub = [](int a, int b) { return a - b; };
  13. api_math_map.set_item(sciter::value("sum"), sciter::vfunc( native_sum ));
  14. api_math_map.set_item(sciter::value("sub"), sciter::vfunc( native_sub ));
  15. api_map.set_item(sciter::value("math"), api_math_map);
  16. return api_map;
  17. }

在tiscript中这么调用

  1. <script type="text/tiscript">
  2. var message = view.helloWorld();
  3. view.native_sum(a, b);
  4. view.nativeApi().math.sub(c, d);
  5. </script>

这里的view是全局对象,代表当前运行Sciter的窗口, 他有很多自带的函数,如close(关闭), msgbox(消息框),selectFile(文件选择框),Update等。

FUNCTION_MAP中定义的函数映射,可以通过view来直接调用:view.native_sum(a, b)。

另外有一种方法可以将Native写的函数包装在一起,比如native_api,view在调用的时候,直接使用view.native_api().math.xxx

sciter::value native_api()函数,返回值是一个Map类型,转换成sciter::value,结构类似下面:

return {
math: {
sum: {native_sum},
sub: {native_sub},
}
}

Native调用Script

比如在script中有这么一个方法:

  1. <script type="text/tiscript">
  2. namespace Test {
  3. function add(n1,n2) { return n1+n2; }
  4. }
  5. </script>

在Native中这么调用:

  1. sciter::dom::element root = self->get_root();
  2. sciter::value r;
  3. try {
  4. r = root.call_function("Test.add",sciter::value(2),sciter::value(2));
  5. } catch (sciter::script_error& err) {
  6. std::cerr << err.what() << std::endl;
  7. }
  8. // or sciter::value r = self->call_function("Test.add",sciter::value(2),sciter::value(2));
  9. assert(r.is_int() && r.get(0) == 4);

Test是script中的命名空间

self是当前窗口sciter::host< window >对象实例

[Sciter] Script与Native交互的更多相关文章

  1. js与native交互

    js与native交互 UIWebView Native调用JS,使用stringByEvaluatingJavaScriptFromString来解释执行js脚本. //script即为要执行的js ...

  2. [Sciter系列] MFC下的Sciter–3.Sciter脚本与底层交互

    [Sciter系列] MFC下的Sciter–3.Sciter脚本与底层交互,脚本调用底层自定义的方法函数. 本系列文章的目的就是一步步构建出一个功能可用,接口基本完善的基于MFC框架的SciterF ...

  3. Hybrid App: 对比UIWebView和WebKit实现JavaScript与Native交互

    一.简介 在前面一篇文章中讲到过实现JavaScript与Native交互的方式有一种就是使用原生内嵌webView.在iOS8之前,开发者只能使用苹果提供的UIWebView类来加载URL或者HTM ...

  4. React Native交互组件之Touchable

    React Native交互组件之Touchable:只要在组件外面包一个Touchable组件就可以实现点击交互. TouchableHighlight:高亮触摸 当点击时,组件的透明度会改变,可以 ...

  5. 【quickhybrid】H5和Native交互原理

    前言 Hybrid架构的核心就是JSBridge交互,而实现这个交互的前提是弄清楚H5和Native端的交互 本文主要介绍Native端(Android/iOS)和H5端(泛指前端)的交互原理 (之前 ...

  6. 与native交互时会出现的问题

    1.jsbridge:  可以用jsbridge与native交互,这属于第三方库,前端后端都需要加jsbridge 2.可以直接调用原生的方法,ios:  window.webkit.message ...

  7. 《React-Native系列》RN与native交互与数据传递

    RN怎么与native交互的呢? 下面我们通过一个简单的Demo来实现:RN页面调起Native页面,Native页面选择电话本数据,将数据回传给RN展示. 首先是 Native侧 1.MainAct ...

  8. 《React-Native系列》3、RN与native交互之Callback、Promise

    接着上一篇<React-Native系列>RN与native交互与数据传递,我们接下来研究另外的两种RN与Native交互的机制 一.Callback机制 首先Calllback是异步的, ...

  9. Android中H5和Native交互的两种方式

    Android中H5和Native交互的两种方式:http://www.jianshu.com/p/bcb5d8582d92 注意事项: 1.android给h5页面注入一个对象(WZApp),这个对 ...

随机推荐

  1. linux shell 搭建本地yum 源,通过IOS镜像 Centeros6,7还有redhat

    Centeros: 准备工作: 将系统镜像放到 /opt 下 脚本: #!/bin/bash mkdir /mnt/cdrom mount -o loop /opt/*.ios;# 此处改为你的系统镜 ...

  2. 剑指Offer(书):斐波那契数列

    题目:大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0).n<=39 分析:第一种方法:递归,45时,时间为5s,50时,我就等不及了.原因是重 ...

  3. 【BZOJ 1222】 [HNOI2001] 产品加工(DP)

    Description 某加工厂有A.B两台机器,来加工的产品可以由其中任何一台机器完成,或者两台机器共同完成.由于受到机器性能和产品特性的限制,不同的机器加工同一产品所需的时间会不同,若同时由两台机 ...

  4. Huffman codes

    05-树9 Huffman Codes(30 分) In 1953, David A. Huffman published his paper "A Method for the Const ...

  5. CentOS 7下安装Composer + Laravel

    1.wget https://dl.laravel-china.org/composer.phar -O /usr/local/bin/composer chmod a+x /usr/local/bi ...

  6. python基础——1(简介与变量)

    目录 一.编程语言介绍 1.1.机器语言: 1.2.汇编语言: 1.3.高级语言: 二.安装python解释器 2.1.验证python解释器的安装 2.2.设置环境变量 三.执行python程序的两 ...

  7. 【ITOO 2】.NET 动态建库建表:使用SQL字符串拼接方式

    导读:在最近接手的项目(高效云平台)中,有一个需求是要当企业用户注册时,给其动态的新建一个库和表.刚开始接手的时候,是一点头绪都没有,然后查了一些资料,也问了问上一版本的师哥师姐,终于有了点头绪.目前 ...

  8. C. The Smallest String Concatenation-C++sort排序~~

    C. The Smallest String Concatenation time limit per test 3 seconds memory limit per test 256 megabyt ...

  9. hibernate保存oracle日期时间

    用java生成一个带年月日时分秒的时间,通过hibernate对象保存到oracle中的Date字段中, 第一种方法: java实体类的createDate属性,类型为java.util.Date h ...

  10. jQuery的切换函数(hover,toggle)

    1.hover:(鼠标悬停与离开事件) 一个模仿悬停事件(鼠标移动到一个对象上面及移出这个对象)的方法.这是一个自定义的方法,它为频繁使用的任务提供了一种“保持在其中”的状态. 当鼠标移动到一个匹配的 ...