mre下的控件实现(四、Button使用)

这里有一个实例使用(一、实现)中的按钮控件,达到的效果如下图所示,

用mre sdk(这里使用的是mre sdk 2.5版本)创建一个工程,工程名为mtid(实际上这个是我要在做一个工具)。

//生成的工程,自动生成mtid.c,代码改动成如下
//主窗体在这里创建
#include "vmsys.h"
#include "vmio.h"
#include "vmgraph.h"
#include "vmchset.h"
#include "vmstdlib.h"
#include "ResID.h"
#include "vm4res.h"
#include "vmlog.h"

#include "MtidView.h"
#include "widget.h"

VMINT        layer_hdl[1];
VMINT        SCREEN_HEIGHT;
VMINT        SCREEN_WIDTH;

Widget* g_mtid_view;

void handle_sysevt(VMINT message, VMINT param);
void handle_keyevt(VMINT event, VMINT keycode);
void handle_penevt(VMINT event, VMINT x, VMINT y);
void handle_exitevt(void* ctx);

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);
    vm_res_init();
}

void handle_sysevt(VMINT message, VMINT param) 
{
    switch (message) 
    {
    case VM_MSG_CREATE:
        vm_log_info("handle_sysevt VM_MSG_CREATE");
        if(layer_hdl[0] == -1)
        {
            SCREEN_HEIGHT = vm_graphic_get_screen_height();
            SCREEN_WIDTH = vm_graphic_get_screen_width();
            layer_hdl[0] = vm_graphic_create_layer(0, 0, vm_graphic_get_screen_width(),vm_graphic_get_screen_height(),-1);
            vm_graphic_set_clip(0, 0, vm_graphic_get_screen_width(), vm_graphic_get_screen_height());
        }
        g_mtid_view = MtidViewInit(layer_hdl, handle_exitevt, NULL);
        break;
    case VM_MSG_ACTIVE:        
        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);
            vm_graphic_set_clip(0, 0, vm_graphic_get_screen_width(), vm_graphic_get_screen_height());
            widget_on_paint(g_mtid_view);
        }
        break;
        
    case VM_MSG_PAINT:
        vm_log_info("handle_sysevt VM_MSG_PAINT");
        widget_on_paint(g_mtid_view);
        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:
        handle_exitevt(NULL);
        break;    
    }
}

void handle_keyevt(VMINT event, VMINT keycode) 
{
    if( layer_hdl[0] != -1 )
    {
        vm_graphic_delete_layer(layer_hdl[0]);
        layer_hdl[0] = -1;
    }
    
    vm_exit_app();        /* quit application */
}

void handle_penevt(VMINT event, VMINT x, VMINT y)
{
    widget_on_pen_evt(g_mtid_view, event, x, y);
}

void handle_exitevt(void* ctx)
{
    if( layer_hdl[0] != -1 )
    {
        vm_graphic_delete_layer(layer_hdl[0]);
        layer_hdl[0] = -1;
    }
    if( layer_hdl[1] != -1 )
    {
        vm_graphic_delete_layer(layer_hdl[0]);
        layer_hdl[1] = -1;
    }
    widget_on_destroy(g_mtid_view);
    vm_res_deinit();
    vm_exit_app();
} 
//主窗体 MtidView.h
#ifndef __MTID_VIEW_H__
#define __MTID_VIEW_H__

#include "typedef.h"

struct _Widget;
typedef struct _Widget Widget;

extern VMINT    SCREEN_HEIGHT;
extern VMINT    SCREEN_WIDTH;

typedef void (*ExitCbFunc)(void* ctx);

Widget* MtidViewInit(int* layer, ExitCbFunc exit, void* ctx);
void MtidViewOnPaint(Widget* thiz);
void MtidViewOnPenEvt(Widget* thiz, VMINT event, VMINT x, VMINT y);
void MtidViewOnKeyEvt(Widget* thiz, VMINT event, int keycode);

#endif /*__MTID_VIEW_H__*/ 
//主窗体接口实现,在这里拼装子控件
#include "button.h"
#include "widget.h"
#include "MtidView.h"

#define MTID_NUMBER 15
#define MTID_FILE_PATH "E:\\mre\\mtid.txt"

typedef struct _MtidViewExtrat
{
    char write_buf[MTID_NUMBER + 1];
    char mtid[MTID_NUMBER + 1];
    ExitCbFunc exit;
    void* ctx;
} MtidViewExtra;

static void MtidViewNumBtnOnClicked(void* param1, void* param2)
{
    Widget* thiz = (Widget*)param2;
    MtidViewExtra* extra = (MtidViewExtra*)thiz->extra;

    char tmp[MTID_NUMBER + 1] = {0};
    int len = 0;
    int i = 0;

    memset(extra->write_buf, 0, sizeof(extra->write_buf));
    sprintf(tmp, "%d", (int)param1);
    len = strlen(extra->mtid);
    if(len < 15)
    {
        int i = 0;
        strcat(extra->mtid, tmp);
    }

    len = strlen(extra->mtid);
    for(i = 0; i < MTID_NUMBER - len; i++)
    {
        extra->write_buf[i] = '0';
    }
    strcat(extra->write_buf, extra->mtid);
    
    MtidViewOnPaint(thiz);
}

static void MtidViewSureBtnOnClicked(void* param1, void* param2)
{
    Widget* thiz = (Widget*)param2;
    MtidViewExtra* extra = (MtidViewExtra*)thiz->extra;
    int* layer = widget_get_layer(thiz);
    
    wbh_write(MTID_FILE_PATH, "w", extra->write_buf);

    if(NULL != extra->exit)
        extra->exit(extra->ctx);
}

static void MtidViewClearBtnOnClicked(void* param1, void* param2)
{
    Widget* thiz = (Widget*)param2;
    MtidViewExtra* extra = (MtidViewExtra*)thiz->extra;
    int len = 0;
    int i = 0;

    memset(extra->mtid, 0, sizeof(extra->mtid));
    len = strlen(extra->mtid);
    for(i = 0; i < MTID_NUMBER - len; i++)
    {
        extra->write_buf[i] = '0';
    }
    strcat(extra->write_buf, extra->mtid);
    MtidViewOnPaint(thiz);
}

static void MtidViewAddNumBtn(Widget* thiz)
{
    Rect rect = {0};
    Widget* btn = NULL;
    int btn_area_x = 0;
    int btn_area_y = SCREEN_HEIGHT/5;
    int btn_h = (SCREEN_HEIGHT*4/5)/4;
    int btn_w = SCREEN_WIDTH/3;
    int i = 0;

    int* layer = widget_get_layer(thiz);

    for(i = 1; i <= 10; i++)
    {
        char text[16] = {0};
        sprintf(text, "%d", i%10);
        
        rect.x_pos = btn_area_x + ((i-1)%3)*btn_w;
        rect.y_pos = btn_area_y + (i-1)/3*btn_h;
        rect.width = btn_w;
        rect.height = btn_h;
        btn = button_create(100 + i, rect, layer, text);

        vm_log_info("MtidViewAddNumBtn btn:%d", btn);
        button_set_on_clicked_func(btn, MtidViewNumBtnOnClicked, (void*)(i%10), (void*)thiz);
        widget_add_child(thiz, btn);
    }

    rect.x_pos = btn_area_x + btn_w;
    rect.y_pos = btn_area_y + 3*btn_h;
    rect.height = btn_h;
    rect.width = btn_w;
    btn = button_create(100 + 11, rect, layer, "清空");
    button_set_on_clicked_func(btn, MtidViewClearBtnOnClicked, NULL, (void*)thiz);
    widget_add_child(thiz, btn);

    rect.x_pos = btn_area_x + btn_w * 2;
    rect.y_pos = btn_area_y + 3*btn_h;
    rect.height = btn_h;
    rect.width = btn_w;
    btn = button_create(100 + 12, rect, layer, "确定");
    button_set_on_clicked_func(btn, MtidViewSureBtnOnClicked, NULL, (void*)thiz);
    widget_add_child(thiz, btn);
}

void MtidViewOnPaint(Widget* thiz)
{
    int i = 0;
    MtidViewExtra* extra = (MtidViewExtra*)thiz->extra;
    VMWCHAR w_str[32] = {0};

    VMINT wstr_width, wstr_height;
    VMINT x_pos = widget_get_x_pos_abs(thiz);
    VMINT y_pos = widget_get_y_pos_abs(thiz);
    VMINT height = widget_get_height(thiz);
    VMINT width = widget_get_width(thiz);
    int* layer = widget_get_layer(thiz);
    int layer_hd = layer[widget_get_layer_index(thiz)];
    vm_graphic_color start_color;
    vm_graphic_color end_color;    
    vm_graphic_color color;

    color.vm_color_565 = VM_COLOR_WHITE;
    vm_graphic_setcolor(&color);
    vm_graphic_fill_rect_ex(layer_hd, 0, 0, width, height);

    end_color.vm_color_565 = VM_COLOR_888_TO_565(217, 210, 168);
    start_color.vm_color_565 = VM_COLOR_888_TO_565(217, 210, 168);
    vm_graphic_gradient_paint_rect(layer_hd, x_pos + 2, y_pos + 2,     x_pos + width - 4, y_pos + height/5 - 4, start_color, end_color, VM_GDI_GP_VER);

    vm_graphic_setcolor(&color);
    vm_graphic_roundrect_ex(layer_hd, x_pos+1, y_pos+1, width-2, height/5-2, 5);

    color.vm_color_565 = VM_COLOR_BLACK;
    vm_graphic_setcolor(&color);
    vm_gb2312_to_ucs2(w_str, sizeof(w_str), extra->write_buf);
    vm_graphic_set_font(VM_LARGE_FONT);
    wstr_width = vm_graphic_get_string_width(w_str);
    wstr_height = vm_graphic_get_string_height(w_str);
    vm_graphic_textout_to_layer(layer_hd, x_pos + width - wstr_width - 8,  y_pos + height/5 - wstr_height - 8,  w_str,  vm_wstrlen(w_str));

    for(i = 0; i < MAX_WIDGET_CHILD; i++)
    {
        if(NULL != thiz->child[i])
        {
            widget_on_paint(thiz->child[i]);    
        }
    }
    vm_graphic_flush_layer(layer, 1);
}

void MtidViewOnPenEvt(Widget* thiz, VMINT event, VMINT x, VMINT y)
{
    int i = 0;
    for(i = 0; i < MAX_WIDGET_CHILD; i++)
    {
        vm_log_info("MtidViewOnPenEvt thiz->child[%d]:%d", i, thiz->child[i]);
        if(NULL != thiz->child[i])
        {
            widget_on_pen_evt(thiz->child[i], event, x, y);    
        }
    }
    vm_graphic_flush_screen();
}

void MtidViewOnKeyEvt(Widget* thiz, int event, int keycode)
{
}

void MtidViewOnDestroy(Widget* thiz)
{
    int i = 0;
    for(i = 0; i < MAX_WIDGET_CHILD; i++)
    {
        if(NULL != thiz->child[i])
        {
            widget_on_destroy(thiz->child[i]);    
            thiz->child[i] = NULL;
        }
    }
    if(thiz->extra)
        WBH_FREE(thiz->extra);

    widget_deinit(thiz);
    WBH_FREE(thiz);
}

Widget* MtidViewInit(int* layer, ExitCbFunc exit, void* ctx)
{
    Widget* thiz = NULL;
    Rect rect = {0};
    MtidViewExtra* extra = NULL;
    int i =0, len =0;
    
    thiz = WBH_MALLOC(sizeof(Widget));
    extra = WBH_MALLOC(sizeof(MtidViewExtra));

    thiz->on_destroy = (OnDestroyFunc)MtidViewOnDestroy;
    thiz->on_paint = (OnPaintFunc)MtidViewOnPaint;
    thiz->on_pen_event = (OnPenEventFunc)MtidViewOnPenEvt;
    thiz->on_key_event = (OnKeyEventFunc)MtidViewOnKeyEvt;
    extra->exit = exit;
    extra->ctx = ctx;
    thiz->extra = (void*)extra;

    rect.x_pos= 0;
    rect.y_pos = 0;
    rect.width = SCREEN_WIDTH;
    rect.height = SCREEN_HEIGHT;
    widget_init(thiz, CONTROL_TYPE_VIEW, 100, rect, layer);
    MtidViewAddNumBtn(thiz);

    len = strlen(extra->mtid);
    for(i = 0; i < MTID_NUMBER - len; i++)
    {
        extra->write_buf[i] = '0';
    }

    strcat(extra->write_buf, extra->mtid);
    return thiz;
}









posted @ 2012-08-12 11:52  $逝水无痕$  阅读(262)  评论(0编辑  收藏  举报