实现效果如图:
一.先设置按钮大小
如:BUTTON_Create(20, 40, 53, 53, ID_SYSTEM, BUTTON_CF_SHOW );
此处新建了一个53*53的正方形按钮(即上面第一个按钮),坐标x:20,y:40, 按钮的ID自定义为ID_SYSTEM, BUTTON_CF_SHOW默认即可。
二.把图片转化为c文件
如果需要皮肤效果的话,在按钮的长宽的基础上下左右减3正合适,此处选取了一张47*47的图标,打开软件,file导入
选择调色板模式,选择后有一个弹出框提示你,没关系,确认。
可以有各种色彩转换格式供选择,但默认选择Best palette即可,Best palette + transparency为带透明效果。
File,另存为,再选择保存类型为c文件,保存
选择保存为565格式的,点击OK就完成图片格式转化了。
如图,这是我转化的一些图片文件,先把文件复制到你的工程本地路径里,再在keil里面导入这些文件。图片文件不必做任何修改就能使用。
三.程序相关设置
1.声明外部文件:extern const GUI_BITMAP bmmusic;
说明:music 为你保存时,c文件的名称,则图片转换器转换的图片数据名默认为bmmusic。若图片名字为system,则此处为bmsystem。
2.设置图片背景:BUTTON_SetBitmapEx(ahButton[1],BUTTON_BI_UNPRESSED, &bmmusic,3,3);
说明:ahButton[1]为按钮句柄,BUTTON_BI_UNPRESSED默认属性,&bmmusic为按钮的背景图片,即上面引用的外部文件名,3,3此处为图片距按钮内边缘左边和上面的边距,默认为0.
附:效果图完整的代码如下,供参考。
MY_UI.h
#ifndef __GUI_APP_H
#define __GUI_APP_H
#include "includes.h"
static void _cbBkWin(WM_MESSAGE * pMsg);
void Main_Task(void);
#endif
MY_UI.c
#include "includes.h"
#include "bsp.h"
#include "GUI.h"
#include "DIALOG.h"
#include "WM.h"
#include "BUTTON.h"
#include "CHECKBOX.h"
#include "DROPDOWN.h"
#include "EDIT.h"
#include "FRAMEWIN.h"
#include "LISTBOX.h"
#include "MULTIEDIT.h"
#include "RADIO.h"
#include "SLIDER.h"
#include "TEXT.h"
#include "PROGBAR.h"
#include "SCROLLBAR.h"
#include "LISTVIEW.h"
/******************************GUI 按钮 ID**************************************/
#define ID_SYSTEM (GUI_ID_USER + 0)
#define ID_MUSIC (GUI_ID_USER + 1)
#define ID_ICCARD (GUI_ID_USER + 2)
#define ID_RADIO (GUI_ID_USER + 3)
#define ID_FILE (GUI_ID_USER + 4)
#define ID_PHONE (GUI_ID_USER + 9)
#define ID_MESSAGE (GUI_ID_USER + 10)
#define ID_SETTING (GUI_ID_USER + 11)
#define ID_BEGIN (GUI_ID_USER + 12)
#define ID_PROBAR (GUI_ID_USER + 13)
#define ID_TIME (GUI_ID_USER + 14)
#define ID_HEADER0 (GUI_ID_USER + 20)
#define ID_HEADER1 (GUI_ID_USER + 21)
#define ID_HEADER2 (GUI_ID_USER + 22)
/***************************全局变量********************************************/
int T; //时间设置窗口中手动改变值之后的程序区分标志,1手动设置了时间,0未设置
//static int K; //RTC初始化成功标志
static int i=0,Return_start,Return_time;
extern OS_TCB System_TCB; //定义任务控制块
extern OS_TCB Setting_TCB; //定义任务控制块
extern OS_TCB File_TCB; //定义任务控制块
extern OS_TCB Taskmgr_TCB; //定义任务控制块
extern CPU_STK System_Stk[TASK_SYSTEM_STK_SIZE]; //定义任务堆栈
extern CPU_STK Setting_Stk[TASK_SETTING_STK_SIZE]; //定义任务堆栈
extern CPU_STK File_Stk[TASK_FILE_STK_SIZE]; //定义任务堆栈
extern CPU_STK Taskmgr_Stk[TASK_TASKMGR_STK_SIZE]; //定义任务堆栈
static WM_HWIN aheader_Time;
extern WM_HWIN hWin_START; //窗口句柄
extern WM_HWIN hWin_TIME; //窗口句柄
WM_HWIN CreateSTART(void);
WM_HWIN CreateTime(void);
WM_HWIN CreateSYSTEM(void);
WM_HWIN CreateSETTING(void);
WM_HWIN CreateFile(void);
WM_HWIN CreateTAKMGR(void);
void RTC_Time(void); //实时时钟函数
/**********************************外部声明*************************************/
extern const GUI_BITMAP bmWinbk_1;
extern const GUI_BITMAP bmcao;
extern const GUI_BITMAP bmmusic;
extern const GUI_BITMAP bmphone;
extern const GUI_BITMAP bmmessage;
extern const GUI_BITMAP bmsetting;
extern const GUI_BITMAP bmfile;
extern const GUI_BITMAP bmICcard;
extern const GUI_BITMAP bmsystem;
extern const GUI_BITMAP bmradio;
//const GUI_FONT * pFont = &GUI_Font16_1;
//桌面回调
static void _cbBkWin(WM_MESSAGE * pMsg)
{
OS_ERR err;
switch (pMsg->MsgId)
{
case WM_PAINT:
GUI_SetBkColor(GUI_DARKCYAN);
GUI_Clear();
GUI_DrawBitmap(&bmWinbk_1,0,0); //主界面壁纸,占用flash约150k!
break;
case WM_NOTIFY_PARENT:
if(pMsg->Data.v==WM_NOTIFICATION_RELEASED)
{
int Id=WM_GetId(pMsg->hWinSrc);
switch (Id)
{
case ID_HEADER0:
//创建开始菜单
if(i==0) Return_start=CreateSTART(); //创建子对话框
if(i==1&&Return_start==hWin_START) GUI_EndDialog(hWin_START,0); //关闭子对话框
i++;
if(i==2)i=0;
break;
case ID_HEADER1:
OSTaskCreate((OS_TCB *)&Taskmgr_TCB, //任务控制块指针
(CPU_CHAR *)"Taskmgr", //任务名称
(OS_TASK_PTR )Task_Taskmgr, //任务代码指针
(void *)0, //传递给任务的参数parg
(OS_PRIO )TASK_TASKMGR_PRIO, //任务优先级
(CPU_STK *)&Taskmgr_Stk[0], //任务堆栈基地址
(CPU_STK_SIZE)TASK_TASKMGR_STK_SIZE/10,//堆栈剩余警戒线
(CPU_STK_SIZE)TASK_TASKMGR_STK_SIZE, //堆栈大小
(OS_MSG_QTY )0, //可接收的最大消息队列数
(OS_TICK )0, //时间片轮转时间
(void *)0, //任务控制块扩展信息
(OS_OPT )(OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR), //任务选项
(OS_ERR *)&err); //返回值
break;
case ID_HEADER2:
//创建时间设置窗口
if(i==0) Return_time=CreateTime(); //创建子对话框
if(i==1&&Return_time==hWin_TIME) GUI_EndDialog(hWin_TIME,0); //关闭子对话框
i++;
if(i==2)i=0;
break;
case ID_SYSTEM:
OSTaskCreate((OS_TCB *)&System_TCB, //任务控制块指针
(CPU_CHAR *)"My_sys", //任务名称
(OS_TASK_PTR )Task_System, //任务代码指针
(void *)0, //传递给任务的参数parg
(OS_PRIO )TASK_SYSTEM_PRIO, //任务优先级
(CPU_STK *)&System_Stk[0], //任务堆栈基地址
(CPU_STK_SIZE)TASK_SYSTEM_STK_SIZE/10,//堆栈剩余警戒线
(CPU_STK_SIZE)TASK_SYSTEM_STK_SIZE, //堆栈大小
(OS_MSG_QTY )0, //可接收的最大消息队列数
(OS_TICK )0, //时间片轮转时间
(void *)0, //任务控制块扩展信息
(OS_OPT )(OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR), //任务选项
(OS_ERR *)&err); //返回值
break;
case ID_FILE:
OSTaskCreate((OS_TCB *)&File_TCB, //任务控制块指针
(CPU_CHAR *)"FileMgr", //任务名称
(OS_TASK_PTR )Task_File, //任务代码指针
(void *)0, //传递给任务的参数parg
(OS_PRIO )TASK_FILE_PRIO, //任务优先级
(CPU_STK *)&File_Stk[0], //任务堆栈基地址
(CPU_STK_SIZE)TASK_FILE_STK_SIZE/10,//堆栈剩余警戒线
(CPU_STK_SIZE)TASK_FILE_STK_SIZE, //堆栈大小
(OS_MSG_QTY )0, //可接收的最大消息队列数
(OS_TICK )0, //时间片轮转时间
(void *)0, //任务控制块扩展信息
(OS_OPT )(OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR), //任务选项
(OS_ERR *)&err); //返回值
break;
case ID_SETTING:
OSTaskCreate((OS_TCB *)&Setting_TCB, //任务控制块指针
(CPU_CHAR *)"Setting", //任务名称
(OS_TASK_PTR )Task_Setting, //任务代码指针
(void *)0, //传递给任务的参数parg
(OS_PRIO )TASK_SETTING_PRIO, //任务优先级
(CPU_STK *)&Setting_Stk[0], //任务堆栈基地址
(CPU_STK_SIZE)TASK_SETTING_STK_SIZE/10,//堆栈剩余警戒线
(CPU_STK_SIZE)TASK_SETTING_STK_SIZE, //堆栈大小
(OS_MSG_QTY )0, //可接收的最大消息队列数
(OS_TICK )0, //时间片轮转时间
(void *)0, //任务控制块扩展信息
(OS_OPT )(OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR), //任务选项
(OS_ERR *)&err); //返回值
break;
}
}
case WM_TOUCH:
break;
case WM_DELETE:
break;
default:
WM_DefaultProc(pMsg);
}
}
void Main_Task(void)
{
WM_HWIN aheader[2];
BUTTON_Handle ahButton[12]={0};
PROGBAR_Handle arProgbar[1]={0};
// TEXT_Handle ahText[10]={0};
WM_SetCallback(WM_HBKWIN,&_cbBkWin); //主界面背景图
WM_SetCreateFlags(WM_CF_MEMDEV);
WM_EnableMemdev(WM_HBKWIN);
/*******************************系统全局设置***************************************/
//默认皮肤设置
BUTTON_SetDefaultSkin(BUTTON_SKIN_FLEX); // Sets the default skin for new widgets
PROGBAR_SetDefaultSkin(PROGBAR_SKIN_FLEX);
FRAMEWIN_SetDefaultSkin(FRAMEWIN_SKIN_FLEX);
MULTIPAGE_SetDefaultSkin(MULTIPAGE_SKIN_FLEX);
CHECKBOX_SetDefaultSkin(CHECKBOX_SKIN_FLEX);
DROPDOWN_SetDefaultSkin(DROPDOWN_SKIN_FLEX);
HEADER_SetDefaultSkin(HEADER_SKIN_FLEX);
MENU_SetDefaultSkin(MENU_SKIN_FLEX);
RADIO_SetDefaultSkin(RADIO_SKIN_FLEX);
SCROLLBAR_SetDefaultSkin(SCROLLBAR_SKIN_FLEX);
SLIDER_SetDefaultSkin(SLIDER_SKIN_FLEX);
SPINBOX_SetDefaultSkin(SPINBOX_SKIN_FLEX);
//默认对齐方式
//MULTIPAGE_SetDefaultAlign(unsigned Align);
LISTBOX_SetDefaultTextAlign(GUI_TA_HCENTER | GUI_TA_VCENTER);
//默认颜色设置
//FRAMEWIN_SetDefaultBarColor(0,GUI_YELLOW); //对话框标题,不起作用
//FRAMEWIN_SetDefaultTextColor(1,GUI_DARKGRAY); //标题栏文字
FRAMEWIN_SetDefaultClientColor(GUI_YELLOW); //对话框背景
WINDOW_SetDefaultBkColor(GUI_DARKCYAN); //WINDOW背景
LISTBOX_SetDefaultBkColor(LISTBOX_CI_SELFOCUS,GUI_DARKCYAN); //列表框背景
LISTBOX_SetDefaultBkColor(LISTBOX_CI_SEL,GUI_DARKCYAN);
LISTBOX_SetDefaultTextColor(LISTBOX_CI_SELFOCUS,GUI_YELLOW); //列表框文字
LISTBOX_SetDefaultTextColor(LISTBOX_CI_SEL,GUI_YELLOW);
/************************************************************************************/
//主界面状态栏
aheader[0] = HEADER_CreateEx(0,0,21,20,WM_HBKWIN,WM_CF_SHOW,0,ID_HEADER0);
HEADER_AddItem(aheader[0], 22, "T", TEXT_CF_HCENTER|TEXT_CF_VCENTER);
//HEADER_SetBMPEx(aheader[0], 0,&bmhome_u,0,0);
aheader[1] = HEADER_CreateEx(148,0,35,20,WM_HBKWIN,WM_CF_SHOW,0,ID_HEADER1);
HEADER_AddItem(aheader[1], 36, "CPU", TEXT_CF_HCENTER|TEXT_CF_VCENTER);
//HEADER_SetBMP(aheader[1],0,&bmhome_u);
aheader_Time = HEADER_CreateEx(184,0,55,20,WM_HBKWIN,WM_CF_SHOW,0,ID_HEADER2);
HEADER_AddItem(aheader_Time, 56, NULL, TEXT_CF_HCENTER|TEXT_CF_VCENTER);
//主界面按钮创建
ahButton[0] = BUTTON_Create(20, 40, 53, 53, ID_SYSTEM, BUTTON_CF_SHOW );
ahButton[1] = BUTTON_Create(93, 40,53, 53, ID_MUSIC, BUTTON_CF_SHOW );
ahButton[2] = BUTTON_Create(166, 40,53, 53, ID_ICCARD, BUTTON_CF_SHOW );
ahButton[3] = BUTTON_Create(20, 113, 53, 53, ID_RADIO, BUTTON_CF_SHOW );
ahButton[4] = BUTTON_Create(93, 113, 53, 53, ID_FILE, BUTTON_CF_SHOW );
ahButton[9] = BUTTON_Create(20, 247, 53, 53, ID_PHONE, BUTTON_CF_SHOW );
ahButton[10] = BUTTON_Create(93, 247, 53, 53, ID_MESSAGE, BUTTON_CF_SHOW );
ahButton[11] = BUTTON_Create(166, 247, 53, 53, ID_SETTING, BUTTON_CF_SHOW );
arProgbar[0] = PROGBAR_Create(22,0,125,20, WM_CF_SHOW);
//设置图标背景图
BUTTON_SetBitmapEx(ahButton[0],BUTTON_BI_UNPRESSED, &bmsystem,3,3);
BUTTON_SetBitmapEx(ahButton[1],BUTTON_BI_UNPRESSED, &bmmusic,3,3);
BUTTON_SetBitmapEx(ahButton[2],BUTTON_BI_UNPRESSED,&bmICcard,3,3);
BUTTON_SetBitmapEx(ahButton[3],BUTTON_BI_UNPRESSED,&bmradio,3,3);
BUTTON_SetBitmapEx(ahButton[4],BUTTON_BI_UNPRESSED,&bmfile,3,3);
BUTTON_SetBitmapEx(ahButton[9],BUTTON_BI_UNPRESSED, &bmphone,3,3);
BUTTON_SetBitmapEx(ahButton[10],BUTTON_BI_UNPRESSED,&bmmessage,3,3);
BUTTON_SetBitmapEx(ahButton[11],BUTTON_BI_UNPRESSED,&bmsetting,3,3);
PROGBAR_SetBarColor(arProgbar[0],0,0xff0000);
PROGBAR_SetBarColor(arProgbar[0],1,0x00c000);
//PROGBAR_SetValue(arProgbar[0],30);
PROGBAR_SetMinMax(arProgbar[0], 0, 100);
/*
T=0;
if(RTC_Init()!=1)K=1; //RTC初始化 ,一定要初始化成功
else K=0;
*/
while(1)
{
PROGBAR_SetValue(arProgbar[0], OSStatTaskCPUUsage); //直接调用OSStatTaskCPUUsage,即CPU占用率的值
RTC_Time();
GUI_Delay(100);
}
}
//实时时钟函数
void RTC_Time(void)
{
OS_ERR err;
u8 t=0;
char hour[2],min[2];
if(RTC_Init()!=1) //RTC初始化成功才执行
{
if(t!=calendar.sec)
{
t=calendar.sec;
//LCD_ShowNum(60,130,calendar.w_year,4,16);
//LCD_ShowNum(100,130,calendar.w_month,2,16);
//LCD_ShowNum(124,130,calendar.w_date,2,16);
/*
switch(calendar.week)
{
case 0:
LCD_ShowString(60,148,200,16,16,"Sunday ");
break;
case 1:
LCD_ShowString(60,148,200,16,16,"Monday ");
break;
case 2:
LCD_ShowString(60,148,200,16,16,"Tuesday ");
break;
case 3:
LCD_ShowString(60,148,200,16,16,"Wednesday");
break;
case 4:
LCD_ShowString(60,148,200,16,16,"Thursday ");
break;
case 5:
LCD_ShowString(60,148,200,16,16,"Friday ");
break;
case 6:
LCD_ShowString(60,148,200,16,16,"Saturday ");
break;
}
*/
sprintf(hour, " %d" , calendar.hour);
sprintf(min, " %d" , calendar.min);
//sprintf(sec, " %d" , calendar.sec);
strcat(hour," :");
strcat(hour,min);
//TEXT_SetText(hText_Time,hour);
HEADER_SetItemText(aheader_Time,0,hour);
}
OSTimeDlyHMSM(0, 0,0,100,OS_OPT_TIME_HMSM_STRICT,&err);
}
}