40.3 官方WIDGET_PhoneButton实例 这个例子主要是在按钮上面显示图片,演示一种简单的来电话情景。这个例子所在模拟器中的位置:
源程序如下:
- #include "GUI.h"
- #include "BUTTON.h"
-
- /*******************************************************************
- *
- * static variables
- *
- ********************************************************************
- */
- /*******************************************************************
- *
- * Bitmap data, 3 phone logos
- */
- static const GUI_COLOR Colors[] = { 0x000000, 0xFFFFFF };
-
- static const GUI_LOGPALETTE Palette = { 2, 1, Colors };(1)
-
- static const unsigned char acPhone0[] = {
- ________, ________, ________, ________,
- ________, ________, ________, ________,
- ________, ________, ________, ________,
- ________, ________, ________, ________,
- ________, ________, ________, ________,
- ________, ________, ________, ________,
- ________, ________, ________, ________,
- ________, ________, ________, ________,
- ________, ________, ________, ________,
- ________, ________, ________, ________,
- ________, ________, ________, ________,
- _____XXX, XXXXXXXX, XXXXXXXX, XXX_____,
- ___XXXXX, XXXXXXXX, XXXXXXXX, XXXXX___,
- __XXXXXX, XXXXXXXX, XXXXXXXX, XXXXXX__,
- _XXXXXXX, X_______, _______X, XXXXXXX_,
- _XXXXXXX, X__XX___, ___XX__X, XXXXXXX_,
- _XXXXXXX, X__XX___, ___XX__X, XXXXXXX_,
- _XXXXXXX, X__XX___, ___XX__X, XXXXXXX_,
- ________, ___XX___, ___XX___, ________,
- _______X, XXXXXXXX, XXXXXXXX, X_______,
- ______XX, XXXXXXXX, XXXXXXXX, XX______,
- _____XXX, XXXX__X_, _X__XXXX, XXX_____,
- ____XXXX, XXXX__X_, _X__XXXX, XXXX____,
- ___XXXXX, XXXXXXXX, XXXXXXXX, XXXXX___,
- ___XXXXX, XXXX__X_, _X__XXXX, XXXXX___,
- ___XXXXX, XXXX__X_, _X__XXXX, XXXXX___,
- ___XXXXX, XXXXXXXX, XXXXXXXX, XXXXX___,
- ___XXXXX, XXXX__X_, _X__XXXX, XXXXX___,
- ___XXXXX, XXXX__X_, _X__XXXX, XXXXX___,
- ___XXXXX, XXXXXXXX, XXXXXXXX, XXXXX___,
- ___XXXXX, XXXXXXXX, XXXXXXXX, XXXXX___
- };
-
- static const unsigned char acPhone1[] = {
- ________, ________, ________, ________,
- ______XX, X_______, ________, ________,
- ____XXXX, XXXXX___, ________, ________,
- ____XXXX, XXXXXXX_, ________, ________,
- ___XXXXX, XXXXXXXX, X_______, ________,
- ___XXXXX, XXXXXXXX, XXX_____, ________,
- _____XXX, XXXX_XXX, XXXXX___, ________,
- _______X, XXXX___X, XXXXXXX_, ________,
- ________, _XX_____, _XXXXXXX, X_______,
- ________, ________, ___XXXXX, XXX_____,
- ________, ________, _____XXX, XXXXX___,
- ________, ________, _______X, XXXXXX__,
- ________, ________, ________, XXXXXXX_,
- ________, ________, ________, XXXXXXX_,
- ________, ________, _______X, XXXXXXXX,
- ________, ___XX___, ___XX__X, XXXXXXXX,
- ________, ___XX___, ___XX___, _XXXXXXX,
- ________, ___XX___, ___XX___, ___XXXX_,
- ________, ___XX___, ___XX___, _____XX_,
- _______X, XXXXXXXX, XXXXXXXX, X_______,
- ______XX, XXXXXXXX, XXXXXXXX, XX______,
- _____XXX, XXXX__X_, _X__XXXX, XXX_____,
- ____XXXX, XXXX__X_, _X__XXXX, XXXX____,
- ___XXXXX, XXXXXXXX, XXXXXXXX, XXXXX___,
- ___XXXXX, XXXX__X_, _X__XXXX, XXXXX___,
- ___XXXXX, XXXX__X_, _X__XXXX, XXXXX___,
- ___XXXXX, XXXXXXXX, XXXXXXXX, XXXXX___,
- ___XXXXX, XXXX__X_, _X__XXXX, XXXXX___,
- ___XXXXX, XXXX__X_, _X__XXXX, XXXXX___,
- ___XXXXX, XXXXXXXX, XXXXXXXX, XXXXX___,
- ___XXXXX, XXXXXXXX, XXXXXXXX, XXXXX___
- };
-
- static const unsigned char acPhone2[] = {
- ________, ________, ________, ________,
- ________, ________, _______X, XX______,
- ________, ________, ___XXXXX, XXXX____,
- ________, ________, _XXXXXXX, XXXX____,
- ________, _______X, XXXXXXXX, XXXXX___,
- ________, _____XXX, XXXXXXXX, XXXXX___,
- ________, ___XXXXX, XXX_XXXX, XXX_____,
- ________, _XXXXXXX, X___XXXX, X_______,
- _______X, XXXXXXX_, _____XX_, ________,
- _____XXX, XXXXX___, ________, ________,
- ___XXXXX, XXX_____, ________, ________,
- __XXXXXX, X_______, ________, ________,
- _XXXXXXX, ________, ________, ________,
- _XXXXXXX, ________, ________, ________,
- XXXXXXXX, X_______, ________, ________,
- XXXXXXXX, X__XX___, ___XX___, ________,
- XXXXXXX_, ___XX___, ___XX___, ________,
- _XXXX___, ___XX___, ___XX___, ________,
- _XX_____, ___XX___, ___XX___, ________,
- _______X, XXXXXXXX, XXXXXXXX, X_______,
- ______XX, XXXXXXXX, XXXXXXXX, XX______,
- _____XXX, XXXX__X_, _X__XXXX, XXX_____,
- ____XXXX, XXXX__X_, _X__XXXX, XXXX____,
- ___XXXXX, XXXXXXXX, XXXXXXXX, XXXXX___,
- ___XXXXX, XXXX__X_, _X__XXXX, XXXXX___,
- ___XXXXX, XXXX__X_, _X__XXXX, XXXXX___,
- ___XXXXX, XXXXXXXX, XXXXXXXX, XXXXX___,
- ___XXXXX, XXXX__X_, _X__XXXX, XXXXX___,
- ___XXXXX, XXXX__X_, _X__XXXX, XXXXX___,
- ___XXXXX, XXXXXXXX, XXXXXXXX, XXXXX___,
- ___XXXXX, XXXXXXXX, XXXXXXXX, XXXXX___
- };
-
- static const GUI_BITMAP bm_1bpp_0 = { 32, 31, 4, 1, acPhone0, &Palette}; (2)
- static const GUI_BITMAP bm_1bpp_1 = { 32, 31, 4, 1, acPhone1, &Palette};
- static const GUI_BITMAP bm_1bpp_2 = { 32, 31, 4, 1, acPhone2, &Palette};
-
- /*******************************************************************
- *
- * static code
- *
- ********************************************************************
- */
- /*******************************************************************
- *
- * _Wait
- */
- static int _Wait(int Delay) {(3)
- int EndTime;
- int r;
-
- r = 1;
- EndTime = GUI_GetTime() + Delay;
- while (GUI_GetTime() < EndTime) {
- GUI_Exec();
- if (GUI_GetKey() == GUI_ID_OK) {
- r = 0;
- break;
- }
- }
- return r;
- }
-
- /*******************************************************************
- *
- * _DemoButton
- */
- static void _DemoButton(void) {
- BUTTON_Handle hButton;
-
- GUI_SetFont(&GUI_Font8x16);
- GUI_DispStringHCenterAt("Click on phone button...", 160,80);
- GUI_Delay(500);
- //
- // Create the button and modify its attributes
- //
- hButton = BUTTON_Create(142, 100, 36, 40, GUI_ID_OK, WM_CF_SHOW);(4)
- BUTTON_SetBkColor (hButton, 1, GUI_RED);
- //
- // Loop until button is pressed
- //
- while (1) {
- BUTTON_SetBitmapEx(hButton, 0, &bm_1bpp_1, 2, 4); (5)
- BUTTON_SetBitmapEx(hButton, 1, &bm_1bpp_1, 2, 4);
- if (!_Wait(50)) break;
- BUTTON_SetBitmapEx(hButton, 0, &bm_1bpp_0, 2, 4);
- BUTTON_SetBitmapEx(hButton, 1, &bm_1bpp_0, 2, 4);
- if (!_Wait(45)) break;
- BUTTON_SetBitmapEx(hButton, 0, &bm_1bpp_2, 2, 4);
- BUTTON_SetBitmapEx(hButton, 1, &bm_1bpp_2, 2, 4);
- if (!_Wait(50)) break;
- BUTTON_SetBitmapEx(hButton, 0, &bm_1bpp_0, 2, 4);
- BUTTON_SetBitmapEx(hButton, 1, &bm_1bpp_0, 2, 4);
- if (!_Wait(45)) break;
- }
- BUTTON_SetBitmapEx(hButton, 0, &bm_1bpp_1, 2, 4);
- BUTTON_SetBitmapEx(hButton, 1, &bm_1bpp_1, 2, 4);
- GUI_ClearRect(0, 80, 319, 120);
- GUI_DispStringHCenterAt("You have answered the telephone", 160, 145);
- GUI_Delay(2000);
- //
- // Delete button object
- //
- BUTTON_Delete(hButton);
- GUI_ClearRect(0, 50, 319, 239);
- GUI_Delay(400);
- }
-
- /*********************************************************************
- *
- * Public code
- *
- **********************************************************************
- */
- /*********************************************************************
- *
- * MainTask
- */
- void MainTask(void) {
- GUI_Init();
- GUI_SetBkColor(GUI_BLACK);
- GUI_Clear();
- GUI_SetColor(GUI_WHITE);
- GUI_SetFont(&GUI_Font24_ASCII);
- GUI_DispStringHCenterAt("WIDGET_PhoneButton - Sample", 160, 5);
- while (1) {
- _DemoButton();
- }
- }
复制代码
1. 图片数据的调色板。 2. 用于应用函数调用的GUI_BITMAP格式图片数据。 3. 按键时间等待函数,如果超过了设置的等待时间,按钮还没有按下,那么返回1,如果在设置的时间内按下了,返回0. 4. 按钮按下时的背景颜色。 5. 设置按钮按下或者常态时使用的图片。 显示效果如下: |