• [Sciter] Script与Native交互


    原帖地址: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 >对象实例

  • 相关阅读:
    函数类型和函数对象
    _strdup和strdup还有string.h和string事
    VS包含头文件的路径设置
    (转)C++学习建议
    2.2Python数据处理篇之---math模块的数学函数
    2.1Python数据处理篇之---内建有关数学的函数
    19LaTeX学习系列之---LaTeX的总结
    18LaTeX学习系列之---LaTeX的参考文献
    17LaTeX学习系列之---LaTeX的版面设计
    16LaTeX学习系列之---LaTeX数学公式的补充
  • 原文地址:https://www.cnblogs.com/yinxufeng/p/5653920.html
Copyright © 2020-2023  润新知