这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 活动中心 » 合作大赛 » 分析VRE的demo程序hello world(转)

共2条 1/1 1 跳转至

分析VRE的demo程序hello world(转)

院士
2010-09-03 09:54:35     打赏
分析VRE的demo程序hello world
Main函数中调用vm_reg_sysevt_callback注册系统回调函数
查API可知,函数原型如下:(须包括vmsys.h头文件)
void vm_reg_sysevt_callback(
void (*f)(VMINT message, VMINT param)
);
总共注册了三个函数:
handle_sysevt 系统事件
handle_keyevt 按键事件
handle_penevt 画笔事件
然后分别是对三个函数的实现,用SWITCH/CASE实现,
比如系统事件可能发生VM_MSG_CREATE,VM_MSG_PAINT消息等等
以下是hello程序源代码,详细注释见代码内
#include "vmsys.h"
#include "vmio.h"
#include "vmgraph.h"
#include "vmchset.h"
#include "vmstdlib.h"


#define SUPPORT_BG
//#pragma comment(lib,"kernel32.lib");
VMINT layer_hdl[1]; // layer handle array.层句柄数组

/* ---------------------------------------------------------------------------
* local variables 本地变量
* --------------------------------------------------------------------
void handle_sysevt(VMINT message, VMINT param);
void handle_keyevt(VMINT event, VMINT keycode);
void handle_penevt(VMINT event, VMINT x, VMINT y);
static void draw_hello(void);
void vm_main(void)
{
layer_hdl[0] = -1;

vm_reg_sysevt_callback(handle_sysevt);//注册三个回调函数
vm_reg_keyboard_callback(handle_keyevt);
vm_reg_pen_callback(handle_penevt);
}

void handle_sysevt(VMINT message, VMINT param) //函数的实现
{
#ifdef SUPPORT_BG
/* The application updates the screen when receiving the message VM_MSG_PAINT
* what is sent after the application is activated. 当应用程序激活后会产生该消息
The application can skip the process on screen when the VM_MSG_ACTIVE or VM_MSG_INACTIVE is received.当收到ACTIVE或INACTIVE消息,应用程序忽略该过程(指屏幕的刷新动作)
*/
switch (message)
{
case VM_MSG_CREATE:
/* the GDI operation is not recommended as the response of the message*/
break;
case VM_MSG_PAINT:
/* cerate base layer that has same size as the screen*/
/*If rotating screen is needed, and it rotates successful,
the application will receive the VM_MSG_PAINT event, then,
the application needs to delete the level and recreate it. */
if( layer_hdl[0] != -1 )
{
vm_graphic_delete_layer(layer_hdl[0]);
layer_hdl[0] = -1;
}

if( layer_hdl[0] == -1 )
{
layer_hdl[0] = vm_graphic_create_layer(0, 0,
vm_graphic_get_screen_width(),
vm_graphic_get_screen_height(), -1);//-1表示透明颜色
}

/* set clip area */
//设置剪裁区,GDI操作只有在剪裁区内才有效
vm_graphic_set_clip(0, 0,
vm_graphic_get_screen_width(),
vm_graphic_get_screen_height());

draw_hello();
break;
case VM_MSG_SCREEN_ROTATE:
/*The application can rotate the screen*/
break;
case VM_MSG_HIDE:
if( layer_hdl[0] != -1 )
{
vm_graphic_delete_layer(layer_hdl[0]);
layer_hdl[0] = -1;
}
break;
case VM_MSG_QUIT:
if( layer_hdl[0] != -1 )
{
vm_graphic_delete_layer(layer_hdl[0]);
layer_hdl[0] = -1;
}
vm_exit_app();
break;
}
#else//上面表示在调试模式下怎么处理
switch (message)
{
case VM_MSG_CREATE:
case VM_MSG_ACTIVE:
/*cerate base layer that has same size as the screen*/
if( layer_hdl[0] == -1 )
{
layer_hdl[0] = vm_graphic_create_layer(0, 0,
vm_graphic_get_screen_width(),
vm_graphic_get_screen_height(), -1);
}

/* set clip area*/
vm_graphic_set_clip(0, 0,
vm_graphic_get_screen_width(),
vm_graphic_get_screen_height());
break;

/*If rotating screen is needed, and it rotates successful,
the application will receive the VM_MSG_PAINT event, then,
the application needs to delete the level and recreate it. */
case VM_MSG_PAINT:
draw_hello();
break;
case VM_MSG_SCREEN_ROTATE:
/*The application can rotate the screen*/
break;
case VM_MSG_INACTIVE:
if( layer_hdl[0] != -1 )
{
vm_graphic_delete_layer(layer_hdl[0]);
layer_hdl[0] = -1;
}
break;
case VM_MSG_QUIT:
if( layer_hdl[0] != -1 )
{
vm_graphic_delete_layer(layer_hdl[0]);
layer_hdl[0] = -1;
}
vm_exit_app();
break;
}
#endif
}

void handle_keyevt(VMINT event, VMINT keycode)
{
/* press any key and return*/
if( layer_hdl[0] != -1 )
{
vm_graphic_delete_layer(layer_hdl[0]);
layer_hdl[0] = -1;
}

vm_exit_app();
}

void handle_penevt(VMINT event, VMINT x, VMINT y)
{
/* touch and return*/
if( layer_hdl[0] != -1 )
{
vm_graphic_delete_layer(layer_hdl[0]);
layer_hdl[0] = -1;
}

vm_exit_app();
}


static void draw_hello(void) //怎么输出hello
{
VMWCHAR s[50];
VMUINT8* buf;
int x, y, w;

vm_gb2312_to_ucs2(s, 50, "Hello, world!");
w = vm_graphic_get_string_width(s);
x = (vm_graphic_get_screen_width() - w) / 2;
y=(vm_graphic_get_screen_height()-
m_graphic_get_character_height()) / 2;
//获取X,Y位置,这里是让它居中显示

/* get the target buffer*/
buf = vm_graphic_get_layer_buffer(layer_hdl[0]);
//获取第0层缓冲区指针,以便在上面操作(画图,写字等)
/* fill the screen*/
vm_graphic_fill_rect(buf, 0, 0, vm_graphic_get_screen_width(), vm_graphic_get_screen_height(),VM_COLOR_WHITE,VM_COLOR_BLACK);
//最后两个参数,白色为线条颜色,黑色为填充区颜色
/* draw text */
vm_graphic_textout(buf, x, y, s, wstrlen(s), VM_COLOR_BLUE);
//显示文字到该层的缓冲区,默认字体为VM_MEDIUM_FONT
/* flush the screen with data in the buffer*/
vm_graphic_flush_layer(layer_hdl, 1);
//调用该API,LCD屏才会刷新
}
注:关于那个UCS2编码,大家可以参考这篇文档http://www.docin.com/p-76591675.html
Universal Character Set UCS是所有其他字符集标准的一个超集。它保证与其他字符集是双向兼容的,就是说,如果你将任何文本字符串翻译到UCS格式,然后再翻译回原编码,你不会丢失任何信息。


转自VRE论坛http://vre.net.ru



关键词: 分析     程序     hello     world     VMINT     h    

菜鸟
2010-09-08 23:09:04     打赏
2楼

谢谢分享!


共2条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]