Hello.c
上传用户:liaojie
上传日期:2021-10-23
资源大小:21k
文件大小:13k
源码类别:

BREW编程

开发平台:

Visual C++

  1. /*===========================================================================
  2.   FILE: Hello.c
  3. ===========================================================================*/
  4. /*===============================================================================
  5. INCLUDES AND VARIABLE DEFINITIONS
  6. =============================================================================== */
  7. #include "AEEModGen.h"          // Module interface definitions
  8. #include "AEEAppGen.h"          // Applet interface definitions
  9. #include "AEEShell.h"           // Shell interface definitions
  10. #include "AEEstdlib.h"
  11. #include "Hello.bid"
  12. #include "res.brh"
  13. #include "hello.h"
  14. #define  TIMER 1000   //1秒=1000毫秒,按1秒计时
  15. //12个月,每个月的天数
  16. static uint8 g_arrMonthDay[12] 
  17.     = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  18. /*-------------------------------------------------------------------
  19. Function Prototypes
  20. -------------------------------------------------------------------*/
  21. static  boolean CKeyApp_HandleEvent(
  22.           IApplet* pMe, AEEEvent eCode, uint16 wParam, uint32 dwParam);
  23. boolean CKeyApp_InitAppData(CKeyApp* pMe);
  24. static void CKeyApp_DrawWeekText(CKeyApp * pMe); //画日 一 二 。。。
  25. static void CKeyApp_DrawDay(CKeyApp *pMe);       //画日期的布局
  26. static void CKeyApp_DrawRect(CKeyApp *pMe);      //画选中日期的框位置
  27. static void CKeyApp_ViewYearMonth(CKeyApp *pMe); //在左上角显示年-月
  28. static void CKeyApp_ViewHourMinSec(CKeyApp *pMe);//在右上角显示时:分:秒
  29. static void CKeyApp_CalculateDay(CKeyApp * pMe); //计算年月日是星期几
  30. static void CKeyApp_Draw(CKeyApp *pMe);          //绘制界面
  31. static void CKeyApp_UpEvent(CKeyApp * pMe);   //向上移动
  32. static void CKeyApp_DownEvent(CKeyApp * pMe); //向下移动
  33. static void CKeyApp_LeftEvent(CKeyApp * pMe); //向左移动
  34. static void CKeyApp_RightEvent(CKeyApp * pMe);//向右移动
  35. /*===========================================================================
  36. FUNCTION: AEEClsCreateInstance
  37. ===========================================================================*/
  38. int AEEClsCreateInstance(AEECLSID ClsId, IShell *pIShell, IModule *po, void **ppObj)
  39.     *ppObj = NULL;
  40.     
  41.     if (ClsId == AEECLSID_HELLO)
  42.     {
  43.         // Create the applet and make room for the applet structure
  44.         if ( AEEApplet_New(sizeof(CKeyApp),
  45.             ClsId, pIShell, po,
  46.             (IApplet**)ppObj,
  47.             (AEEHANDLER)CKeyApp_HandleEvent,
  48.             NULL) == TRUE ) 
  49.         {
  50.             if (CKeyApp_InitAppData((CKeyApp*)*ppObj))
  51. {
  52. //Data initialized successfully
  53. return(AEE_SUCCESS);
  54. }
  55. else
  56. {
  57. // AEEApplet_New was called.
  58. IAPPLET_Release((IApplet*)*ppObj);
  59. return EFAILED;
  60. }
  61.         } // end AEEApplet_New        
  62.     }
  63.     
  64.     return(EFAILED);
  65. }
  66. boolean CKeyApp_InitAppData(CKeyApp* pMe)
  67. {
  68.     JulianType mJulianDate;
  69.     // Get the device information for this handset.
  70.     // Reference all the data by looking at the pMe->DeviceInfo structure
  71.     // Check the API reference guide for all the handy device info you can get
  72.     pMe->m_DeviceInfo.wStructSize = sizeof(pMe->m_DeviceInfo);
  73.     ISHELL_GetDeviceInfo(pMe->a.m_pIShell, &pMe->m_DeviceInfo);
  74.     GETJULIANDATE(0, &mJulianDate);
  75.     pMe->m_nYear  = mJulianDate.wYear;
  76.     pMe->m_nMonth = mJulianDate.wMonth;
  77.     pMe->m_nDay   = mJulianDate.wDay;
  78.     pMe->m_nToday = mJulianDate.wDay;
  79.     pMe->m_nWeek  = 0;     //一号是星期几
  80.     pMe->m_nToday = 0 ;    //今天是几号
  81.     pMe->m_nFirstDay = 0;  //一号是今年的第几天
  82. IDisplay_ClearScreen(pMe->a.m_pIDisplay) ; 
  83.     // Insert your code here for initializing or allocating resources...
  84.     // if there have been no failures up to this point then return success
  85.     return TRUE;
  86. }
  87. /*===========================================================================
  88. FUNCTION SampleAppWizard_HandleEvent
  89. ===========================================================================*/
  90. static boolean CKeyApp_HandleEvent(IApplet* pMe, AEEEvent eCode, uint16 wParam, uint32 dwParam)
  91. {  
  92.     //    AEEDeviceInfo  deviceInfo;
  93.     CKeyApp *pApp = (CKeyApp *)pMe;
  94.     switch (eCode) 
  95.     {
  96.         // App is told it is starting up
  97.         case EVT_APP_START:   
  98.             CKeyApp_DrawWeekText (pApp);  //画日 一 二 。。。
  99.             CKeyApp_Draw (pApp);
  100.             ISHELL_SetTimer (pApp->a.m_pIShell, TIMER, 
  101.                 (PFNNOTIFY) CKeyApp_ViewHourMinSec, (void *)pApp );
  102.             return(TRUE);
  103.         
  104.         case EVT_APP_STOP:
  105.             ISHELL_CancelTimer (pApp->a.m_pIShell,
  106.                 (PFNNOTIFY) CKeyApp_ViewHourMinSec, pApp );
  107.             return(TRUE);
  108.     
  109.         // App is told it is KEY
  110.         case EVT_KEY:
  111.             switch(wParam) 
  112.             { 
  113.                 case AVK_UP:  
  114.                     CKeyApp_UpEvent (pApp);
  115.                     break; 
  116.             
  117.                 case AVK_DOWN:  
  118.                     CKeyApp_DownEvent (pApp);
  119.                     break; 
  120.             
  121.                 case AVK_LEFT:  
  122.                     CKeyApp_LeftEvent (pApp);
  123.                     break; 
  124.         
  125.                 case AVK_RIGHT: 
  126.                     CKeyApp_RightEvent (pApp);
  127.                     break; 
  128.         
  129.                 default:  
  130.                     return FALSE;            
  131.             } 
  132.             CKeyApp_Draw (pApp);
  133.             return TRUE; 
  134.     
  135.         default:
  136.             break;
  137.     }
  138.     
  139.     return FALSE;
  140. }
  141. // 重刷整个屏幕
  142. static void CKeyApp_Draw(CKeyApp *pMe)
  143. {
  144.     pMe->m_nFirstDay = 0;        
  145.     CKeyApp_ViewYearMonth (pMe); //在左上角显示年-月
  146.     CKeyApp_CalculateDay (pMe);  //计算年月日是星期几
  147.     CKeyApp_DrawDay (pMe);       //画日期的布局
  148.     CKeyApp_DrawRect (pMe);      //画选中日期的框位置
  149. }
  150. static void CKeyApp_DrawWeekText(CKeyApp * pMe)
  151. {
  152.     // print header   
  153.     RGBVAL clr ;
  154.     uint8 i;
  155.     AECHAR sTextBuf[100]={0};
  156.     
  157.     clr = IDisplay_SetColor (
  158.         pMe->a.m_pIDisplay, CLR_USER_TEXT, MAKE_RGB(0, 0, 255));
  159.     
  160.     for (i=0; i<7; i++)
  161.     {
  162.         MEMSET (sTextBuf, 0, sizeof(sTextBuf));
  163.         ISHELL_LoadResString (pMe->a.m_pIShell, 
  164.                  RES_RES_FILE, 
  165.  (int16)(IDS_WEEK_TITLE+i), 
  166.  sTextBuf, 
  167.  ARRAY_SIZE(sTextBuf));
  168.         IDISPLAY_DrawText (pMe->a.m_pIDisplay,   // Display instance
  169.             AEE_FONT_BOLD,                       // Use BOLD font
  170.             sTextBuf,                            // Text - Normally comes from resource
  171.             -1,                                  // -1 = Use full string length
  172.             (pMe->m_DeviceInfo.cxScreen/7)*i+2,  // Ignored - IDF_ALIGN_CENTER
  173.             pMe->m_DeviceInfo.cyScreen/8,        // Ignored - IDF_ALIGN_MIDDLE
  174.             NULL,                                // No clipping
  175.             NULL);
  176.         
  177.     }
  178.     
  179.     IDisplay_SetColor (pMe->a.m_pIDisplay, CLR_USER_TEXT, clr);
  180.     IDISPLAY_Update (pMe->a.m_pIDisplay);    
  181. }
  182. //确定这一个月的一号所显示的位置,根据这个月的天数来往后一次显示日期。
  183. static void CKeyApp_DrawDay(CKeyApp *pMe)
  184. {
  185.     uint16 i;
  186.     AECHAR* psTextBuf;
  187.     AEERect day = {
  188.         0, pMe->m_DeviceInfo.cyScreen/4,
  189.         pMe->m_DeviceInfo.cxScreen,
  190.         pMe->m_DeviceInfo.cyScreen-pMe->m_DeviceInfo.cyScreen/4
  191.     };
  192. IDISPLAY_EraseRect(pMe->a.m_pIDisplay,&day);
  193.     psTextBuf = (AECHAR*) MALLOC( 100 * sizeof( AECHAR ) );
  194.     for (i=1; i<=g_arrMonthDay[pMe->m_nMonth-1]; i++)
  195.     {
  196.         //初始化内存为0
  197.         MEMSET(psTextBuf, 0,100 * sizeof( AECHAR ) );
  198.         WSPRINTF(psTextBuf, 100 * sizeof( AECHAR ), L"%d", i);
  199.         
  200.         IDISPLAY_DrawText(pMe->a.m_pIDisplay,    // Display instance
  201.             AEE_FONT_BOLD,       // Use BOLD font
  202.             psTextBuf,           // Text - Normally comes from resource
  203.             -1,                  // -1 = Use full string length
  204.             (((i-1+pMe->m_nWeek)%7))*(pMe->m_DeviceInfo.cxScreen/7)+2,    // Ignored - IDF_ALIGN_CENTER
  205.             (((i-1+pMe->m_nWeek)/7)+2)*(pMe->m_DeviceInfo.cyScreen/8),    // Ignored - IDF_ALIGN_MIDDLE
  206.             NULL,                // No clipping
  207.             NULL);
  208.     }
  209.     
  210.     IDISPLAY_Update(pMe->a.m_pIDisplay);
  211. }
  212. static void CKeyApp_DrawRect(CKeyApp *pMe)
  213. {
  214.     CKeyApp *pApp = (CKeyApp *)pMe;
  215.     AEERect rc = {
  216.         ((pMe->m_nDay - 1 + pMe->m_nWeek)%7) * (pMe->m_DeviceInfo.cxScreen/7),
  217.         ((pMe->m_nDay - 1 + pMe->m_nWeek)/7 + 2) * (pMe->m_DeviceInfo.cyScreen/8),
  218.         15,
  219.         15
  220.     }; 
  221.     
  222.     IDISPLAY_DrawRect (pApp->a.m_pIDisplay, &rc, 0xff000000, 0x00ff0000, IDF_RECT_FRAME); 
  223.     IDISPLAY_Update (pApp->a.m_pIDisplay); 
  224. }
  225. //显示时间年月在手机左上角
  226. static void CKeyApp_ViewYearMonth(CKeyApp *pMe)
  227. {
  228.     AECHAR sTextBuf[20] = {0};
  229.     AEERect ym = { 0, 0, 50, pMe->m_DeviceInfo.cyScreen/8 };
  230.     WSPRINTF(sTextBuf, sizeof(sTextBuf), L"%d-%d",pMe->m_nYear,pMe->m_nMonth);
  231. IDISPLAY_EraseRect(pMe->a.m_pIDisplay,&ym);
  232.     IDISPLAY_DrawText (pMe->a.m_pIDisplay,    // Display instance
  233.         AEE_FONT_BOLD,      // Use BOLD font
  234.         sTextBuf,           // Text - Normally comes from resource
  235.         -1,                 // -1 = Use full string length
  236.         0,                  // Ignored - IDF_ALIGN_CENTER
  237.         0,                  // Ignored - IDF_ALIGN_MIDDLE
  238.         NULL,               // No clipping
  239.         NULL);
  240.     
  241.     IDISPLAY_Update(pMe->a.m_pIDisplay);
  242. }
  243. static void CKeyApp_ViewHourMinSec(CKeyApp *pMe)
  244. {
  245.     CKeyApp *pApp = (CKeyApp *)pMe;
  246.     JulianType mJulianDate;
  247.     AECHAR sTextBuf[20] = {0};
  248.     AEERect hms = {
  249.         pMe->m_DeviceInfo.cxScreen-50,  0,
  250.     50,  pMe->m_DeviceInfo.cyScreen/8
  251.     };
  252.     GETJULIANDATE(0, &mJulianDate);     
  253. IDISPLAY_EraseRect(pMe->a.m_pIDisplay, &hms);
  254.        
  255.     if (pMe->m_nToday != mJulianDate.wDay)
  256.     {
  257.         pMe->m_nYear =  mJulianDate.wYear;
  258.         pMe->m_nMonth = mJulianDate.wMonth;
  259.         pMe->m_nDay =   mJulianDate.wDay;
  260.         pMe->m_nToday = mJulianDate.wDay;
  261.     }
  262.     WSPRINTF (sTextBuf, sizeof(sTextBuf), 
  263.              L"%02d:%02d:%02d",mJulianDate.wHour,
  264.              mJulianDate.wMinute,mJulianDate.wSecond);
  265.     
  266.     IDISPLAY_DrawText (pMe->a.m_pIDisplay,    // Display instance
  267.         AEE_FONT_BOLD,       // Use BOLD font
  268.         sTextBuf,            // Text - Normally comes from resource
  269.         -1,                  // -1 = Use full string length
  270.         pMe->m_DeviceInfo.cxScreen-50,  // Ignored - IDF_ALIGN_CENTER
  271.         0,            // Ignored - IDF_ALIGN_MIDDLE
  272.         NULL,         // No clipping
  273.         NULL);
  274.     IDISPLAY_Update(pMe->a.m_pIDisplay);
  275.     ISHELL_SetTimer( pApp->a.m_pIShell, TIMER,
  276.         (PFNNOTIFY) CKeyApp_ViewHourMinSec, (void *)pApp );
  277. }
  278. //计算年月日是星期几
  279. static void CKeyApp_CalculateDay(CKeyApp * pMe)
  280. {
  281.     //1,判断这一年是否为闰年,是设flg=1,否flg=0
  282.     uint16 i;
  283.     if ((pMe->m_nYear%400 == 0)
  284.         || ((pMe->m_nYear%4==0) && (pMe->m_nYear%100!=0)))
  285.     {
  286.         g_arrMonthDay[1] = 29;//闰年,二月有29天
  287.     }
  288.     else
  289.     {
  290.         g_arrMonthDay[1] = 28;
  291.     }
  292.     
  293.     //2,计算显示月份的一号为这一年的第几天pMe->m_nDay
  294.     for ( i=0; i<pMe->m_nMonth-1; i++)
  295.     {
  296.         pMe->m_nFirstDay += g_arrMonthDay[i];
  297.     }
  298.     pMe->m_nFirstDay = pMe->m_nFirstDay+1;//一号为这一年的第几天pMe->m_nDay
  299.     
  300.     /*3 计算1号是周几
  301.     pMe->m_nYear是年份数,pMe->m_nFirstDay是这一天在这一年中的累积天数,
  302.     也就是这一天在这一年是第几天。算出来的pMe->m_nDurday除以7,
  303.     余数是几就是星期几(pMe->m_nWeek),如果余数是0,则为星期日*/
  304.     i = (pMe->m_nYear-1) + (pMe->m_nYear-1)/4 - (pMe->m_nYear-1)/100
  305.         +  (pMe->m_nYear-1)/400 + pMe->m_nFirstDay;
  306.     pMe->m_nWeek = i%7;
  307. }
  308. // 向上按键时计算年月日,移至上一周
  309. static void CKeyApp_UpEvent(CKeyApp * pMe)
  310. {
  311.     pMe->m_nDay -= 7;
  312.     // 换月份
  313.     if (pMe->m_nDay <= 0)
  314.     {
  315.         pMe->m_nMonth--;
  316.         
  317.         if (pMe->m_nMonth <= 0)
  318.         {
  319.             pMe->m_nYear--;
  320.             pMe->m_nMonth = 12;
  321.         }
  322.         pMe->m_nDay = g_arrMonthDay[pMe->m_nMonth-1] + pMe->m_nDay;
  323.     }
  324. }
  325. // 向下按键时计算年月日,移至下一周
  326. static void CKeyApp_DownEvent(CKeyApp * pMe)
  327. {
  328. uint16 NowMonth=pMe->m_nMonth;
  329.     pMe->m_nDay += 7;
  330.     // 换月份
  331.     if (pMe->m_nDay > g_arrMonthDay[pMe->m_nMonth-1])
  332.     {
  333.         pMe->m_nMonth ++;
  334.         if (pMe->m_nMonth > 12)
  335.         {
  336.             pMe->m_nYear++;
  337.             pMe->m_nMonth = 1;
  338.         }
  339.         pMe->m_nDay = pMe->m_nDay%g_arrMonthDay[NowMonth-1];
  340.     }
  341. }
  342. // 向左按键时计算年月日,移至上一日
  343. static void CKeyApp_LeftEvent(CKeyApp * pMe)
  344. {
  345.     pMe->m_nDay --;
  346.     // 换月份
  347.     if (pMe->m_nDay <= 0)
  348.     {
  349.         pMe->m_nMonth--;
  350.         if (pMe->m_nMonth <= 0)
  351.         {
  352.             pMe->m_nYear--;
  353.             pMe->m_nMonth = 12;
  354.         }
  355.         pMe->m_nDay = g_arrMonthDay[pMe->m_nMonth-1];
  356.     }
  357. }
  358. //向右按键时计算年月日,移至下一日
  359. static void CKeyApp_RightEvent(CKeyApp * pMe)
  360. {
  361.     pMe->m_nDay ++;
  362.     // 换月份
  363.     if (pMe->m_nDay > g_arrMonthDay[pMe->m_nMonth-1])
  364.     {
  365.         pMe->m_nMonth ++;
  366.         if (pMe->m_nMonth > 12)
  367.         {
  368.             pMe->m_nYear ++;
  369.             pMe->m_nMonth = 1;
  370.         }
  371.         pMe->m_nDay = 1;
  372.     }
  373. }