[Sciter] Script与Native交互
Script与Native交互基础
1 参照SDK中“plain-win”的例子,自定义一个窗口类,继承自sciter::host< window >和sciter::event_handler
class window: public sciter::host<window>, public sciter::event_handler{HWND _hwnd;static LRESULT CALLBACK wnd_proc(HWND, UINT, WPARAM, LPARAM);static window* ptr(HWND hwnd);static bool init_class();public:// notification_handler traits:HWND get_hwnd() const { return _hwnd; }HINSTANCE get_resource_instance() const{ return ghInstance; }window();bool init(); // instancebool is_valid() const { return _hwnd != 0; }};
注意:
HWND get_hwnd() const { return _hwnd; }
HINSTANCE get_resource_instance() const{ return ghInstance; }
必须要实现,否则host无法获取资源
2 在窗口初始化的时候,通过sciter::attach_dom_event_handler绑定Sciter DOM事件到该窗口上
bool window::init(){SetWindowLongPtr(_hwnd, GWLP_USERDATA, LONG_PTR(this));setup_callback();// // attach event_handler to the windowsciter::attach_dom_event_handler(_hwnd, this);load_file(L"res:default.htm");return true;}
简化方式
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的消息映射方式来绑定事件处理函数
BEGIN_FUNCTION_MAPFUNCTION_0("helloWorld", helloWorld);FUNCTION_2("native_sum", native_sum); // 后面的2表示有2个参数FUNCTION_0("native_api", native_api);END_FUNCTION_MAPsciter::string helloWorld() { return L"Hello u-minimal World"; }int native_sum(sciter::value a, sciter::value b) { return a.d + b.d; }sciter::value native_api() {sciter::value api_map;sciter::value api_math_map;std::function<int(int,int)> native_sum = [](int a, int b) { return a + b; };std::function<int(int,int)> native_sub = [](int a, int b) { return a - b; };api_math_map.set_item(sciter::value("sum"), sciter::vfunc( native_sum ));api_math_map.set_item(sciter::value("sub"), sciter::vfunc( native_sub ));api_map.set_item(sciter::value("math"), api_math_map);return api_map;}
在tiscript中这么调用
<script type="text/tiscript">var message = view.helloWorld();view.native_sum(a, b);view.nativeApi().math.sub(c, d);</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中有这么一个方法:
<script type="text/tiscript">namespace Test {function add(n1,n2) { return n1+n2; }}</script>
在Native中这么调用:
sciter::dom::element root = self->get_root();sciter::value r;try {r = root.call_function("Test.add",sciter::value(2),sciter::value(2));} catch (sciter::script_error& err) {std::cerr << err.what() << std::endl;}// or sciter::value r = self->call_function("Test.add",sciter::value(2),sciter::value(2));assert(r.is_int() && r.get(0) == 4);
Test是script中的命名空间
self是当前窗口sciter::host< window >对象实例
[Sciter] Script与Native交互的更多相关文章
- js与native交互
js与native交互 UIWebView Native调用JS,使用stringByEvaluatingJavaScriptFromString来解释执行js脚本. //script即为要执行的js ...
- [Sciter系列] MFC下的Sciter–3.Sciter脚本与底层交互
[Sciter系列] MFC下的Sciter–3.Sciter脚本与底层交互,脚本调用底层自定义的方法函数. 本系列文章的目的就是一步步构建出一个功能可用,接口基本完善的基于MFC框架的SciterF ...
- Hybrid App: 对比UIWebView和WebKit实现JavaScript与Native交互
一.简介 在前面一篇文章中讲到过实现JavaScript与Native交互的方式有一种就是使用原生内嵌webView.在iOS8之前,开发者只能使用苹果提供的UIWebView类来加载URL或者HTM ...
- React Native交互组件之Touchable
React Native交互组件之Touchable:只要在组件外面包一个Touchable组件就可以实现点击交互. TouchableHighlight:高亮触摸 当点击时,组件的透明度会改变,可以 ...
- 【quickhybrid】H5和Native交互原理
前言 Hybrid架构的核心就是JSBridge交互,而实现这个交互的前提是弄清楚H5和Native端的交互 本文主要介绍Native端(Android/iOS)和H5端(泛指前端)的交互原理 (之前 ...
- 与native交互时会出现的问题
1.jsbridge: 可以用jsbridge与native交互,这属于第三方库,前端后端都需要加jsbridge 2.可以直接调用原生的方法,ios: window.webkit.message ...
- 《React-Native系列》RN与native交互与数据传递
RN怎么与native交互的呢? 下面我们通过一个简单的Demo来实现:RN页面调起Native页面,Native页面选择电话本数据,将数据回传给RN展示. 首先是 Native侧 1.MainAct ...
- 《React-Native系列》3、RN与native交互之Callback、Promise
接着上一篇<React-Native系列>RN与native交互与数据传递,我们接下来研究另外的两种RN与Native交互的机制 一.Callback机制 首先Calllback是异步的, ...
- Android中H5和Native交互的两种方式
Android中H5和Native交互的两种方式:http://www.jianshu.com/p/bcb5d8582d92 注意事项: 1.android给h5页面注入一个对象(WZApp),这个对 ...
随机推荐
- [LUOGU] P3469 [POI2008]BLO-Blockade
https://www.luogu.org/problemnew/show/P3469 求无向图分别删去每个点后不连通的点对数. 首先,对于任何一个点,它本身删了,就会和剩下的n-1个点不连通,点对是 ...
- <Spring Cloud>入门五 hystrix
1.服务熔断 1.1引入坐标 <dependency> <groupId>org.springframework.cloud</groupId> <artif ...
- 杭电 1241 Oil Deposits (很好的dfs)
Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...
- DEV Express
记录在使用DEV Express中遇到的所有问题及解决方案 问题1:将Dev11升级到Dev14 解决方案:将解决方案中原有Dev引用删除,重新添加必须的Dev14引用,问题解决: 问题2:LC.ex ...
- selenium之文件上传
文件上传是所有UI自动化测试都要面对的一个头疼问题,今天博主在这里给大家分享下自己处理文件上传的经验,希望能够帮助到广大被文件上传坑住的seleniumer. 首先,我们要区分出上传按钮的种类,大体上 ...
- Python模块学习 - openpyxl读写excel
openpyxl模块介绍 openpyxl模块是一个读写Excel 2010文档的Python库,如果要处理更早格式的Excel文档,需要用到额外的库,openpyxl是一个比较综合的工具,能够同时读 ...
- 【转载】CentOS6.5升级手动安装GCC4.8.2
一.简易安装 操作环境 CentOS6.5 64bit,原版本4.4.7,不能支持C++11的特性~,希望升级到4.8.2 不能通过yum的方法升级,需要自己手动下载安装包并编译 1.1 获取安装包并 ...
- 【数据传输 1】服务器—>客户端之间的数据类型转换
导读:在做项目的时候,在controller中,将List数据类型转换为了JSON字符串,那么,为什么要将其数据转换为JOSN呢?这样的转换是否是必须的,在这个转换过程中,又经过了那些步骤?注:本篇博 ...
- hdu 3940
#include<stdio.h> #include<math.h> #include<string.h> double first(double vx,doubl ...
- UVA12103 贪心+置换
题意:给出26个大写字母的置换B,问是否存在一个置换A,舍得A^2=B,如果存在输出Yes,否则输出No 题解: 研究一下置换A与A^2关系. 假设A=(a1 a2 a3)(b1 b2 b3 b4) ...