说明:
本文章旨在总结备份、方便以后查询,由于是个人总结,如有不对,欢迎指正;另外,内容大部分来自网络、书籍、和各类手册,如若侵权请告知,马上删帖致歉。
QQ 群 号:513683159 【相互学习】
内容来源:
官方的手册(免费获取)Pxx=该手册的对应页码xx
github-awtk
normal_dialog.c
#include "awtk.h"
static ret_t on_yes(void* ctx, event_t* e) {
tk_quit(); //退出TK事件主循环(整个程序退出)
return RET_OK;
}
static ret_t on_no(void* ctx, event_t* e) {
window_close(widget_get_window(WIDGET(e->target))); //关闭窗口(对话框)
return RET_OK;
}
static ret_t on_open_dialog(void* ctx, event_t* e) {
widget_t* ok = NULL;
widget_t* cancel = NULL;
widget_t* label = NULL;
widget_t* dlg = dialog_create_simple(NULL, 0, 0, 240, 160); //创建dialog对象,同时创建title/client。
dialog_set_title(dlg, "Dialog"); //设置对话框的标题文本:Dialog
ok = button_create(dialog_get_client(dlg), 20, 80, 80, 30);//在对话框中创建button对象
widget_on(ok, EVT_CLICK, on_yes, NULL); //注册指定事件的处理函数
widget_set_text(ok, L"Yes"); //设置控件的文本:Yes
cancel = button_create(dialog_get_client(dlg), 140, 80, 80, 30);//在对话框中创建button对象
widget_on(cancel, EVT_CLICK, on_no, NULL); //注册指定事件的处理函数
widget_set_text(cancel, L"No"); //设置控件的文本:No
label = label_create(dialog_get_client(dlg), 10, 30, 200, 30);
widget_set_text(label, L"Are you sure to quit?"); //设置控件的文本:Are you sure to quit?
return RET_OK;
}
ret_t application_init() {
widget_t* win = window_create(NULL, 0, 0, 0, 0); //创建window对象
widget_t* ok = button_create(win, 0, 0, 0, 0); //创建button对象
locale_info_change(locale_info(), "zh", "CN"); //设置当前的国家和语言。
widget_set_tr_text(ok, "ok"); //获取翻译之后的文本,然后调用widget_set_text
widget_set_self_layout_params(ok, "center", "middle", "50%", "30"); //设置控件自己的布局(缺省布局器)参数(过时,请用widget_set_self_layout)。
widget_on(ok, EVT_CLICK, on_open_dialog, NULL); //注册指定事件的处理函数
widget_layout(win); //布局当前控件及子控件
return RET_OK;
}
modal_dialog.c
#include "awtk.h"
static ret_t on_yes(void* ctx, event_t* e) {
dialog_quit(widget_get_window(WIDGET(e->target)), DIALOG_QUIT_YES); //退出模态显示,关闭对话框。
return RET_OK;
}
static ret_t on_no(void* ctx, event_t* e) {
dialog_quit(widget_get_window(WIDGET(e->target)), DIALOG_QUIT_NO); //退出模态显示,关闭对话框。
return RET_OK;
}
static ret_t on_open_dialog(void* ctx, event_t* e) {
int code = 0;
widget_t* ok = NULL;
widget_t* cancel = NULL;
widget_t* label = NULL;
widget_t* dlg = dialog_create_simple(NULL, 0, 0, 240, 160); //创建dialog对象,同时创建title/client。
dialog_set_title(dlg, "Dialog"); //设置对话框的标题文本:Dialog
ok = button_create(dialog_get_client(dlg), 20, 80, 80, 30); //在对话框中创建button对象
widget_on(ok, EVT_CLICK, on_yes, NULL); //注册指定事件的处理函数
widget_set_text(ok, L"Yes"); //设置控件的文本:Yes
cancel = button_create(dialog_get_client(dlg), 140, 80, 80, 30); //在对话框中创建button对象
widget_on(cancel, EVT_CLICK, on_no, NULL); //注册指定事件的处理函数
widget_set_text(cancel, L"No"); //设置控件的文本:No
label = label_create(dialog_get_client(dlg), 10, 30, 200, 30); //在对话框中创建label对象
widget_set_text(label, L"Are you sure to quit?"); //设置控件的文本:Are you sure to quit?
code = dialog_modal(dlg); //模态显示对话框。 dialog_modal返回后,dialog对象将在下一个idle函数中回收。也就是在dialog_modal调用完成后仍然可以访问dialog中控件,直到本次事件结束。
if(code == DIALOG_QUIT_YES) {
tk_quit(); //退出TK事件主循环(整个程序退出)
}
return RET_OK;
}
ret_t application_init() {
widget_t* win = window_create(NULL, 0, 0, 0, 0); //创建window对象
widget_t* ok = button_create(win, 0, 0, 0, 0); //创建button对象
locale_info_change(locale_info(), "zh", "CN"); //设置当前的国家和语言。
widget_set_tr_text(ok, "ok"); //获取翻译之后的文本,然后调用widget_set_text
widget_set_self_layout_params(ok, "center", "middle", "50%", "30"); //设置控件自己的布局(缺省布局器)参数(过时,请用widget_set_self_layout)。
widget_on(ok, EVT_CLICK, on_open_dialog, NULL); //注册指定事件的处理函数
widget_layout(win); //布局当前控件及子控件
return RET_OK;
}
两程序实现效果相同。
点击窗口中确定按键后,弹出对话框。
选择Yes,则关闭整个窗口
选择No,则关闭对话框
执行过程:
1.生成资源:sh assets_gen.sh
,添加新资源故需重新生成资源。【因为此处并不使用.data文件,故可省略】
2.编译:scons
3.执行:./bin/demo